So i am trying to take a while loop and add values to an array. In the end I want to display the Item, #number of each Item and the Total amount for each item.

Example


$count_array = array();
// This is what i have working. Now I need to add the amount that this item cost
array_push($count_array, trim($item));


// I currently take out the item name and how many total for each item. 
foreach (array_count_values($count_array) as $k => $v) {
    echo "<tr><td align='center'>";
	echo "$v";
	echo "</td><td colspan='5'>&nbsp;&nbsp;";
	echo "$k";
	echo "</td></tr>";
	//echo "\$a[$k] => $v.\n";
}


// I need to add this to the array above so I know how much each item costed so i can add it up in the end to figure out total cost. 
$item_row_total = $row['invoice_dtl_item_retail'] * $row['invoice_dtl_quantity'];

My data now looks like this

4 Announcements
1 Student
5 Store
1 Supply
17 Voting

What i want to do is have it say
4 Announcements $400
1 Student $50

The dollar amount would be the cost of each item. So Announcments in this example is $100 each adding to $400 and Student is $50 each adding to $50

    where is the value of each item coming from? can you print out the value of $item for us?
    in essence, to get the total costs of you obviously need to data points, the number of sales for each item and the value of each individual items, so you'll need to prep that data.
    if your $item's contain that information like so

    /// raw data prepared for summing
    $items = array(
    array('annoucement'=>100),
    array('student'=>50),
    array('annoucement'=>100),
    array('annoucement'=>100),
    array('annoucement'=>100),
    );
    /// where we store the info
    $sums = array(); 
    
    /// go thru each item in the data, organizing by type and adding counts/sums
    foreach($items as $item) {
         list($title,$value) = $item; 
         if (empty($sums[$title])) { /// if first iteration, set initial values
            $sums[$title]['value'] = $value;
            $sums[$title]['count'] = 1; 
          }
         else { /// else add to initial values; 
             $sums[$title]['value'] += $value; 
             $sums[$title]['count']++; 
         } 
    }
    
    ///finally display your results
    foreach($sums as $item => $sum) {
        echo $sum['count'].' '.$item.' '.$sum['value']; 
    }
    

      add a new function which returns value for a supplied name

      foreach (array_count_values($count_array) as $k => $v) { 
      $cost=get_cost($k)    * $v ;
      echo "<tr><td align='center'>";
          echo "$v";
          echo "</td><td colspan='5'>&nbsp;&nbsp;";
          echo "$k";
      echo "</td><td>$cost";
          echo "</td></tr>"; 
      ...
      ...
      function get_cost($item_name){
      return <find price for the passed item> 
      }
      
        Write a Reply...