Jump to content

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
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]
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
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 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]
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
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]
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.