Jump to content

Splitting (I think) a table field.


Siggles

Recommended Posts

I have a piece of code which takes the result of a column from a dbase and cuts off the first bit of text revealing the userid and then it matches the userid with the username and displays the username. Such as...

 

In the dbase - the to_address column data is

u_378

 

$userto=$row[to_address];

$userto2=str_replace("u_","","$userto");

 

and then there is a bit of script that macthes the resulting varaible to the username column. That all workfs fine. But what if there is more than one username listed in the dbase like follows...

 

u_378:u_384:u_135:u_358

 

Is there a way I can get all those seperate numbers and then display them using my current script..(all shown below)

 

$userto=$row[to_address];

  $userto2=str_replace("u_","","$userto");

 

   echo "<td valign=\"top\" width=\"10%\">$row[username]</td>";

   

   $results = mysql_query("SELECT username FROM phpbb3_users WHERE user_id = $userto2 ");

  while($row2 = mysql_fetch_array($results)){

   echo "<td valign=\"top\">$row2[username]</td>";

   }

 

I'm thinking I would need to use maybe 'split' and then a 'foreach'. I can;t get the syntax right!!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/
Share on other sites

Still need help here.

 

So the database column to_address normally has...

u_358

 

And I use

str_replace("u_","","$userto");
to get the numbers from it. I then use

 

$results = mysql_query("SELECT username FROM phpbb3_users WHERE user_id = $userto2");

  while($row2 = mysql_fetch_array($results)){

  echo "<td valign=\"top\">$row2[username]</td>";

  }

 

to get the username that corresponds to the userid.

 

Now I need to take info like this....

 

u_2:u_358:u_135:u_378

 

split it somehow and print each seperate username stilll using the SELECT statement somehow.

 

So instead of Max it shows Max, John, Craig

 

Any ideas?

 

 

Thanks, I finally got it to look how I wanted...

 

 echo "<tr>";
$var=$row[to_address];
$var = str_replace('u_', '', $var);
$var = str_replace(':', ',', $var);

echo "<td valign=\"top\" width=\"10%\">$row[username]</td><td valign=\"top\">";
$results = mysql_query("SELECT username FROM phpbb3_users WHERE user_id IN ($var)");
while($row2 = mysql_fetch_array($results)){
echo "$row2[username] ";
}
echo "</td>";

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.