Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/163134-solved-php-mysql-help/
Share on other sites

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>';

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.

 

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>';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.