doa24uk Posted October 30, 2006 Share Posted October 30, 2006 Hi all,I wanna get 50 results from my table, [b]fine[/b][code]$query="SELECT * FROM **** ORDER BY price ASC LIMIT 50";[/code]and then I wanna loop until I've got all the results - [b]fine[/b]Now, what I wanna do is output my code in rows of 5 - how can I do that?So with my 50 results I'll end up with 10 rows and 5 columns each time.I'm sure its something really simple but I can't figure it out!Many thanksDoA Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/ Share on other sites More sharing options...
genericnumber1 Posted October 30, 2006 Share Posted October 30, 2006 try something like this...[code]<?php$result = mysql_query("SELECT * FROM **** ORDER BY price ASC LIMIT 50");$i = 0;while($row = mysql_fetch_array($result)){ ++$i; // logic blah blah blah if($i == 5){ echo "<br>"; $i = 0; }}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-116972 Share on other sites More sharing options...
doa24uk Posted October 30, 2006 Author Share Posted October 30, 2006 It doesn't work cos I'm using a table.Here's the complete code, I wanna split it up after every 5 results.If anyone can help i'd be SUPER greatful![code]<?$query="SELECT * FROM *** ORDER BY *** DESC LIMIT 50";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();echo '<tr align="center">';$i=0;while ($i < $num) {$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++;?><td><a href="<? echo "$url"; ?>" Alt="<? echo "$ltitle"; ?>"><? echo "$ltitle"; ?></a><br><? echo "$ldesc"; ?><br><b>£ <? echo "$price" ?></td><?}?></tr>[/code]DoA Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-116987 Share on other sites More sharing options...
.josh Posted October 30, 2006 Share Posted October 30, 2006 you know, it's not really that great of a stretch of the imagination to apply the same concept to tables:[code]$i = 0;echo "<table><tr>";while ($i < $num) { $i++; echo "<td>stuff</td>"; if ($i == 5) { echo "</tr><tr>"; }}echo "</tr></table>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-116989 Share on other sites More sharing options...
doa24uk Posted October 30, 2006 Author Share Posted October 30, 2006 can I set it up so that it says if i = 5, 10,15,20 etc...The code above is giving really weird results - the last result is correct (Title 1 - Description 1 - 102.00)See attached imageCheers!DoA[attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-117005 Share on other sites More sharing options...
doni49 Posted October 31, 2006 Share Posted October 31, 2006 [quote author=doa24uk link=topic=113275.msg460238#msg460238 date=1162248174]can I set it up so that it says if i = 5, 10,15,20 etc...The code above is giving really weird results - the last result is correct (Title 1 - Description 1 - 102.00)See attached imageCheers!DoA[/quote]Try this on for size (it's CV's code--just modified slightly):[code]$i = 0;$j = 0;echo "<table><tr>";while ($i < $num) { $i++; $j++; echo "<td>stuff</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></table>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-117121 Share on other sites More sharing options...
.josh Posted October 31, 2006 Share Posted October 31, 2006 oops. i meant to do that. i had a brain fart. my bad. Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-117131 Share on other sites More sharing options...
doa24uk Posted October 31, 2006 Author Share Posted October 31, 2006 a brain fart lol I like it.Thats working - kind of! But its only looping the first result 7 times!Here's the code I'm now using - can you shed any light on it?[code]<?$query="SELECT * FROM ***** ORDER BY price DESC LIMIT 50";$result=mysql_query($query);$num=mysql_numrows($result);mysql_close();$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]Thanks for your time.DoA Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-117251 Share on other sites More sharing options...
doni49 Posted November 5, 2006 Share Posted November 5, 2006 What code are you using to set $url, $ltitle, $desc and $price? It looks like THOSE values are always the same. Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-119762 Share on other sites More sharing options...
JasonLewis Posted November 5, 2006 Share Posted November 5, 2006 isnt it mysql_num_rows not mysql_numrows or dosnt it matter? and doni49 is right. are you even setting the variables anywhere. i cant see them defined... Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-119792 Share on other sites More sharing options...
sasa Posted November 5, 2006 Share Posted November 5, 2006 try[code]<?$query = "SELECT * FROM *** ORDER BY *** DESC LIMIT 50";$result = mysql_query($query);$num = mysql_numrows($result);mysql_close();$i = 0;$count = 0;echo '<table>';while ($i < $num) { $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++; if (!$count) echo '<tr align="center">';?><td><a href="<? echo "$url"; ?>" Alt="<? echo "$ltitle"; ?>"><? echo "$ltitle"; ?></a><br><? echo "$ldesc"; ?><br><b>£ <? echo "$price" ?></td><? if (++$count == 5) { echo '</tr>'; $count = 0; }}if($count) { while ($count++ < 5) echo '<td> </td>'; echo '</tr>';}?></table>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-119859 Share on other sites More sharing options...
Stooney Posted November 5, 2006 Share Posted November 5, 2006 I will show you the easiest way to go about it.[code]$query="SELECT * FROM **** ORDER BY price ASC LIMIT 50"; //query$result=@mysql_query($query); //query database if($result){ //if query workedecho "<table border=1> //create table and 'title' columns <tr><td>Row1</td><td>Row2</td><td>Row3</td><td>Row4</td><td>Row5</td></tr>";$data=mysql_fetch_array($result); //make data readable in an array$num=count($data); //count number of elements in arrayfor($i=0; $i=$num; $i+5) //increment in 5's{ echo "<tr><td>$data[$i]</td> //display array element $i and the 4 elements following $i <td>$data[$i+1]</td> <td>$data[$i+2]</td> <td>$data[$i+3]</td> <td>$data[$i+4]</td></tr>";}echo "</table>";[/code]that is the easiest i could come up with. I didn't test it, so watch for slight typo's. Quote Link to comment https://forums.phpfreaks.com/topic/25629-get-50-results-but-loop-table-is-sets-of-5/#findComment-119987 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.