Jump to content


Photo

get 50 results but loop table is sets of 5


  • Please log in to reply
11 replies to this topic

#1 doa24uk

doa24uk
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 30 October 2006 - 09:35 PM

Hi all,

I wanna get 50 results from my table, fine

$query="SELECT * FROM **** ORDER BY price ASC LIMIT 50";

and then I wanna loop until I've got all the results - fine

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

#2 genericnumber1

genericnumber1
  • Members
  • PipPipPip
  • Advanced Member
  • 1,858 posts

Posted 30 October 2006 - 09:41 PM

try something like this...
<?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;
   }
}
?>


#3 doa24uk

doa24uk
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 30 October 2006 - 10:07 PM

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!

<?

$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>

DoA

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 30 October 2006 - 10:14 PM

you know, it's not really that great of a stretch of the imagination to apply the same concept to tables:
$i = 0;
echo "<table><tr>";
while ($i < $num) {
   $i++;
   echo "<td>stuff</td>";
   if ($i == 5) {
      echo "</tr><tr>";
   }
}
echo "</tr></table>";

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 doa24uk

doa24uk
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 30 October 2006 - 10:42 PM

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]

#6 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 31 October 2006 - 03:44 AM

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


Try this on for size (it's CV's code--just modified slightly):
$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>";

Don

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 31 October 2006 - 04:33 AM

oops. i meant to do that. i had a brain fart. my bad.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 doa24uk

doa24uk
  • Members
  • PipPipPip
  • Advanced Member
  • 102 posts

Posted 31 October 2006 - 01:12 PM

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?

<?

$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>";
?>


Thanks for your time.

DoA

#9 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 05 November 2006 - 12:23 AM

What code are you using to set $url, $ltitle, $desc and $price?  It looks like THOSE values are always the same.
Don

#10 JasonLewis

JasonLewis
  • Members
  • PipPipPip
  • Advanced Member
  • 3,351 posts
  • LocationVictoria, Australia

Posted 05 November 2006 - 01:43 AM

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...
Good luck with your coding.
Jason / ProjectFear / Jaysonic

#11 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 05 November 2006 - 06:50 AM

try
<?
$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>


#12 Stooney

Stooney
  • Members
  • PipPipPip
  • Advanced Member
  • 1,093 posts
  • LocationLas Cruces, NM

Posted 05 November 2006 - 04:50 PM

I will show you the easiest way to go about it.


$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>";





that is the easiest i could come up with.  I didn't test it, so watch for slight typo's.

<embed src='.wav' autostart='true' loop='false'
width='2' height='0'>
</embed>
i found that ERMMMMM thats called PHP!!!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users