Colleen78 Posted July 31, 2006 Share Posted July 31, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/ Share on other sites More sharing options...
.josh Posted July 31, 2006 Share Posted July 31, 2006 $sql = 'SELECT COUNT( * ) AS `Rows` , `item_id` FROM `tbl_item` GROUP BY `item_id` ORDER BY `item_id` DESC LIMIT 3' Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66223 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 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 Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66229 Share on other sites More sharing options...
bpops Posted July 31, 2006 Share Posted July 31, 2006 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 onewhile ($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. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66237 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66637 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66676 Share on other sites More sharing options...
bpops Posted July 31, 2006 Share Posted July 31, 2006 Glad you got it working!Can you please explain a little more the linking problem you're now having? Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66677 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 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/c45I don't know what to put in to get that. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66694 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 well don't you already have the image src??$SSL_URL/item_images/$thumbnail_image Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66696 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 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? Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66701 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 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.. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66702 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66706 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 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 Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66711 Share on other sites More sharing options...
Colleen78 Posted July 31, 2006 Author Share Posted July 31, 2006 Ok, the only problem now is underscores...The link needs to be like this:http://www.domain.com/item/Dragon_Hatchling_Figurine/217/c45and right now I have it doing this:http://www.domain.com/item/Dragon Hatchling Figurine/217/c45Any idea how to get the underscores in? Thanks again! Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66715 Share on other sites More sharing options...
pixy Posted July 31, 2006 Share Posted July 31, 2006 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. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66717 Share on other sites More sharing options...
bpops Posted July 31, 2006 Share Posted July 31, 2006 further info:http://us2.php.net/str_replaceexample:[code]$item_name = str_replace($item_name, " ", "_");[/code]I think that might work.. Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66722 Share on other sites More sharing options...
ryanlwh Posted July 31, 2006 Share Posted July 31, 2006 bpops, wrong order of arguments :)[code]$item_name = str_replace(" ", "_",$item_name);[/code] Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66724 Share on other sites More sharing options...
bpops Posted July 31, 2006 Share Posted July 31, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66730 Share on other sites More sharing options...
Colleen78 Posted August 1, 2006 Author Share Posted August 1, 2006 Ok, that's working now! Thanks so much! What do I owe you guys?! :-X ;D Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66771 Share on other sites More sharing options...
bpops Posted August 1, 2006 Share Posted August 1, 2006 After becoming an expert, come back and help someone else.. I think that's how this site's supposed to work :P Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66776 Share on other sites More sharing options...
ryanlwh Posted August 1, 2006 Share Posted August 1, 2006 and learn from each other. don't ever laugh at noobs, because we all were noobs at some point :) Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66778 Share on other sites More sharing options...
Colleen78 Posted August 1, 2006 Author Share Posted August 1, 2006 That is excellent advice guys! I will definitely do that, it's nice to help out. :) Link to comment https://forums.phpfreaks.com/topic/16072-need-help-with-pulling-last-3-entries-from-database-with-php/#findComment-66783 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.