pacchiee Posted June 21, 2009 Share Posted June 21, 2009 I have developed an application in which am going to add sales of mobile phones on a daily basis. Some day there may be 10 mobile phone sold and some days 5 and some days none! When adding sales to the database, am inserting the date along with the model, cost and other details. Now, I want to know what is the highest daily sales done in any given month. I just want the (highest) number of mobile phones sold on any day. For instance, in the month of June, lets consider this to be the database entry: (Date:Model) 2009-06-01:X1 2009-06-02:X1 2009-06-02:X1 2009-06-02:X1 2009-06-03:X1 2009-06-09:X1 2009-06-10:X1 2009-06-10:X1 2009-06-12:X1 2009-06-19:X1 Above, highest number of X1 sold was "3" on 2009-06-02. I want a way to get the number "3". Please help. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/ Share on other sites More sharing options...
wildteen88 Posted June 21, 2009 Share Posted June 21, 2009 One way could be $data = '2009-06-01:X1 2009-06-02:X1 2009-06-02:X1 2009-06-02:X1 2009-06-03:M55 2009-06-09:X1 2009-06-10:X1 2009-06-10:X1 2009-06-12:M55 2009-06-19:X1 2009-06-12:M55 2009-06-12:M55 2009-06-12:M55 2009-06-12:M55'; $rows = explode("\r\n", $data); foreach($rows as $sale) { list($date, $model) = explode(':', $sale); isset($sales[$model][$date]) ? $sales[$model][$date] += 1 : $sales[$model][$date] = 1; } echo '<pre>' . print_r($sales, true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/#findComment-860721 Share on other sites More sharing options...
pacchiee Posted June 21, 2009 Author Share Posted June 21, 2009 Thanks wildteen88. The output looks something like this: Array ( [X1] => Array ( [2009-06-01] => 1 [2009-06-02] => 3 [2009-06-09] => 1 [2009-06-10] => 2 [2009-06-19] => 1 ) [M55] => Array ( [2009-06-03] => 1 [2009-06-12] => 5 ) ) This is very very close to what am actually looking for. I need it in a bit different way. Something like below would be great: X1 = 3 M55 = 5 or those highest sales in a day can be assigned to some variable so that it can be printed on to the page. Quote Link to comment https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/#findComment-860771 Share on other sites More sharing options...
wildteen88 Posted June 21, 2009 Share Posted June 21, 2009 To get to sales for each model you can use function getTopSale(&$saleDates) { $key = array_search(max($saleDates), $saleDates); return array($key, $saleDates[$key]); } foreach($sales as $model => $saleDates) { list($date, $sold) = getTopSale($saleDates); echo '<h2>'.$model.'</h2>'. '<p>Sold '.$sold.' on '.$date.'</p>'; } Quote Link to comment https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/#findComment-860785 Share on other sites More sharing options...
pacchiee Posted June 21, 2009 Author Share Posted June 21, 2009 Hi wildteel88, The output now is: X1 Sold 3 on 2009-06-02 M55 Sold 5 on 2009-06-12 This was exactly what I was looking for. Thank you so much for the help. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/#findComment-860916 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.