Bit of PHP/Mysql help needed please

Bit of PHP/Mysql help needed please

Author
Discussion

BliarOut

Original Poster:

72,857 posts

240 months

Thursday 29th July 2010
quotequote all
I'm building a form to display some data. The form runs through twice, once as the 'original' and once as the duplicate. All is well except when it loops a second time it displays a blank row at the head of the table.

As you can see I've figured out that I need to reset the pointer but I'm sure I should somehow 'call' the row ready for the next time the script loops. Can anyone point me in the right direction?

{{{
<?php
do { ?>
<tr class="data">
<td><?php echo $row_ItemsRecordset['ColA']; ?></td>
<td><?php echo $row_ItemsRecordset['ColB']; ?></td>
<td><?php echo $row_ItemsRecordset['ColC']; ?></td>
<td><?php echo $row_ItemsRecordset['ColD']; ?></td>
<td>&nbsp;</td>
</tr>
<?php
} while ($row_ItemsRecordset = mysql_fetch_assoc($ItemsRecordset));
//============================================================+
//Reset the recordset before reusing it in the repeat region.
//============================================================+
mysql_data_seek($ItemsRecordset,0); ?>
}}}

lestag

4,614 posts

277 months

Thursday 29th July 2010
quotequote all
mysql_data_seek($ItemsRecordset,1);

as in row 1 rather than row 0 maybe?

sonic_2k_uk

4,007 posts

208 months

Thursday 29th July 2010
quotequote all
BliarOut said:
I'm building a form to display some data. The form runs through twice, once as the 'original' and once as the duplicate. All is well except when it loops a second time it displays a blank row at the head of the table.

As you can see I've figured out that I need to reset the pointer but I'm sure I should somehow 'call' the row ready for the next time the script loops. Can anyone point me in the right direction?

{{{
<?php
do { ?>
<tr class="data">
<td><?php echo $row_ItemsRecordset['ColA']; ?></td>
<td><?php echo $row_ItemsRecordset['ColB']; ?></td>
<td><?php echo $row_ItemsRecordset['ColC']; ?></td>
<td><?php echo $row_ItemsRecordset['ColD']; ?></td>
<td>&nbsp;</td>
</tr>
<?php
} while ($row_ItemsRecordset = mysql_fetch_assoc($ItemsRecordset));
//============================================================+
//Reset the recordset before reusing it in the repeat region.
//============================================================+
mysql_data_seek($ItemsRecordset,0); ?>
}}}
Sticking it into a foreach should solve your problem

{{{

$query = mysql_query ("SELECT...");

foreach ($row = mysql_fetch_assoc ($query))
{
?>
....
<?php
}

mysql_data_seek ($query, 0);

foreach ($row = mysql_fetch_assoc ($query))
{
?>
....
<?php
}

}}}


ETA: I'd also look at PDO under php5.

Edited by sonic_2k_uk on Thursday 29th July 13:35