Adding to an associative array in a for loop???

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Adding to an associative array in a for loop???

Post by mairaj »

I am looking to add to an associative array within a for loop. something like...

Code: Select all

for ($i=0; $i < 10; $i++) &#123;
$array = array_push("$itemid => $size");
&#125;

to use in a SQL query. Something like...

Code: Select all

foreach ($array as $key => $val) &#123; 
$query = mysql_query("SELECT * FROM items WHERE ItemID = '$key' && Size = '$val'");
But this doesn't seem to work. I really need help and would be grateful to anybody can do!

Thanks so much!
Mairaj.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

your using array_push wrong

http://php.net/array_push

but you dont even need to use array push in this situation

for loop {
$array[$itemid] = $size;
}


looks like you might have other problems as well. post more of your code
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

Hi! Thanks for your help again rehfeld!

for loop {
$array[$itemid] = $size;
}

This works but here is the problem I am facing now...

Please consider the following scenario:
I am trying to sell a "PC Monitor"... and this comes in 3 different sizes

The entry for this in my database is like:

ItemID: 10
ItemName: Samsung PC Monitor
Size1: Small 15"
Size2: Medium 17"
Size3: Large 21"

Now a user on my website is allowed to choose any OR all of the three sizes using the chekboxes. Say he checked ALL the three sizes and clicked on the button saying -- View Details. Now on the view details page I am using the following code to get the deatils for each size of the product:

Code: Select all

for loop &#123;
$array&#1111;$itemid] = $size;
&#125;

foreach ($array as $key => $val) &#123;
$query = "SELECT * FROM details WHERE ItemID = '$key' && Size = '$val'";
&#125;
and what I am expecting the array to be like is:
$array = array('10 => Small 15" ', '10 => Medium 17" ', '10 => Large 21" ');

But the array seems to be only like:
$array = array('10 => Large 21"');

and when I say

Code: Select all

foreach ($array as $key => $val) &#123;
echo "Key: $key --- Val: $val"
&#125;
It's only showing:

Key: 10 --- Val: Large 21"

Why so like that???

It's working fine with two different products but not with two different sizes. If i choose all the 3 sizes for two products, say "Samsung PC Monitor" and "Acer PC Monitor" then only the 3rd size of both the products is showing up and not all the sizes.

In this case the $array is coming up like:

$array = array('10 = > Large 21" ', '11 = > Large 21" ');
where 11 is the id for "Acer PC Monitor"

Can I get some help with this??

I don't know how clear I am in explaining my problem but if you have any questions please feel free to ask me.

Thanks soooo much!
Mairaj.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Its because your not using the array correctly, at the moment you seem to be over writing the array value each time, which is why you only see 10 => 21 which is the last value that you set for it. What you are trying to do is:

Code: Select all

$array['10'][0] = blah;
$array['10'][1] = rahh;
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

Ok so how should I be doing that? I want the array to be like:

$array = array('10 => size1', '10 => size2', '11 => size1', '11 => size2', '11 => size3');

Any ideas???

Thanks!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$array = array();
$array[10] = array('size1', 'size2');
$array[11] = array('size2', 'size3');

print_r($array);
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

Sorry but this is not clear to me :(

What I want to do is:

If a user selects all the 3 sizes of a single item then it will be considered as 3 different items. Ex.:

Say if a user selected all the 3 sizes for "Samsung PC Monitor" then it will be considered as 3 different items...

Samsung PC Monitor -- Small 15"
Samsung PC Monitor -- Small 17"
Samsung PC Monitor -- Small 21"

and the variable $itemsselected will be set to '3'.

I don't know what the code should be, but I am asuming it to be something like:

Code: Select all

$itemsselected = 3;

For($i=0; $i < $itemsselected; $i++) &#123;
$array = array('$itemid => $size');
&#125;
OR

Code: Select all

$itemsselected = 3;

For($i=0; $i < $itemsselected; $i++) &#123;
$array&#1111;$itemid] = $size;
&#125;
Now here I want this array to be built like:

Code: Select all

$array = array('10 => Small 15', '10 => Medium 17', '10 => Large 21');
so that I can use it like:

Code: Select all

foreach ($array as $key => $val) &#123;
echo "$key -- $val";
&#125;
Where I am expecting it to print:

10 -- Small 15"
10 -- Medium 17"
10 -- Large 21"

Sorry if this is something simple and I am not able to understand. I am a novice in PHP.

Thanks,
Mariaj.
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

Hi

Try the following code. I am not clear as to where you get your data from, so I have made up some test data.

Code: Select all

<?php

$all_items = array();   // Array that holds all selections

// Test data
$items_selected = 3;
$item_id = 10;
$item_size[1] = 'Small 15"';
$item_size[2] = 'Medium 17"';
$item_size[3] = 'Large 21"';
for ($i=0; $i < $items_selected; ++$i)
{
  $all_items[] = array($item_id => $item_size[$i + 1]);
}

// More test data
$items_selected = 2;
$item_id = 11;
$item_size[1] = 'Small 15"';
$item_size[2] = 'Tiny 13"';
for ($i=0; $i < $items_selected; ++$i)
{
  $all_items[] = array($item_id => $item_size[$i + 1]);
}

// Scan through all test data
foreach ($all_items as $master_key => $master_value)
{
  foreach ($master_value as $key => $val)
  {
    // build query
    $query = ' SELECT ' .
              '  * ' .
              ' FROM ' .
              '  details ' .
              ' WHERE ' .
              '  (ItemID = ' . $key . ')' .
              ' AND ' .
              '  (Size = ' . $val . ')';

    echo $query . '<br />';
  }
}
?>
Hope this helps
mairaj
Forum Newbie
Posts: 11
Joined: Mon Sep 20, 2004 2:40 am

Post by mairaj »

Hi all of you guys in here... Thanks for helping me. I know it's too late but i fell ill and couldn't log back in for so long to thank you guys :(

And swdev, thanks for the code. That solved my problem. Worked excellent!!!

Oh BTW A Happy New Year to all!

Cheers.
Post Reply