Break out of loop if value is encountered?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Break out of loop if value is encountered?

Post by Steveo31 »

I'm inserting a bunch of actors/actresses to a DB by splitting the input with the delimiter "|". Then it inserts to the DB like so:

Code: Select all

<?php

mysql_connect('localhost', 'root');
mysql_select_db('people');

$actors = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows";

$indiv = explode("|", $actors);
foreach($indiv as $key=>$value){
   echo $key.':  '.$value.'<br />';

   $get = "SELECT name FROM people_table";
   $get_query = mysql_query($get);
#######
### Not working:
#######
			while($row = mysql_fetch_assoc($get_query)){
			    if($value == $row['name']){
			       break;
			    }
			}
## end "not working"
			$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')";
			$query = mysql_query($sql) or die(mysql_error());

?>
So yea.. the "while" part is what isn't working. Basically I need it to stop the loop, thus not inserting the name if it is found in the database.

:?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

change

Code: Select all

$indiv = explode("|", $actors);
to

Code: Select all

$indiv = explode(" | ", $actors);
should take care of it.
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

That... or should I trim() them?

Thanks for the reply though.
Post Reply