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
https://forums.phpfreaks.com/topic/15032-exiting-a-loop/
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
https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60439
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
https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60452
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
https://forums.phpfreaks.com/topic/15032-exiting-a-loop/#findComment-60454
Share on other sites

Archived

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

×
×
  • 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.