Page 1 of 1

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

Posted: Fri Nov 12, 2004 2:26 pm
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.

Posted: Fri Nov 12, 2004 2:49 pm
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

Posted: Sat Nov 13, 2004 2:07 am
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.

Posted: Sat Nov 13, 2004 2:40 am
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;

Posted: Sat Nov 13, 2004 3:07 am
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!

Posted: Sat Nov 13, 2004 3:12 am
by timvw

Code: Select all

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

print_r($array);

Posted: Sat Nov 13, 2004 3:53 am
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.

Posted: Sat Nov 13, 2004 6:11 am
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

Posted: Mon Jan 03, 2005 12:47 am
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.