Array help - storing number of sales

what is the best way to store a number of sales from each staff mon to sat (day of week) in a array?

Example of data:

Monday

Staff One: 4
Staff Two: 4
Staff Three: 2

Tuesday

Staff One: 6
Staff Two: 3
Staff Four: 1

and so to Sat

Is this the best solution: $Day[‘Monday’][‘staff_name’] = 2;
Numner 2 is number of sales from Staff_name on Monday

You’re checking if $Sales[$day] is an array, but are still utlilising it even if it’s not.

$Sales = array();
$SQL = "SELECT * FROM sales WHERE submit_date >= $StartMon AND submit_date <= $EndSat";
$q=mysql_query($SQL) or die(mysql_error());
while($k=mysql_fetch_object($q)){
    $day = date('D',$k->submit_date);
    $agent = $k->username;
    if(!array_key_exists($day, $Sales)){
            $Sales[$day] = array();
        }
    if(!array_key_exists($agent, $Sales[$day])){
        $Sales[$day][$agent] = 1;
    }else{
        $Sales[$day][$agent]++;
    }
}

In that case, like Guido said, what you’re proposing is fine :slight_smile:

I keep getting an error, what I did wrong?

Notice: Undefined index: Tue
Notice: Undefined index: admin
Notice: Undefined index: Wed


		$Sales = array();

		  $SQL = "SELECT * FROM sales WHERE submit_date >= $StartMon AND submit_date <= $EndSat";
		  $q=mysql_query($SQL) or die(mysql_error());
          while($k=mysql_fetch_object($q))
		 {
			  $day = date('D',$k->submit_date);
			  $agent = $k->username;
			  if (is_array($Sales[$day]))
			 {
				  $Sales[$day][$agent]++;
			 }
			 else
			 {
				 $Sales[$day][$agent] = 1;
			 }
		 }

Thanks Jake, That was very helpful.

Not storing.

Just outputting.

What is the best solution to store into array?

I don’t know if this is the best way but this did a trick, any better way let me know please.


     $Day = array('Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun');
     $Sales = array();
	 $username = array();

	   foreach($Day as $EachDay)
		 {
		 $Sales[$EachDay] = array();
		 }

		 $SQL = "SELECT username FROM sales WHERE submit_date >= $StartMon AND submit_date <= $EndSat";
         $q=mysql_query($SQL) or die(mysql_error());
		 while($k=mysql_fetch_object($q)){
			 $username[] = $k->username;
		 }

		foreach($Sales as $key => $value)
		 {
			 foreach($username as $agent)
			 {
			 $Sales[$key][$agent] = array();
			 $Sales[$key][$agent] = 0;
			 }
		 }

I am a bit stuck with outputting from array.

I am getting there but I want to ouput all the staff name from each day even they have not made any sales.

I have tried this:


     $Day = array('Mon', 'Tue', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun');
     $Sales = array();

	 foreach($Day as $EachDay)
		 {
		 $Sales[$EachDay] = array();
		 }

     
	 $SQL = "SELECT * FROM sales WHERE submit_date >= $StartMon AND submit_date <= $EndSat";
     $q=mysql_query($SQL) or die(mysql_error());
      
	  while($k=mysql_fetch_object($q)){
        $day = date('D',$k->submit_date);
        $agent = $k->username;
        if(!array_key_exists($day, $Sales)){
            $Sales[$day] = array();
        }
        if(!array_key_exists($agent, $Sales[$day])){
            $Sales[$day][$agent] = 1;
        } else{
             $Sales[$day][$agent]++;
        }
} 

foreach($Sales as $key => $value)
		 {
	 echo  "<b> $key</b> <br />";
	 foreach($Sales[$key] as $username => $count)
			 {
		 echo "$username - $count <br />";
			 }
		 }

It output like this:

[B]Mon[/B]
[B]Tue[/B]
staff1 - 2
staff2 - 2
[B]Wed[/B]
staff1 - 4
[B]Thur[/B]
[B]Fri
Sat
Sun [/B]

I need to display all the staffs name in all DAY even if they have no sales become 0

Are you planning on storing the data somewhere? If so, how, and where?

I would call the array $sales, but for the rest it looks very readable to me :slight_smile: