suttercain Posted April 10, 2007 Share Posted April 10, 2007 Good evening fellow insomniacs. I am echoing a mysql database result which is stored in the database as such "Superman, Batman, Wonder Woman, etc" I am using the explode to split the into an array using the comma as the delimiter. Then I am trying to get it so if I click on Superman I am taken to the Superman profile page... Batman I go to the Batman profile page. This is the code I am using: if ($row['characters'] == !NULL) { $people = explode (", ", $row['characters']); echo "<b>CHARACTER(S):</b><br><font color='#263B5A'>" ; sort ($people); foreach ($people as $key=> $val) { $rez = mysql_query ("SELECT name, character_id FROM characters"); while ($row = mysql_fetch_assoc($rez)){ echo "<a href='view_characters.php?id={$row['character_id']}'>" . $val . "</a><br>"; } } echo "</font><br><br>"; } It echos out the following: Batman <---Link to id 1 //Not Correct Batman <---Link to id 2 // Correct Batman <---Link to id 3 //Not Correct (No link to 3 should even appear) Superman <---Link to id 1 // Correct Superman <---Link to id 2 //Not Correct Superman <---Link to id 3 //Not Correct (No link to 3 should even appear) which is partually correct... but I want it to echo each only 1 time: Batman Superman (not three times each) It feels like I am so close but I can't get it. Any advice or suggestions? Thanks Link to comment https://forums.phpfreaks.com/topic/46384-while-loop-in-a-foreach-loop-semi-results/ Share on other sites More sharing options...
btherl Posted April 10, 2007 Share Posted April 10, 2007 Try this $val_escaped = mysql_real_escape_string($val); $rez = mysql_query ("SELECT name, character_id FROM characters WHERE name = '$val_escaped'") or die(mysql_error()); The die() is just to make sure there's no errors in your query. You could do it with a single query as well, which will be more complex but also more efficient. Link to comment https://forums.phpfreaks.com/topic/46384-while-loop-in-a-foreach-loop-semi-results/#findComment-225602 Share on other sites More sharing options...
suttercain Posted April 10, 2007 Author Share Posted April 10, 2007 Awesome that worked at got me the results. For my own edu-mac-tion... How did that work it out? Thanks! I really appreciate your time. Link to comment https://forums.phpfreaks.com/topic/46384-while-loop-in-a-foreach-loop-semi-results/#findComment-225604 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.