homepage Welcome to WebmasterWorld Guest from 23.20.220.61
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
do.while loop to create data table
find the ID of the previous iteration of the loop
Mr_Cat




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

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.

 

cffrost2




msg:4533717
 3:45 pm on Jan 6, 2013 (gmt 0)

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.

skoff




msg:4534290
 1:43 pm on Jan 8, 2013 (gmt 0)

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!

skoff




msg:4534333
 4:12 pm on Jan 8, 2013 (gmt 0)

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'];
}

Mr_Cat




msg:4534374
 5:34 pm on Jan 8, 2013 (gmt 0)

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?

skoff




msg:4534376
 5:48 pm on Jan 8, 2013 (gmt 0)

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

Mr_Cat




msg:4534397
 6:35 pm on Jan 8, 2013 (gmt 0)

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

skoff




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved