Jump to content

Archived

This topic is now archived and is closed to further replies.

gladiator83x

Exiting a Loop

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
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 variable
mysql_query("insert into `End_Review` (`id`,`topic_title`,`date`) values ('','$title','$test_date')");
}
?>
[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.