Hello;
I need help accessing and manipulating elements in a multi-dimentional array.
What I have got is two tables. The first table, named "Display," is a storage table for display advertisement images. The second table, called "RunningAds," is the table that has the currently active ads.
When an advertisement is started/activated the id is copied from the Display table to the RunningAds table.
I have figured out how to extract the data from both tables and then put the data into a multi-dimentional array. What I'm having a problem with is how to access and manipulate the elements of the multi-dimentional array.
What I need to do is:
1). Output the dimensions for each advertisement in the Display table.
2). Sum the number of records that are in the Display table for each dimension/size ad (i.e. there are (3) 88 x 31 records, (4) 120 x 60 records, etc.)
3). Sum the number of records that are in the RunningAds table for each dimension/size ad.
// Extract the rows from the Display table
$Result = mysql_query ("SELECT id, Width, Height FROM Display");
while ($Row = mysql_fetch_array ($Result)){
$id = $Row['id'];
// See how many displays are in the RunningAds table
$ResCurrent = mysql_query ("SELECT id FROM RunningAds WHERE id = '$id'");
$NumRows = mysql_num_rows($ResCurrent);
$Dims = $Row['Width']." x ".$Row['Height'];
// There are 58 records in the Display table.
// All 58 records are in the RunningAds table.
// There are 16 different sizes (i.e. Width x Height)
// There are at least two records for each ad size
// in the RunningAds table
if ($NumRows > 0){
$Ad[$Dims][] = 1;
}else{
$Ad[$Dims][] = 0;
}
}
Using print_r for the above code the output looks like this:
Array (
[88 x 31] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[120 x 60] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[120 x 90] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[120 x 240] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 )
[120 x 600] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 )
[125 x 125] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[160 x 600] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[180 x 150] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[234 x 60] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[240 x 400] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[250 x 250] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[300 x 250] => Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 )
[300 x 600] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[336 x 280] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[468 x 60] => Array ( [0] => 1 [1] => 1 [2] => 1 )
[728 x 90] => Array ( [0] => 1 [1] => 1 [2] => 1 ) )
I have had a little bit of success at accessing the array elements. The array_sum function sums the number of ads in the Display table for each size like I need. The second foreach loop outputs whether the ad is in the RunningAds table but it does not sum the number of running ads for each size ad. The snippet below does not output the dimensions for the ads.
foreach ($Ad as $a) {
echo "<p>".array_sum($a)."<br>";
foreach ($a as $b) {
echo $b."<br>";
}
}
// The above snippet outputs something like this:
3
1
1
1
4
1
1
1
9
1
1
1
// The output table needs to look something like this:
(3) 88 x 31 display ads, (3) ads running
(4) 120 x 60 display ads, (3) ads running
(9) 120 x 90 display ads, (3) ads running
... and so forth
I have been working on this problem for about two days and am about skull-numb.
I would appreciate any suggestions. Thanks.