Jump to content


Photo

Exiting a Loop


  • Please log in to reply
6 replies to this topic

#1 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 19 July 2006 - 01:03 PM

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());}
            }

#2 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 19 July 2006 - 01:10 PM

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:

while (condition_is_true) {
   // Do all this
}


#3 chrisprse

chrisprse
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationUnited Kingdom

Posted 19 July 2006 - 01:11 PM

My first thought would be to delete what you have posted and try:

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')");
}
}


#4 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 19 July 2006 - 01:12 PM

Hehe :)

#5 gladiator83x

gladiator83x
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 19 July 2006 - 01:42 PM

My first thought would be to delete what you have posted and try:

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')");
}
}


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.

#6 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 19 July 2006 - 01:46 PM

Just reading back on what your problem was, i would suggest that you dont need a loop.
<?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 variable
mysql_query("insert into `End_Review` (`id`,`topic_title`,`date`) values ('','$title','$test_date')");
}
?>


#7 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 19 July 2006 - 01:46 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users