Jump to content

Exiting a Loop


gladiator83x

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());}
            }
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.