gladiator83x Posted July 19, 2006 Share Posted July 19, 2006 Hi All,I have a variable named $title that holds a title. I also have a table with a column full of titles. I am trying to compare that variable to each cell inside the column of titles, and if there is no match, I wanted to append it to the end only once. However, I am a little stuck because everytime there is no match that variable is appended many times because it is in the loop. Is there any way that I can exit this loop as soon as I do not find a match and append it on only once? This is my code: for($i=0;$i<mysql_num_rows($res);$i++) { $row=mysql_fetch_assoc($res); if ($row[topic_title]!= $title){ echo $test_date; mysql_query("INSERT INTO End_Review (id,topic_title, date) VALUES('','$title','$test_date' ) ") or die(mysql_error());} } Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/ Share on other sites More sharing options...
designationlocutus Posted July 19, 2006 Share Posted July 19, 2006 Change your loop structure to a while loop.It waits for a specific condition before terminating the loop and gives you 0 or more pass throughs:[code]while (condition_is_true) { // Do all this}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60438 Share on other sites More sharing options...
chrisprse Posted July 19, 2006 Share Posted July 19, 2006 My first thought would be to delete what you have posted and try:[code=php:0]while($row = mysql_fetch_array($res)) {if($row['topic_title'] != $title) { echo $test_date; mysql_query("insert into `End_Review` (`id`,`topic_title`,`date`) values ('','$title','$test_date')");}}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60439 Share on other sites More sharing options...
designationlocutus Posted July 19, 2006 Share Posted July 19, 2006 Hehe :) Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60440 Share on other sites More sharing options...
gladiator83x Posted July 19, 2006 Author Share Posted July 19, 2006 [quote author=chrisprse link=topic=101096.msg399777#msg399777 date=1153314709]My first thought would be to delete what you have posted and try:[code=php:0]while($row = mysql_fetch_array($res)) {if($row['topic_title'] != $title) { echo $test_date; mysql_query("insert into `End_Review` (`id`,`topic_title`,`date`) values ('','$title','$test_date')");}}[/code][/quote]I tried that and my table seemed to grow even more. Any other suggestions? I think that I would have to exit the loop somehow. Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60452 Share on other sites More sharing options...
GingerRobot Posted July 19, 2006 Share Posted July 19, 2006 Just reading back on what your problem was, i would suggest that you dont need a loop.[code]<?php$sql = mysql_query("SELECT * FROM `yourtable` WHERE `title`='$title'") or die(mysql_error());$num = mysql_num_rows($sql);if($num == 0){//ie no rows contain a title of that in the variablemysql_query("insert into `End_Review` (`id`,`topic_title`,`date`) values ('','$title','$test_date')");}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60454 Share on other sites More sharing options...
designationlocutus Posted July 19, 2006 Share Posted July 19, 2006 The while as it is set will run until the end of the array. You need your if condition as the while condition to exit early.You will probably need OR in the condition to to check for end of array. Quote Link to comment https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60455 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.