Mutley Posted March 21, 2007 Share Posted March 21, 2007 I have multiple rows for the same "id", but when I come to display it in a table, it loops the 'id' rows 2 or 3 times, what I want to do is loop each "id" once, so: id:|obja:|objb: 1 | 1/2 | 12/15 2 | 5/6 | 20/30 Instead of: id:|obja:|objb: 1 | 1 | 12 1 | 2 | 15 2 | 5 | 20 2 | 6 | 30 ... But also pull information out of the rows fields for each 'id', is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/ Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 You could do something like this: <?php $result = mysql_query("SELECT * FROM `table`"); $array = array(); while($row = mysql_fetch_assoc($result)) { if(array_key_exists($row['id'], $array)) { $array[$row['id']]['obja'] .= "/".$row['obja']; $array[$row['id']]['objb'] .= "/".$row['objb']; } else $array[$row['id']] = array("obja" => $row['obja'], "objb" => $row['objb']); } foreach ($array as $id => $content) echo $id.", ".$content['obja'].", ".$content['objb']."<br>"; ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-211943 Share on other sites More sharing options...
Mutley Posted March 21, 2007 Author Share Posted March 21, 2007 Could you help explain what each part does please? I'm not familiar with arrays. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-211947 Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 This will go over the data and every time check if a row with the same id was in the past or not (using array_key_exists()): If it was- add the data to the previous one/s. If it wasn't- create the new "row" in the array. Then it prints the values (the foreach part). Orio. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-211950 Share on other sites More sharing options...
Mutley Posted March 21, 2007 Author Share Posted March 21, 2007 I've got it working thanks. If I wanted to make a new query using one of the variables from that array, how do I do it? Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-211971 Share on other sites More sharing options...
Orio Posted March 21, 2007 Share Posted March 21, 2007 The array's structure is like this: Array ( [1] => Array ( [obja] => Some-Value [objb] => Some-Value ) [2] => Array ( [obja] => Value1/Value2 [objb] => Val1/Val2 ) ..... ) In this case id=1 had one match, id=2 had two matches. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-211974 Share on other sites More sharing options...
Mutley Posted March 21, 2007 Author Share Posted March 21, 2007 <table width="80%" border="1" cellspacing="0" cellpadding="3"> <tr> <td>id:</td> <td>obja:</td> <td>objb:</td> <td>objc:</td> </tr><tr> <? $result = mysql_query("SELECT * FROM `table`"); $array = array(); while($row = mysql_fetch_assoc($result)) { if(array_key_exists($row['id'], $array)) { $array[$row['id']]['obj1'] .= "/".$row['obj1']; $array[$row['obj2']]['obj2'] .= "/".$row['obj2']; $array[$row['obj3']]['obj3'] .= "/".$row['obj3']; } else $array[$row['id']] = array("obj1" => $row['obj1'], "obj2" => $row['obj2'], "obj3" => $row['obj3']); } foreach ($array as $id => $content) echo "<tr><td>".$id."</td><td>".$content['obj1']."</td><td>".$content['obj2']."</td><td>".$content['obj3']."</td><br>"; ?> </tr> </table> As you see above I'm trying to get each part into columns of a table but with "obj2" it has 2 different numbers which I want in 2 different sections, rather than "5/6" it has those numbers in table columns. I've confused myself now, lol. So if there are lots of '1' in obja, it only displays one, not 1/1/1/1/1. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-212015 Share on other sites More sharing options...
Mutley Posted March 21, 2007 Author Share Posted March 21, 2007 Still need to know how to do this with more control over the variables. ??? Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-212401 Share on other sites More sharing options...
Mutley Posted March 22, 2007 Author Share Posted March 22, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-212828 Share on other sites More sharing options...
Orio Posted March 22, 2007 Share Posted March 22, 2007 I didn't understand what you are trying to do. Do you want to show each number only once? Examples: 1/1/2/1 will become- 1/2 1/2/3/1 will become- 1/2/3 etc' Am I right? Orio. Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-212831 Share on other sites More sharing options...
Mutley Posted March 22, 2007 Author Share Posted March 22, 2007 Yep, here is my problem in more detail... <table width="80%" border="1" cellspacing="0" cellpadding="3"> <tr> <td>ID:</td> <td>User in Position 1:</td> <td>User in Position 2:</td> That's what I want the headings to be, now you've helped fix the problem of the ID repeating. In the database is this: id | position | user 1 | 1 | 7 1 | 2 | 4 2 | 2 | 3 2 | 1 | 2 I would like it to show like this on my webpage: ID | User in Position 1 | User in Position 2 1 | 7 | 4 2 | 3 | 2 Now I also want to join (which I tried using an INNER JOIN from your array but the variables don't work ) another table to get the usernames with their user_ids, so it will finally look like this: ID | User in Position 1 | User in Position 2 1 | bob | mike 2 | fred | tim I hope that helps. At first my database was: ID | user_pos_1 | user_pos_2 ...but it causes problems earlier having it like that (harder to join tables). Thanks a lot Orio, your help is really appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/#findComment-212878 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.