Jump to content

Listing info in row once


Mutley

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/43659-listing-info-in-row-once/
Share on other sites

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.

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.

<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.

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!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.