OOP Challenge How to nest using foreach in php mysql result set?

I am having trouble retrieving mysql records. What I'm trying to achieve is to execute a mysql query once or twice twice, save the results in an array, and then scroll through the array using foreach to save the result, as shown below.

There are thousands of such entries in my main database table (partner_redirect):

 id     member     partnerid         Status                       datePost
 1       87887       1                 1                         2014-08-09  
 2       988         3                 0                         2014-08-09
 3       4545        1                 1                         2014-08-09  
 4       8892        5                 0                         2014-08-09 
 5       8891        5                 0                         2014-08-09 
 6       8894        5                 0                         2014-08-09 
 7       889         5                 0                         2014-08-02 
 8       2341        3                 1                         2014-08-09    
 9       8893        1                 0                         2014-08-01 
 10      23224       3                 1                         2014-08-01   
 11      88913       5                 0                         2014-08-03 
 12      2324        3                 0                         2014-08-04   

.................................................. ..................... ............................. ..........................................

Here is my code:

db.class.php

  class Database {
  ..............................
  ........................
  #-#############################################
  # desc: returns all the results (not one row)
  # param: (MySQL query) the query to run on server
  # returns: assoc array of ALL fetched results
  public function fetch_all_array($sql) {
    $query_id = $this->query($sql);
    $out = array();

    while ($row = $this->fetch_array($query_id)) {
        $out[] = $row;
    }

    $this->free_result($query_id);
    return $out;
  }#-#fetch_all_array()


} 

Implementation:

 include_once ('Database.class.php');

  $sql="select id,name from partner ";

$col=$db->fetch_all_array($sql);

 foreach($col as $part)
{
   $id=$part['id'];
   $name=$part['name'];

   $sql ="select partnerid,datePost as date ,Status from  partner_redirect_link where partnerID=".$id;

   $partner_redirect=$db->fetch_all_array($sql);

  foreach($partner_redirect as $part_red)
  {

    $data['id']=$part_red['partnerID'];
    $data['date']=$part_red['date'];
    if(isset($previous) &&($previous!=$part_red['date']))
    {
    while($previous==$part_red['date'])
    {
    $redirected=$part_red['redirectedStatus'];


    #if redirected status =0 then
    if ($redirected==0)
    {
        $totalNotredirect=$totalNotredirect+1;
        $data['totalNotredirect']=$totalNotredirect;
        #$data['totalredirect']=0;
    }else{# if 1
        $totalredirected=$totalredirected+1;
        $data['totalredirect']=$totalredirected;
        #$data['totalredirect']=0;
    }

  }

   }else{
       $previous=$data['date'];

        $redirected=$part_red['redirectedStatus'];


        #if redirected status =0 then
        if ($redirected==0)
        {
            $totalNotredirect=$totalNotredirect+1;
            $data['totalNotredirect']=$totalNotredirect;
            #$data['totalredirect']=0;
        }else{# if 1
            $totalredirected=$totalredirected+1;
            $data['totalredirect']=$totalredirected;
            #$data['totalredirect']=0;
        }

     $data['date']=$previous;
      $date[]=$previous;
    }

   }
}

I am trying to store data, but I do not know how I can match and count every record, storing / comparing each date in an array.

, , , foreach :

 Date          Partner  TotalRecords(per partner)  count(status=1)  count(status=0)     
 2014-08-09    1         2                              2                  0
 2014-08-09    3         2                              1                  1
 2014-08-09    5         3                              0                  3
 2014-08-04    3         1                              0                  1
 2014-08-03    5         1                              0                  1
 2014-08-01    1         1                              0                  1
 2014-08-01    3         1                              1                  1

UPDATE:

, , - . , mysql, , , . ????

.

+4
1

PHP SQL . .

SQL

5 , SQL . , . , , .

select 
    datePost,
    partnerid,
    count(*) as `partner_totalrecords`,
    sum(case when `status` = 0 then 1 else 0 end) `count_status_0`,
    sum(case when `status` = 1 then 1 else 0 end) `count_status_1`
from partner_redirect
group by partnerid;

SO answer . , . .

PHP

. , , .

$rows=$db->fetch_all_array($sql);

foreach($rows as $row) {
    var_dump($row);
}

( MySQLWorkbench)

Resulting table

+3

All Articles