Siggles Posted February 21, 2008 Share Posted February 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/ Share on other sites More sharing options...
Daniel0 Posted February 21, 2008 Share Posted February 21, 2008 You can do something like this: $var = 'u_378:u_384:u_135:u_358'; $parts = explode(':', $var); Quote Link to comment https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/#findComment-472834 Share on other sites More sharing options...
Siggles Posted February 21, 2008 Author Share Posted February 21, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/#findComment-473025 Share on other sites More sharing options...
Daniel0 Posted February 21, 2008 Share Posted February 21, 2008 You can do this: <?php $var = 'u_2:u_358:u_135:u_378'; $var = str_replace('u_', '', $var); $var = str_replace(':', ',', $var; $query = "SELECT username FROM phpbb3_users WHERE user_id IN({$var})"; // etc... ?> Quote Link to comment https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/#findComment-473033 Share on other sites More sharing options...
Siggles Posted February 21, 2008 Author Share Posted February 21, 2008 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/92289-splitting-i-think-a-table-field/#findComment-473101 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.