Click to See Complete Forum and Search --> : Can't seem to get db contents.


Nibinaear
February 10th, 2005, 05:31 AM
For some reason I can't get the database contents to output to the screen in each of these while loops. If the loop is placed just after the $query variable, it works, but placed seperately in different <? ?> tags it just can't seem to see the variables, does this have something to do with the scope of the variables? I've tried using global $query, $input, but that doesn't help.


<?

include 'imp/odbc_connection.php';

$input = "SELECT DISTINCT * FROM assignment.assessments";

$query = odbc_exec($odbc,$input);

?>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
<table bgcolor = "#009999" width: "70%" height= "70%">
<tr>
<td valign = "top" align = "left">
<form name = "form1"><br/>

<!--Module Box-->
Module:&nbsp;
<select name = "module">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Module']."";
}
?>
"></option>
</select>
<br/>

<!--Assessment Type-->
Assessment Type:&nbsp;
<select name = "type">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Type']."";
}
?>
"></option>
</select>
<br/>

<!--Semester, 1 or 2?-->
Semester:&nbsp;
<!--50-->
<select name = "semester">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Semester']."";
}
?>
"></option>
</select>
<br/>

<!--Percentage Mark-->
Percentage Mark Requirement:&nbsp;
<select name = "percentage">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Percentage mark']."";
}
?>
"></option>
</select>
<br/>

<!--Date Due-->
Date Due:&nbsp;
<select name = "date">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Date Due']."";
}
?>
"></option>
</select>
<br/>

<!--Preperation Time-->
Preparation Time:&nbsp;
<select name = "prep">
<option value = "
<?
while($record = odbc_fetch_array($query))
{
echo "".$record['Preperation time']."";
}
?>
"></option>
</select>
<br/>

</form>
</td>
</tr>
</table>
</body>

</html>

Nibinaear
February 11th, 2005, 11:15 AM
It's almost like the first block of php code, can't see the variables in the second bit, but it compiles. Why is this?

bigBA
February 11th, 2005, 11:57 AM
first, your naming scheme is a bit mis-leading.

input is in fact the query, and query is the result. maybe you will changethis, because so people would easier understand your code and don't have to look at the top lines to see what query in deed is.

so.. yeah, had the same problem right yesterday.

xxxx_fetch_array functions move the internal row counter, so at the end of you (first) while loop it stands at the end of the result set and so following xxx_fetch_arrays (the second, third... while loop) won't get any data.

what you have to do is to rewind the internal row pointer of the result set after each loop. (so the others will also start at the first row)
see if there is any odbc_seek method or something like that, it does exist for mysql, so i think it does fpr odbc

bigBA
February 11th, 2005, 12:07 PM
well, no... no seek method or equivalent available...

but there is a second, optional parameter in odbc_fetch_array - the row number.
and in addition to this there is a function called odbc_num_rows, returning the number of rows in the result set.

so you can use for loops instead of while loops in such a way:

<?
...
$result = odbc_exec(...);
$numrows = odbc_num_rows($result);
?>

...

<?
for($i=0; $i < $numrows; $i++)
{
$row = odbc_fetch_array($result,$i);
echo ...
}
?>

...

<?
for($i=0; $i < $numrows; $i++)
{
$row = odbc_fetch_array($result,$i);
echo ...
}
?>