jamesnaat Posted September 2, 2012 Share Posted September 2, 2012 I have a web app. When admin logs in there is a dashboard showing a summary of various items. The one area I am having trouble with is the "Customer Recently Modified" section of this index page. I add a customer - no issues. When I modify a customer's info and go back to dashboard their information shows up twice under "Recently Modified" section of table. ANY help would be appreciated please see screen shot below). Here is the related code: Admin Index (Shows summary of various items) Relevant section is shown below (I have limited the number of items shown on the index page to 5) <?php foreach(Customer::getRecentlyModified(5) as $customer): ?> <tr> <td> <?php echo $customer->getFirstName(); ?> <?php echo $customer->getLastName(); ?> <small>- <?php echo $customer->getEmail(); ?></small><br /> <?php echo $customer->getCompany(); ?> </td> <td> <?php if($customer->getModifyDate()->format('Y-m-d H:i:s') !== '-0001-11-30 00:00:00') echo ago(strtotime($customer->getModifyDate()->format('Y-m-d H:i:s'))); else echo ago(strtotime($customer->getCreateDate()->format('Y-m-d H:i:s'))); ?> </td> </tr> <?php endforeach; ?> Functions to code above public static function getRecentlyCreated($count=5) { $database = Database::getInstance(); $customers = array(); $result = $database->executeQuery('SELECT * FROM customer ORDER BY CreateDate DESC LIMIT ' . $count); if(!is_null($result)) { foreach($result as $record) { $customers[] = Customer::get($record['ID']); } } return $customers; } public static function getRecentlyModified($count=5) { $database = Database::getInstance(); $customers = array(); $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer" . " UNION SELECT ID, ModifyDate AS Date FROM customer" . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count; $result = $database->executeQuery($sql); if(!is_null($result)) { foreach($result as $record) { $customers[] = Customer::get($record['ID']); } } return $customers; } Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 2, 2012 Share Posted September 2, 2012 $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer" . " UNION SELECT ID, ModifyDate AS Date FROM customer" . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count; Why are you doing that? Quote Link to comment Share on other sites More sharing options...
jamesnaat Posted September 2, 2012 Author Share Posted September 2, 2012 $sql = "SELECT ID, Date FROM (SELECT ID, CreateDate AS Date FROM customer" . " UNION SELECT ID, ModifyDate AS Date FROM customer" . " WHERE ModifyDate NOT LIKE '0000-00-00 00:00:00') modification ORDER BY Date DESC LIMIT ". $count; Why are you doing that? Why am I doing which part?? Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 2, 2012 Share Posted September 2, 2012 The part I posted. Quote Link to comment Share on other sites More sharing options...
jamesnaat Posted September 2, 2012 Author Share Posted September 2, 2012 I was simply trying to pull out queries that had those parameters. I wanted to pull queries then sort them by time and date. I'm sorry if I'm not completeky understanding the question. This is the only way I could get my query to work. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 2, 2012 Share Posted September 2, 2012 Why are you doing a union? Do you know what that does? Hint: you're seeing the results of it. Quote Link to comment Share on other sites More sharing options...
scootstah Posted September 2, 2012 Share Posted September 2, 2012 I'm curious of your thought process on why you decided to use UNION. UNION is used to combine the results from multiple SELECT's into one result set. So, since you are basically running two SELECT's against the same table with the same criteria, you are getting duplicate results. UNION is absolutely unnecessary for this. Quote Link to comment Share on other sites More sharing options...
jamesnaat Posted September 2, 2012 Author Share Posted September 2, 2012 Both of the above responses are absolutely correct!!! Thank you both!!!! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted September 2, 2012 Share Posted September 2, 2012 Now that you've gotten the code sorted out, it's a perfect time to mark the thread as "Solved". PS: Also, please use the tags around your code, as it helps make both your post and your code a lot easier to read. Thanks. Quote Link to comment 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.