Jump to content

Extracting data from a query


scronner

Recommended Posts

I've only been studying PHP for a week now and have come across this problem which I'm sure there is a simple answer to, but I just can't figure it out and would appreciate some help. I've spent far too long on this minor issue already!

 

I have a table which contains a list of products, in this case books, which stores the date when each new item is added.

I have a query that then searches through this table and extracts the 6 most recent additions.

 

Here is the code I have so far:

 

$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
		 $title = $row["title"];
		 $author = $row["author"];
		 $price = $row["price"];
		 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
		 $dynamiclist .= //My table showing the products 			
}
} else {
$dynamicList = "There are currently no Books listed in this store";
}

 

This works well when I need to display the 5 most recent products in a normal table fashion, one below another, on the page.

 

However, I want to display the products in a more personalised order. For example display the newest item in one section of the page and 3rd newest in another.

 

What is the best way to select an individual row from a query? To extract the details of the 2nd newest item to display in the header, for example.

 

Thanks for your help.

 

Link to comment
Share on other sites

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.