acctman Posted August 30, 2007 Share Posted August 30, 2007 I have a While statement below that echos the results from my DB but i also need the mp3_id number it grabs from the DB to use in my javascript below that. how can I get it to output $en['mp3_id'] after the While process has ended? <? $mp3user = mysql_query("SELECT `mp3_id`,`mp3_userid`,`mp3_title` FROM `rate_mp3s` WHERE `mp3_userid` = '".$en['m_id']."'"); echo "<ul style=\"margin-left: 210px;\">\n"; $counter = 1; while($en = mysql_fetch_array($mp3user)) { echo "<li>MP3 " .$counter. " : <span id=\"mp3title-".$counter."\">".$en['mp3_title']. "</span> | <a href=\"process_mp3.php?user=" .$en['mp3_userid']. "&mp3=" .$en['mp3_id']. "\">Delete</a></li>\n"; $counter++; } echo "</ul>\n"; echo "\n"; echo "<script type=\"text/javascript\">\n"; echo "\n"; // This section needs to retrieve mp3_id from $en = mysql_fetch_array($mp3user) echo "new Ajax.InPlaceEditor('mp3title-1', '/process_mp3.php?mp3=" .$en['mp3_id']. "');\n"; echo "new Ajax.InPlaceEditor('mp3title-2', '/process_mp3.php?mp3=" .$en['mp3_id']. "');\n"; echo "new Ajax.InPlaceEditor('mp3title-3', '/process_mp3.php?mp3=" .$en['mp3_id']. "');\n"; echo "\n"; echo "</script>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/ Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 Save them into an array. while($en = mysql_fetch_array($mp3user)) { $mp3IDs[] = $en['mp3_id']; echo "<li>MP3 " .$counter. " : <span id=\"mp3title-".$counter."\">".$en['mp3_title']. "</span> | <a href=\"process_mp3.php?user=" .$en['mp3_userid']. "&mp3=" .$en['mp3_id']. "\">Delete</a></li>\n"; $counter++; } ... foreach($mp3IDs AS $mp3ID){ echo "new Ajax.InPlaceEditor('mp3title-1', '/process_mp3.php?mp3=" .$en['mp3_id']. "');\n"; } etc. Or you could make a big string of the Ajax code in the while loop and print it later. Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-337735 Share on other sites More sharing options...
acctman Posted August 30, 2007 Author Share Posted August 30, 2007 Save them into an array. while($en = mysql_fetch_array($mp3user)) { $mp3IDs[] = $en['mp3_id']; echo "<li>MP3 " .$counter. " : <span id=\"mp3title-".$counter."\">".$en['mp3_title']. "</span> | <a href=\"process_mp3.php?user=" .$en['mp3_userid']. "&mp3=" .$en['mp3_id']. "\">Delete</a></li>\n"; $counter++; } ... foreach($mp3IDs AS $mp3ID){ echo "new Ajax.InPlaceEditor('mp3title-1', '/process_mp3.php?mp3=" .$en['mp3_id']. "');\n"; } etc. Or you could make a big string of the Ajax code in the while loop and print it later. $en['mp3_id'] is blank when echo'ing on the Ajax line at the bottom Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338057 Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 Sorry, that should have been: foreach($mp3IDs AS $mp3ID){ echo "new Ajax.InPlaceEditor('mp3title-1', '/process_mp3.php?mp3=".$mp3ID."');\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338058 Share on other sites More sharing options...
acctman Posted August 30, 2007 Author Share Posted August 30, 2007 jesirose thanks for all the help one more small fix since the Ajax line is not being looped... the mp3title-1,2, and 3 needs to be created. I tried using the counter like I did above but that didn't work. foreach($mp3IDs AS $mp3ID){ $counter = 1; echo "new Ajax.InPlaceEditor('mp3title-".$counter."', '/process_mp3.php?mp3=".$mp3ID."');\n"; $counter++; } Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338070 Share on other sites More sharing options...
darkfreaks Posted August 30, 2007 Share Posted August 30, 2007 Try adding <?php while($counter<=2) ?> before your ajax echo Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338073 Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 You set $counter = 1 in each loop. Use: foreach($mp3IDs AS $counter=>$mp3ID){ echo "new Ajax.InPlaceEditor('mp3title-".$counter."', '/process_mp3.php?mp3=".$mp3ID."');\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338075 Share on other sites More sharing options...
darkfreaks Posted August 30, 2007 Share Posted August 30, 2007 she is correct it will loop only once unless you change the number *nod* Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338077 Share on other sites More sharing options...
Jessica Posted August 30, 2007 Share Posted August 30, 2007 Not quite...it will loop as many times as there are values in the array. However, $counter would always be 1. Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338086 Share on other sites More sharing options...
acctman Posted August 30, 2007 Author Share Posted August 30, 2007 thanks =) coding works perfect Quote Link to comment https://forums.phpfreaks.com/topic/67317-solved-while-statement-question/#findComment-338118 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.