Hey there,
the following is what I get when I perform a typical query on my database:
+--------+----+---+---+----------+
| length | id | l | p | username |
+--------+----+---+---+----------+
| 50 | 12 | 1 | 1 | bibiki |
| 50 | 12 | 1 | 2 | bibiki |
| 50 | 12 | 2 | 1 | bibiki |
| 50 | 12 | 2 | 2 | bibiki |
| 50 | 12 | 3 | 1 | bibiki |
| 50 | 12 | 3 | 2 | bibiki |
| 40 | 13 | 4 | 5 | bibiki |
| 40 | 13 | 4 | 6 | bibiki |
| 40 | 13 | 5 | 5 | bibiki |
| 40 | 13 | 5 | 6 | bibiki |
+--------+----+---+---+----------+
Note that column id has two different values.
I want my view to look like this:
----------------
id 12
l: 1 2 3
p: 1 2
----------------
id 13
l: 4 5
p: 5 6
----------------
Instead of the above, what I am getting right now is:
------------------
id 12
l: 11 22 33(here I do not want these output twice)
.....and so on
//$mainarray contains the table I put above.
//a print_r on $mainArray looks like this:
/*
Array
(
[0] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 1
[p] => 1
[username] => bibiki
)
)
[1] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 1
[p] => 2
[username] => bibiki
)
)
[2] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 2
[p] => 1
[username] => bibiki
)
)
[3] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 2
[p] => 2
[username] => bibiki
)
)
[4] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 3
[p] => 1
[username] => bibiki
)
)
[5] => Array
(
[12] => Array
(
[date] =>
[length] => 50
[id] => 12
[l] => 3
[p] => 2
[username] => bibiki
)
)
[6] => Array
(
[13] => Array
(
[date] =>
[length] => 40
[id] => 13
[l] => 4
[p] => 5
[username] => bibiki
)
)
[7] => Array
(
[13] => Array
(
[date] =>
[length] => 40
[id] => 13
[l] => 4
[p] => 6
[username] => bibiki
)
)
[8] => Array
(
[13] => Array
(
[date] =>
[length] => 40
[id] => 13
[l] => 5
[p] => 5
[username] => bibiki
)
)
[9] => Array
(
[13] => Array
(
[date] =>
[length] => 40
[id] => 13
[l] => 5
[p] => 6
[username] => bibiki
)
)
)
*/
$differentIDs = array();
foreach($mainArray as $row)
{
foreach($row as $k=>$v)
{
$differentIDs[] = $k;
}
}
$activityIDs = array_unique($activityIDs);
//print_r($activityIDs);//this now only contains 12 and 13
echo "<br />";
echo "<br />";
foreach($differentIDs as $q=>$qq)
{
foreach($mainArray as $k=>$row)
{
/*
echo $k;
print_r($row);
echo "<br />";
*/
foreach($row as $r=>$v)
{
if($r == $qq)
{
echo $v['l'];
}
}
}
echo "<br />";
}
print_r($mainArray);
}
Now, as code might indicate, I put column on a separate id, and the iterate on each id. so I have two iterations here.
Then, inside this array I iterate on the main array. Again, I iterate on each array inside the main array. if the array I am iterating in is on an index equal to id, i output the value at index l inside this array.
I could just build an array for index l for each id, and then make sure duplicate l's are removed and output the l's... but I am afraid that is not the best way to do this. I am looking for a better way. if anyone has an idea I would greatly appreciate it.