c_shelswell Posted January 12, 2007 Share Posted January 12, 2007 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] => example3now 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']." - "; } 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 gratefulThanks Quote Link to comment Share on other sites More sharing options...
effigy Posted January 12, 2007 Share Posted January 12, 2007 How about combining the arrays first, instead of trying to juggle two things at once: the output and the combination? Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted January 12, 2007 Author Share Posted January 12, 2007 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] Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 12, 2007 Share Posted January 12, 2007 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:example2example3 [/code] Quote Link to comment Share on other sites More sharing options...
c_shelswell Posted January 12, 2007 Author Share Posted January 12, 2007 Thats fantastic thanks so much. One question though, i'm quite new to php.What does the .= part mean? On this line $content .= "<br>".$values['title'];just trying to understand how your code works.Thanks again. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 12, 2007 Share Posted January 12, 2007 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 textecho '<br />';$c.= ' even more text';echo $c;//echos some text more text even more text?>[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted January 12, 2007 Share Posted January 12, 2007 "$a .= $b" a shorthand form of "$a = $a . $b".Works with math operators too$a += 5 <==> $a = $a + 5$a *= 5 <==> $a = $a * 5etc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.