Query output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frozen
Forum Newbie
Posts: 8
Joined: Fri Feb 22, 2008 11:43 pm

Query output

Post 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
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Query output

Post 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>";
 
}
 
.
frozen
Forum Newbie
Posts: 8
Joined: Fri Feb 22, 2008 11:43 pm

Re: Query output

Post 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
Post Reply