Forum Moderators: coopster

Message Too Old, No Replies

Problem with script not displaying the first record

Still scratching head

         

BadGoat

3:03 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



Hi!

The script snippet below displays all records of a given date, but it always leaves off the first record. And if there is only one record for that date, it appears blank. What am I doing wrong, that it skips the first record?

Thank you :)

Code:
$Day = $_GET['Day'];
$Year = $_GET['Year'];
$Month = $_GET['Month'];
$cal_string = date("Y-m-d", strtotime("$Month $Day $Year"));

$sqlquery = "SELECT id, diwtitle, date_string FROM diw_alpha WHERE date_string = '$cal_string'";

$queryresult = mysql_query($sqlquery) or die(" Could not execute mysql query!");
$row = mysql_fetch_row($queryresult);
$date_string = $row[0];
$diwtitle = $row[1];
$id = $row[2];

$tdcount = 1;
$numtd = 1; // number of cells per row

while($row = mysql_fetch_array($queryresult)) {
if ($tdcount == 1) echo "<tr>";
echo '<td>'.$row['date_string'].'</td><td><a href="damndiw4.php?id='.$row['id'].'"> '.$row['diwtitle'],'</a></td><td>'.$row['id'].'</td>';
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// close up table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;

turock

3:15 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



The problem is that you are doing $row = mysql_fetch_row($queryresult); which retreives the first record set and advances the data pointer to the next record if there is one. So after that when you do while($row = mysql_fetch_array($queryresult)) the data pointer is at the second record set, not the first one.

BadGoat

3:25 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



hi turock,

I follow you in your explanation.. I am pretty green to PHP, I am not sure how to implement a fix for this, whether it's a slight alteration to existing code, or if what I want to accomplish needs a complete re-write. Can you point me in the right direction?

EDIT: I notice that when I echo the variable $date_string, it returns the value of the id of the first record which is not displayed.. I am trying to debug this, and I am sure this is what's causing the initial problerm

BadGoat

3:56 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



Pardon me.. I fixed why the variables were not being echoed correctly. Now when I echo a variable, it displays the correct value for the record that is not being shown. for example, when I echo the $id, the value is 5, which is the id for the first record, which is not displayed.

HeadBut

4:07 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



I think A solution would be:

do{

}while($row = mysql_fetch_array($queryresult))

so you always get the first record before you move the pointer.
That's how I do it.
[us2.php.net...]

Good Luck

BadGoat

6:03 pm on Jun 23, 2005 (gmt 0)

10+ Year Member



hi Headbut,

I implemented the DO WHILE loop as recommended and it still leaves off the first record.. :(

jatar_k

6:06 pm on Jun 23, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



because you grab the first row here

$row = mysql_fetch_row($queryresult);

so when it gets here
while($row = mysql_fetch_array($queryresult)) {

it is always on the second row

maybe try mysql_data_seek [php.net] before your fetch array to reset the pointer

BadGoat

3:24 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



jatar:

Thank you! I added

mysql_data_seek($queryresult,0) ;

in front of my while statement and it works like a charm!

Many thanks :)