boogle1234 Posted March 25, 2011 Share Posted March 25, 2011 Newbie here, I am actually a math teacher working on creating a simulated store front for my students where they keep up with inventory, budgeting etc. Everyday my students will have to look at how much product is in stock, how much product was sold, and then input how much should be ordered for the next day. I am working on an answer key that will be used to validate students input but the 'quantity' and 'sold' fields will be relative to the most recent post. Right now the code below pulls all the rows under 'quantity', but I need it to only pull the most recent post. I have done my best to search the forum and try to find how to do this, but admittedly I am not even really sure what to be searching for. Any help would be great. <?php $con = mysql_connect("localhost","user","pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("myDatabase", $con); $result = mysql_query("SELECT * FROM myTable"); while($row = mysql_fetch_array($result)) { $quantity = $row['quantity']; $sold = $row['numbersold']; $needed = $row['remaining']; echo $needed-($quantity-$sold); } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/231707-pulling-data-from-most-recent-table-entry/ Share on other sites More sharing options...
KevinM1 Posted March 25, 2011 Share Posted March 25, 2011 What does your db table look like? Also, please place your code in tags. Quote Link to comment https://forums.phpfreaks.com/topic/231707-pulling-data-from-most-recent-table-entry/#findComment-1192233 Share on other sites More sharing options...
boogle1234 Posted March 25, 2011 Author Share Posted March 25, 2011 Well I will be doing this for several tables, but the one I am working on right now only has 3 fields. I have setup a field that automatically places the current time stamp, a field where the student lists the product they are ordering, and a field where the student inputs the amount they are ordering. TimeStamp | Product | Amount | I have been trying to fit into my code for it to sort out the max(Timestamp) and then limit 1. Not sure how the ordering of a time stamp day by day, or week by week would work though... Also not sure if that is even the way to go about it, just all I have been able to find. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/231707-pulling-data-from-most-recent-table-entry/#findComment-1192235 Share on other sites More sharing options...
KevinM1 Posted March 25, 2011 Share Posted March 25, 2011 You may be able to break the timestamp down into its component parts, and then order the returning data according to those parts. There's a whole bunch of date/time related functions you can use: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html I'm just unsure if they're all compatible with a timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/231707-pulling-data-from-most-recent-table-entry/#findComment-1192249 Share on other sites More sharing options...
boogle1234 Posted March 25, 2011 Author Share Posted March 25, 2011 Thanks, i will work with it for a while. Instead of sorting max I am just ordering by the timestamp and applying desc. Then limiting 0,1 and pulling the relevant fields. Quote Link to comment https://forums.phpfreaks.com/topic/231707-pulling-data-from-most-recent-table-entry/#findComment-1192268 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.