Jump to content

Frying my brain with this foreach


c_shelswell

Recommended Posts

I just can't figure an easy way to do this and if anyone can tell me that'd be amazing.

I've got an array of data some of the entries have the same id. i.e.

array[0] [id] => 1 [date] => 11-01-2007 [title] => example1
array[1] [id] => 2 [date] => 12-01-2007 [title] => example2
array[2] [id] => 2 [date] => 12-01-2007 [title] => example3

now i need it to spit all this out but if it's got the same id to put all that stuff in one cell. I need both the titles but the date only once.

so far i've got this:
[code]
foreach ($userAccounts as $key => $value)
{
$ids[] = $value['download_id'];
}

$i=0;
echo "<tr><td class='tblContent'>";
foreach ($userAccounts as $key => $value)
{


if ($ids[$i] == $ids[$i+1])
{
echo $value['title']."&nbsp;-&nbsp;";
}

elseif ($ids[$i] != $ids[$i+1])
{

echo $value['title']."<tr><td class='tblContent'>";
}

$i++;
}
[/code]

Now that kind of works it certainly out puts all the titles with the same id on the same line although i'm having an awful time trying to close my <tr><td> tags with out messing it all up. And i've not figured out yet how to only put the date in once on one line.

If anyone could help i'd be really grateful
Thanks
Link to comment
Share on other sites

it is already one array. It's an output of about (in this case) 5 rows from mysql.

here's a print_r of my data.

[code]
Array ( [0] => Array ( [download_id] => 3 [date] => 2007-01-12 [item_price] => 4.99 [qty] => 1 [title] => Live at Barfly [picture] => pic1.jpg ) [1] => Array ( [download_id] => 3 [date] => 2007-01-12 [item_price] => 25.99 [qty] => 1 [title] => Keyboardo [picture] => bassstation.jpg ) [2] => Array ( [download_id] => 3 [date] => 2007-01-12 [item_price] => 2.99 [qty] => 2 [title] => Purple Pete in Skegness [picture] => pic3.jpg ) [3] => Array ( [download_id] => 2 [date] => 2007-01-12 [item_price] => 24.75 [qty] => 1 [title] => John gets morris dancing [picture] => pic6.jpg ) [4] => Array ( [download_id] => 2 [date] => 2007-01-12 [item_price] => 9.99 [qty] => 1 [title] => Bruce (the man) McKnee [picture] => pic5.jpg ) [5] => Array ( [download_id] => 1 [date] => 2007-01-12 [item_price] => 25.99 [qty] => 1 [title] => Keyboardo [picture] => bassstation.jpg ) )

[/code]
Link to comment
Share on other sites

This:[code]<?php
$userAccounts[0] = array ('id' => 1, date => '11-01-2007', 'title' => example1);
$userAccounts[1] = array ('id' => 2, date => '12-01-2007', 'title' => example2);
$userAccounts[2] = array ('id' => 2, date => '12-01-2007', 'title' => example3);

$currentID = "";
$content = "";

echo "<table>";
foreach ($userAccounts as $values) {

    if ($currentID != $values['id']) {
        if ($content) { echo "<tr><td>".$content."</td></tr>\n"; }
        $currentID = $values['id'];
        $content = $values['date'] . ":";
    }
    $content .= "<br>".$values['title'];
}
echo "<tr><td>".$content."</td></tr>";
echo "</table>";

?>
[/code]

Outputs this:
[code]11-01-2007:
example1
12-01-2007:
example2
example3
[/code]
Link to comment
Share on other sites

The . is used as the concatenation operator(had to get my book out for the spelling!) It is used to append text/variables to another variable. The .= appends the following to the original variable. for instance:

[code]
<?php
$a = 'some text';
$b = ' more text';
$c = $a.$b;
echo $c;//echos some text more text
echo '<br />';
$c.= ' even more text';
echo $c;//echos some text more text even more text
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.