Welcome Guest, Not a member yet? Register   Sign In
Creating an associative array through database query
#1

[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
#2

[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/>";
}
#3

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

[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";
}
#5

[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.
#6

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

[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.
#8

[eluser]JoostV[/eluser]
True Smile




Theme © iAndrew 2016 - Forum software by © MyBB