Welcome to WebmasterWorld Guest from 54.167.157.247

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

do.while loop to create data table

find the ID of the previous iteration of the loop

   
2:42 pm on Jan 6, 2013 (gmt 0)

5+ Year Member



Hi folks,

if my database table reads like:

ID | text
17 | blah1
23 | blah2
39 | blah3

and to print it in a table I use:



if ($row = mysql_fetch_array($result)) {

echo "<table width='95%>";

do {
$id = $row['ID'];
$text = $row['text'];

echo "<tr>
<td>Here is the text - " . $text . "</td>
<td>The ID of the text is - " . $ID . "</td>
<td>The ID of the previous entry is - " . $x? . "</td>
</tr> ";
}

while($row = mysql_fetch_array($result));

echo "</table>";
};



How can I find/create $x?

ie

--------------------------------
Here is the text - blah3 | The ID of the text is - 39 | The ID of the previous entry is - 23
--------------------------------

Cheers

I did wonder if just putting in another query at the top of the page might work, but figured one query is better than two :) and there must be a neat simple way.
3:45 pm on Jan 6, 2013 (gmt 0)

5+ Year Member



My suggestion would be store each row into arrays.

$id = array();
$text = array();
While($row = mysql_fetch_array($result)) {
$id[] = $row['text'];
$text[] = $row['id'];
}

Then you'll be able to access each result separately if needed. Just a thought. Maybe a better way. Good luck.
1:43 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



First thing, forget the do while thing.. to pass through all records in your query you should use :
while($row = mysql_fetch_array($result)) {
}

and for what your trying to attempt, your best bet would be to put it in a temp variable. Set it with the $id at the end of the loop. So when the loop will pass to the second records and all others after it will still have the value of the previous $id. I dont know what you did want to do with the first record because there would be no "previous" id so this is the reason of the if/else i put.

$x = 0;
while($row = mysql_fetch_array($result)) {
$id = $row['ID'];
$text = $row['text'];`

echo "<tr>
<td>Here is the text - " . $text . "</td>
<td>The ID of the text is - " . $ID . "</td>
if($x !== 0) {
<td>The ID of the previous entry is - " . $x? . "</td>
}
else {
<td>&nbsp;</td>
}
</tr> ";

$x = $row['ID'];
}

hope this helps!
4:12 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



i made some mistakes heres the code :

$x = 0;
while($row = mysql_fetch_array($result)) {
$ID = $row['ID'];
$text = $row['text'];`

echo "<tr>
<td>Here is the text - " . $text . "</td>
<td>The ID of the text is - " . $ID . "</td>";
if($x !== 0) {
echo "<td>The ID of the previous entry is - " . $x? . "</td>";
}
else {
echo "<td>&nbsp;</td>";
}
echo "</tr>";

$x = $row['ID'];
}
5:34 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



Thanks skoff! :) will try it shortly and am assuming that to do the opposite, get the id of the previous iteration, I just swap the 'x=' round?
5:48 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



you mean to get the id of the next iteration?

and also just dont forget to remove the ? after your $x or you'll get an error
6:35 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



yea the next iteration was what I was after, thanks, but I was wondering if I wanted the previous one as well then I'd just swap the
x=0
and
x=$row['ID']
around.

My head is a bit fried from code now so I may be being dim :)

Cheers
7:35 pm on Jan 8, 2013 (gmt 0)

5+ Year Member



no the reason i set x=0 before the loop is because it need to exist inside of the loop the first time or it would return an error. the asnwer i gave you will return the previous id not the next one.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month