CodeIgniter Forums

Full Version: Creating an associative array through database query
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]theknight[/eluser]
Code:
//for each put all the associated with that child into an array
            foreach($details as $items)
            {
            
               // $ItemsList[] = $items['Name'];
                $ItemsList = array($items['Name'] => $items['NumberNeeded'],);
                
            }
            
       //put all the items into a string
            foreach($ItemsList as $list){  
                       $body .= $list."\n";
                                
                    }

Each item has a quantity, I want to associate each item with it's quantity and put it inside of a string, so that I can use that string within a mail function.

How do I do this? I can get this to work for a single list.

Thanks

El Forum

[eluser]ShawnMcCool[/eluser]
If I'm reading this correctly this is more a PHP question than a CodeIgniter question.

Specifically answering your question (as I understand it) you'd do something like this:

$item_quantity_array = array();

foreach($details as $detail)
{
$item_quantity_array[$detail['Name']] = $detail['NumberNeeded'];
}

print_r($item_quantity_array);

foreach($item_quantity_array as $item_name => $item_number_needed)
{
echo "$item_number_needed item(s) of $item_name<br/>";
}

El Forum

[eluser]theknight[/eluser]
Thanks. Exactly what I was looking for.

El Forum

[eluser]JoostV[/eluser]
Why store the data into a second array? Seems to me you don't need to
Code:
$body = '';
foreach($details as $detail) {
    $body .= $detail[‘Name’]] . ' of ' . $detail[‘NumberNeeded’] . "\n";
}

El Forum

[eluser]ShawnMcCool[/eluser]
Yea, I always wonder how to go about responding to this kind of question. Either I could review their code base and rewrite it or I could just answer their question as they phrased it. I figured the former is unlikely.

El Forum

[eluser]JoostV[/eluser]
Yeah, I know whta you mean.

El Forum

[eluser]theknight[/eluser]
Thanks guys.

That is a quicker way to do the same thing.

I guess there are a 1000 ways to skin a cat.

El Forum

[eluser]JoostV[/eluser]
True Smile