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 {
..............................
........................
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;
}
}
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==0)
{
$totalNotredirect=$totalNotredirect+1;
$data['totalNotredirect']=$totalNotredirect;
}else{
$totalredirected=$totalredirected+1;
$data['totalredirect']=$totalredirected;
}
}
}else{
$previous=$data['date'];
$redirected=$part_red['redirectedStatus'];
if ($redirected==0)
{
$totalNotredirect=$totalNotredirect+1;
$data['totalNotredirect']=$totalNotredirect;
}else{
$totalredirected=$totalredirected+1;
$data['totalredirect']=$totalredirected;
}
$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, , , . ????
.