Jump to content

Archived

This topic is now archived and is closed to further replies.

doa24uk

get 50 results but loop table is sets of 5

Recommended Posts

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 thanks

DoA

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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>&pound <? echo "$price" ?></td>
<?
}
?>
</tr>[/code]

DoA

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 image


Cheers!


DoA

[attachment deleted by admin]

Share this post


Link to post
Share on other sites
[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 image


Cheers!


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]

Share this post


Link to post
Share on other sites
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>&pound; ".$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

Share this post


Link to post
Share on other sites
What code are you using to set $url, $ltitle, $desc and $price?  It looks like THOSE values are always the same.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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>&pound <? echo "$price" ?></td>
<?
if (++$count == 5) {
echo '</tr>';
$count = 0;
}
}
if($count) {
while ($count++ < 5) echo '<td>&nbsp;</td>';
echo '</tr>';
}
?>
</table>[/code]

Share this post


Link to post
Share on other sites
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 worked
echo "<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 array

for($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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.