Page 1 of 1

Query output

Posted: Sat Feb 23, 2008 12:05 am
by frozen
Hi guys, I'm new at php/mysql and im trying to fetch 2 fields of 10 rows of data. And I want to output the first 5 with the 2 fields and then the last 5 with just one field.

So it will look like this for the first 5

New Items in stock..

Product
Description

Product
Description

Then I want to put this for the next 5 products

Also now in stock..

Product
Product
Product


I have a basic script I been trying to work it out on but I have not had any luck. Would anyone be able to help me out or even point me in the direction of some tutorials that shows this.

Code: Select all

<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                     ('Error connecting to mysql');
 
$dbname = '';
mysql_select_db($dbname);
 
$query = 'SELECT *FROM `products` ORDER BY `products`.`ID` ASC'; 
$result = mysql_query($query);
 
while($row = mysql_fetch_array($result)) 
 
{
    echo "{$row['product_title']} <br>" .
         "{$row['description']} <br><br>"; 
 
} 
 
mysql_close($conn);
 
?>
Thanks

Bradley

Re: Query output

Posted: Sat Feb 23, 2008 4:18 am
by EverLearning
First you need to limit your resultset to 10 rows

Code: Select all

 
$query = '
SELECT *
FROM `products`
ORDER BY `products`.`ID` ASC
LIMIT 0, 10';
 
and then you need to use a counter, so you know when to start displaying the second part of your data

Code: Select all

 
$counter = 0;
echo "New Items in stock...<br>";
while($counter < 5 && ($row = mysql_fetch_array($result)) )
{
    echo "{$row['product_title']} <br>" .
         "{$row['description']} <br><br>";
$counter++;
}
 
 
echo "Also now in stock..<br>";
while($row = mysql_fetch_array($result))
{
    echo "{$row['product_title']} <br><br>";
 
}
 
.

Re: Query output

Posted: Sat Feb 23, 2008 7:50 am
by frozen
Thanks alot Ever.

The way I was trying to do it was well wrong then hehe. I was trying to number it with MSQL_NUM and trying to use if to get it to write out the first 5 then else to write out the next 5.

Thanks again