Jump to content

Archived

This topic is now archived and is closed to further replies.

Colleen78

Need help with pulling last 3 entries from database with php

Recommended Posts

I have a shopping cart programmed with php, I am making a custom homepage and want to grab the 3 latest items added and post them on the homepage.

I am a beginner so I need the full code to pull it. We don't have a timestamp so the cart company just said to make it pull the three highest ID's.

This is the php code for that table in the database:

[code=php:0]$sql = 'SELECT COUNT( * ) AS `Rows` , `item_id` FROM `tbl_item` GROUP BY `item_id` ORDER BY `item_id` LIMIT 0, 30'[/code]

I'd appreciate any help, thank you.

Share this post


Link to post
Share on other sites
$sql = 'SELECT COUNT( * ) AS `Rows` , `item_id` FROM `tbl_item` GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3'

Share this post


Link to post
Share on other sites
Thanks Crayon! As I stated, I am new to this. I need all the code. I don't know how to put it together to make it work.

:D

Share this post


Link to post
Share on other sites
It would go something like this..

[code]
//Connect to Database
$host="mysql.yourhost.com";
$database = "your_database";
$user = "your_username";
$password = "your_password";
$connection = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection)
or die ("Couldn't select database");

//Specify the query
$query = 'SELECT COUNT( * ) AS `Rows` , `item_id` FROM `tbl_item` GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3';

//Execut the query
$result = mysql_query($query)
      or die ("Error. Unable to perform query.");

//Loop through all the results, displaying each one
while ($row = mysql_fetch_array($result))
  {
    extract($row);
          echo "
          //HTML goes here.. use $value for the values where the value name is just the name of the column in the mysql table. for example:
          // $username: $info  --  Date:  $date
          ";
}

[/code]

Hope that helps.

Share this post


Link to post
Share on other sites
Thanks so much bpops!

Ok, I edited the echo part but it's coming up empty, this is my code.

[code=php:0] echo "<td>\n";
echo "$item_name\n";
echo "<a href=\"$SSL_URL\"><img src=\"$SSL_URL/item_images/$thumbnail_image\" border=\"0\" /></a>\n";
echo "$short_description";
echo "</td>\n";
[/code]

The td's for the table are showing up in the source just empty. I need to call the items name, image and description and link the image to it's page. The variables in the echo's are the row names in the table.

Share this post


Link to post
Share on other sites
I changed the query line to this:

[code=php:0]$query = 'SELECT * FROM `tbl_item` GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3';[/code]


Now it works, I just need to figure out how to link the images to their pages, if anyone can help, that would be great.

Share this post


Link to post
Share on other sites
Glad you got it working!

Can you please explain a little more the linking problem you're now having?

Share this post


Link to post
Share on other sites
Hey!

I don't know how to link at all. I only put this in the link so far, $SSL_URL which gets the domain root, I need it to add on the rest of the items link.

The items link like so:
http://www.domain.com/item/Dragon_Hatchling_Figurine/217/c45

I don't know what to put in to get that.

Share this post


Link to post
Share on other sites
well don't you already have the image src??

$SSL_URL/item_images/$thumbnail_image

Share this post


Link to post
Share on other sites
Yes, and the image displays correctly, does that same code somehow tie into the products page link so I can have the visitor click the thumbnail and be taken to the product page?

Share this post


Link to post
Share on other sites
oh i see what you mean.. sorry i misunderstood you. i think it should be something like
[code]$SSL_URL/item/$item_name/$item_id/[/code]

you'd have to select the item_name from the table. also, i'm not sure what c45 is..

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=102398.msg406850#msg406850 date=1154386131]
oh i see what you mean.. sorry i misunderstood you. i think it should be something like
[code]$SSL_URL/item/$item_name/$item_id/[/code]

you'd have to select the item_name from the table. also, i'm not sure what c45 is..
[/quote]Thanks ryanlwh! You guys are so smart here! :)

I am not the best at explaining, sorry about that. The cart uses search engine friendly urls so hopefully that doesn't make it too hard to figure out how to get it work, I'll try your code and play around with it a bit and report back.

Thanks so much!

edit: I just looked in the DB, c45 is the category id! :D

Share this post


Link to post
Share on other sites
no problem. we're here to help. search engine friendly urls are fine. i'm thinking the last part "c45" is probably a category id

Share this post


Link to post
Share on other sites
Ok, the only problem now is underscores...

The link needs to be like this:
http://www.domain.com/item/Dragon_Hatchling_Figurine/217/c45

and right now I have it doing this:
http://www.domain.com/item/Dragon Hatchling Figurine/217/c45

Any idea how to get the underscores in? Thanks again!

Share this post


Link to post
Share on other sites
Maybe do a str_replace for spaces with an underscore? I don't know if that would work, just a guess. Then when you have it get it from the database you'd need to take them out again and replace them with spaces.

Share this post


Link to post
Share on other sites
further info:
http://us2.php.net/str_replace

example:
[code]
$item_name = str_replace($item_name, " ", "_");[/code]

I think that might work..

Share this post


Link to post
Share on other sites
bpops, wrong order of arguments :)
[code]$item_name = str_replace(" ", "_",$item_name);[/code]

Share this post


Link to post
Share on other sites
[quote author=ryanlwh link=topic=102398.msg406872#msg406872 date=1154387506]
bpops, wrong order of arguments :)
[code]$item_name = str_replace(" ", "_",$item_name);[/code]
[/quote]

ah thanks, im still a bit of a newb myself :P

Share this post


Link to post
Share on other sites
Ok, that's working now! Thanks so much! What do I owe you guys?!  :-X ;D

Share this post


Link to post
Share on other sites
After becoming an expert, come back and help someone else.. I think that's how this site's supposed to work :P

Share this post


Link to post
Share on other sites
and learn from each other. don't ever laugh at noobs, because we all were noobs at some point :)

Share this post


Link to post
Share on other sites
That is excellent advice guys! I will definitely do that, it's nice to help out. :)

Share this post


Link to post
Share on other sites

×

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.