Jump to content

Recommended Posts

Hello guys.

 

I have a mysql structure like this

 

nickname server_id

1 1

1 2

1 3

1 4

1 5

2 1

2 2

2 3

 

How do I echo it automatically merged into the rows into one nickname if there is more than one server_id assigned to a nickname, something like this is what I need:

 

1 - 1 2 3 4 5

2 - 1 2 3

 

My current code:

<?php
$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['nickname']. " - ". $row['server_id'];
echo "<br />";
}


?>

 

Thank you so much for any responses!

Link to comment
https://forums.phpfreaks.com/topic/174851-echo-merged-data-into-one-row/
Share on other sites

something like this

$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id"; 
$result = mysql_query($query) or die(mysql_error());
$oldnick="";
while($row = mysql_fetch_array($result)){
echo ($oldnick!=$row['nickname'])?"<br>".$row['nickname']:$row['server_id']." "; 
$oldnick=$row['nickname']; 
}

Thank you so much for the response but I have a certain problem, it doesn't echo the server_id if the person only has 1 server_id assigned to the nickname

 

$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id"; 
$result = mysql_query($query) or die(mysql_error());
$oldnick="";
while($row = mysql_fetch_array($result)){
echo ($oldnick!=$row['nickname'])?"<br>".$row['nickname']:$row['server_id']." "; 
$oldnick=$row['nickname']; 
}

 

current code if needed, thanks again :D

Nevermind, I got another code to do something else, here it is

 

$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id
   GROUP BY a.id"; 
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<tr><td>".$row['nickname']."</td>";
if($row['server_id'] == "1"){
echo "<td><img src='http://xxx.net/yes.gif' alt=''></td>";
} else {
echo "<td><img src='http://xxx.net/mo.gif' alt=''></td>";
}
echo "</tr>";

}

 

I am making so that if the nickname has a server_id with a "1" assigned to it, it will be displayed with an image, but the problem is, is that it shows to only a certain amount of nicknames, how i fix?

 

Thank you.

Now you get to use GROUP BY  8)

<?php
$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id
   GROUP BY a.nickname";
$result = mysql_query($query) or die(mysql_error());
   
while($row = mysql_fetch_array($result)){
echo "<tr><td>".$row['nickname']."</td>";
if($row['server_id'] == "1"){
   echo "<td><img src='http://xxx.net/yes.gif' alt=''></td>";
   } else {
   echo "<td><img src='http://xxx.net/mo.gif' alt=''></td>";
   }
echo "</tr>";
   
}?>

OMG this is so frustrating, to keep things simple now, is there a way to turn the nicknames into an array?

 

for example

 

nickname server_id

1 1

1 2

1 3

1 4

1 5

2 1

2 2

2 3

 

into

 

nickname server_id

1 1|2|3|4|5

2 1|2|3

 

 

thank you guys if you reply.

Since I know mysql can do it, there is no need making a messy loop.

 

Like I said, this is a sql question, but I giving a try till someone better comes along.

 

$query = "SELECT GROUP_CONCAT(nickname ORDER BY id DESC) AS `nickname`, 
GROUP_CONCAT(server_id ORDER BY id DESC) AS `server_id` 
FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c 
WHERE c.admin_id = a.id 
AND c.server_id = s.id 
AND a.id = c.admin_id";

 

but I may be off still  :shrug:

you first query would do that

$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$nicks[$row['nickname']][]=$row['server_id']; 
}

then

foreach($nicks as $nickname=>$serverids)
{
echo $nickname." ".implode(" ",$serverids); 
}

you first query would do that

$query = "
SELECT admin_id, server_id, nickname, hostname
  FROM amx_amxadmins a, amx_serverinfo s, amx_admins_servers c
WHERE c.admin_id = a.id
   AND c.server_id = s.id
   AND a.id = c.admin_id"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$nicks[$row['nickname']][]=$row['server_id']; 
}

then

foreach($nicks as $nickname=>$serverids)
{
echo $nickname." ".implode(" ",$serverids); 
}

 

HOLY JESUS.

 

It worked! Thank you soo much!!

 

I've been looking into my laptop screen for 4 hours thinking how to solve my problem, and you came and solved it. Thank you and to the others in the thread!

 

Is there also a way to make so that the nicknames that have the most server_id's assigned to them get to be on the list first? Thank you again!

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.