doa24uk Posted October 31, 2006 Share Posted October 31, 2006 Hi all,my loop is now working & outputting correctly, HOWEVER it is only grabbing the first result and repeatring it.I've narrrowed it down to the fact that $j is resetting to 0 so each time it resets it picks up teh first result again. Am I right in thinking this?If you could have a look that'd be great![code]<?$query="SELECT * FROM *** ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();// logic$i = 0;$j = 0;echo "<tr>";while ($i < $num) { $i++; $j++; echo "<td><a href=".$url." Alt=".$ltitle.">".$ltitle."</a><br>".$ldesc."<br><b>£ ".$price."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>";echo "</tr>";}}?>[/code]It's driving me nuts!Many thanksDoA Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/ Share on other sites More sharing options...
kenrbnsn Posted October 31, 2006 Share Posted October 31, 2006 You're not fetching any records from the database at all, so I'm surprised you're getting any records displayed.Try something like this:[code]<?php$query="SELECT * FROM *** ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_numrows($result);// logic$j = 0;echo "<tr>";while (row = mysql_fetch_assoc($result)) { $j++; echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>£ ".$row['price']."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>";echo "</tr>";}}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117319 Share on other sites More sharing options...
HuggieBear Posted October 31, 2006 Share Posted October 31, 2006 Try changing this:[code=php:0]$num=mysql_numrows($result);[/code] to this [code=php:0]$num=mysql_num_rows($result);[/code]You named the function incorrectly.Huggie Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117323 Share on other sites More sharing options...
doa24uk Posted October 31, 2006 Author Share Posted October 31, 2006 Sorry guys, no luck - this code givesParse error: syntax error, unexpected '=' in /home/lazyrec/public_html/index.php on line 47[code]<?php$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_num_rows($result);mysql_close();$buid = mysql_result($result,$i,"buid");$ltitle = mysql_result($result,$i,"ltitle");$ldesc = mysql_result($result,$i,"ldesc");$url = mysql_result($result,$i,"url");$price = mysql_result($result,$i,"price");$i = 0;$j = 0;echo "<tr>";while (row = mysql_fetch_assoc($result)) { $i++ $j++; echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>£ ".$row['price']."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>";echo "</tr>";}}?>[/code]Line 47 is the blank line before this one - [code]$buid = mysql_result($result,$i,"buid");[/code]DoAp.s. I've also tried without the spaces before & after the = Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117329 Share on other sites More sharing options...
kenrbnsn Posted October 31, 2006 Share Posted October 31, 2006 If you use my suggestion, you don't need the lines:[code]<?phpmysql_close();$buid = mysql_result($result,$i,"buid");$ltitle = mysql_result($result,$i,"ltitle");$ldesc = mysql_result($result,$i,"ldesc");$url = mysql_result($result,$i,"url");$price = mysql_result($result,$i,"price");$i = 0;?>[/code]You don't need the variable [b]$i[/b] at all.Ken Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117331 Share on other sites More sharing options...
trq Posted October 31, 2006 Share Posted October 31, 2006 [code=php:0]<?php$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_num_rows($result);$j = 0;echo "<tr>";while ($row = mysql_fetch_assoc($result)) { $i++ $j++; echo "<td><a href='".$row['url']."' Alt='".$row['ltitle']."'>".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>£ ".$row['price']."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>"; echo "</tr>"; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117333 Share on other sites More sharing options...
doa24uk Posted October 31, 2006 Author Share Posted October 31, 2006 [quote author=thorpe link=topic=113360.msg460578#msg460578 date=1162308814][code=php:0]<?php$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_num_rows($result);$j = 0;echo "<tr>";while ($row = mysql_fetch_assoc($result)) { $i++ $j++; echo "<td><a href='".$row['url']."' Alt='".$row['ltitle']."'>".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>£ ".$row['price']."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>"; echo "</tr>"; }}?>[/code][/quote]none of these are working - unexpected = 's and undexpected T_VARIABLES each timeHere's the code I'm currently running.[code]<?php$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_num_rows($result);$j = 0;echo "<tr>";while ($row = mysql_fetch_assoc($result)) { $j++; echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>£ ".$row['price']."</td>"; //if $j equals 5, then start a new row and reset $j to zero //let $i keep counting the current row. if ($j == 5) { $j = 0; echo "</tr><tr>"; echo "</tr>"; }}?>[/code]EDIT - I REMOVED THE $i++ in the code and it appears to be working brilliantly!hehe thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/25708-loop-only-grabs-first-result-displays-repeatedly/#findComment-117341 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.