cursed Posted September 10, 2009 Share Posted September 10, 2009 Hello all! Apologies for having a newb question, but the following script only outputs one ID , when it should output multiple IDs <? $userfile= file_get_contents("file.txt"); $urls = explode("\n",$userfile); foreach ($urls as $url) { $result = mysql_query("SELECT * FROM url WHERE website='$url'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo $row['id']; } } ?> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/ Share on other sites More sharing options...
kratsg Posted September 10, 2009 Share Posted September 10, 2009 Maybe the file only has one row... as a nicer way to do this, might I suggest the following code? <?php $userfile= file("file.txt",FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($userfile as $line) { $line = mysql_real_escape_string($line);//it might be nice to escape it :-) if this is a file you control, don't need to, but if it's stuff other users add in, might be helpful for preventing against sql injection $result = mysql_query("SELECT * FROM url WHERE website='$line'") or die(mysql_error()); while($row = mysql_fetch_array( $result )) { echo $row['id']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915777 Share on other sites More sharing options...
cursed Posted September 10, 2009 Author Share Posted September 10, 2009 Ah, now it works. I think it was because I had echo (something invalid); echo $row['id']; Thanks for helping me out! I really appreciate it. edit:Right now, i'm at a complete loss. while($row = mysql_fetch_array( $result )) { echo $row['id']; } Then I added: echo "<iframe src=\""; above the echo $row['id']; It only outputs one ID. (I know I should concatenate the string, but this makes it easier for me to see) If I add echo "a"; above echo $row['id']; it works just fine. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915788 Share on other sites More sharing options...
cursed Posted September 10, 2009 Author Share Posted September 10, 2009 Oh, I also tried this: // other code above ?> <iframe src=" <? // website url echo $row['id']; ?> "> <? //continue code I know it looks ugly, but even this code isn't working. Thoughts, anyone? Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915800 Share on other sites More sharing options...
chmpdog Posted September 10, 2009 Share Posted September 10, 2009 you could also do it by setting the id as a var: $id = $row['id']; so now you can do this <iframe src="<? echo $id; ?>"> That way it doesnt look so confusing, and you isolate as much of the php code as possible Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915809 Share on other sites More sharing options...
cursed Posted September 10, 2009 Author Share Posted September 10, 2009 Hi, I just did that. while($row = mysql_fetch_array( $result )) { $id = $row['id'] ?> <iframe src="<?=$id?>"> <? } } ?> Still same problem :[ Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915813 Share on other sites More sharing options...
kratsg Posted September 10, 2009 Share Posted September 10, 2009 Doesn't anyone know html? Iframes have closing tags. If you look at the code, I'm sure you'll see all the ids inside there... close your iframe :-o Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915833 Share on other sites More sharing options...
cursed Posted September 10, 2009 Author Share Posted September 10, 2009 Hey kratsg, thanks for the response. Here is the completely code outputted from the script is this: Connected to MySQL<br />Connected to Database<iframe src="9"> Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915882 Share on other sites More sharing options...
kratsg Posted September 10, 2009 Share Posted September 10, 2009 It's because you need to close your iframe with </iframe> <iframe src="<?=$id?>"> <? Think of the open iframe tag like a textarea. Since it isn't being echoed out through PHP but as normal HTML, it's a vacuum for all the code beneath it. In effect, stopping your script short. Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-915891 Share on other sites More sharing options...
cursed Posted September 11, 2009 Author Share Posted September 11, 2009 Gee, I feel stupid. I thought php didn't 'care' about html, so it would output <iframe src="1"> <iframe src="2"> <iframe src="3"> Thanks again for helping me out again! Quote Link to comment https://forums.phpfreaks.com/topic/173728-solved-foreach-and-while-inside-each-other-doesnt-work/#findComment-916454 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.