Break the 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 the loop if value is encountered

Post by Steveo31 »

A few things suggested here didn't work, so I'll throw the problem back in here.

The user can input names of people, and PHP will split and trim based on the | delimiter. From there, I would like to insert the names into a database ONLY if it isn't there. Here's what I have, and isn't breaking if found:

Code: Select all

<?php

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

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

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

foreach($indiv as $key=>$value){
			$t_value = trim($value);
   echo $key.':  '.$t_value.' '.'<br />';

   $get = "SELECT name FROM people_table";
   $get_query = mysql_query($get);

	while($row = mysql_fetch_assoc($get_query)){
	    if($t_value = $row['name']){
	        echo 'found...';
	        break;
	    }
	}

$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')";
$query = mysql_query($sql) or die(mysql_error());
}
?>
It will echo 'found' it seems, at random, so I am obviously doing something wrong here.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

$found = FALSE;
while($row = mysql_fetch_assoc($get_query) && $found == FALSE){ 
       if($t_value = $row['name']){
           $found = TRUE;
           echo 'found...'; 
           break; 
       } 
   }
Try that :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uhm......

Code: Select all

if($t_value = $row['name']){
== not =
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

There are a couple of errors in you code .... ($t_value = $row['name'] ) ... this will assign $row['name'] to $t_value which will almost always evaluate to true... your break will only break out of the while loop, which will then execute the insert statement.

try this

Code: Select all

<?php 

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

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

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

foreach($indiv as $key=>$value){ 
   $t_value = trim($value); 
   echo $key.':  '.$t_value.' '.'<br />'; 

   $get = "SELECT name FROM people_table"; 
   $get_query = mysql_query($get); 

   while($row = mysql_fetch_assoc($get_query)){ 
       if($t_value == $row['name']){ 
           echo 'found...'; 
           continue 2; 
       } 
   } 

$sql = "INSERT INTO `people_table` (`name`) VALUES ('$value')"; 
$query = mysql_query($sql) or die(mysql_error()); 
} 
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Well, I'm going to throw my code in here too:

Code: Select all

$conn = mysql_connect('localhost','root','root_pass');
$db = mysql_select_db($conn,'people');
$actor_list = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows | Jack Black"; 

$actor_array = explode("|", $actor_list);
foreach($actor_array as $actor)
{
   $query = <<<SQL
INSERT INTO
  name
  (name)
VALUES
  ('$actor')
SQL;
   $result = @mysql_query($query);
   if(mysql_error() == '1062')
   {
     echo "$actor was already found, and was not added.";
    }
}
This will work if the name is the primary key.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Post by litebearer »

my $.02

Code: Select all

<?php 

// define the array
$actors = "John Wayne | Bruce Lee | Steven Segall | Tim Meadows | Jack Black"; 
$indiv = explode("|", $actors); 
//count the fields
$num_name = count($indiv);

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

$i = 0;
for ($i = 0; $i < $num_name; $i ++) &#123;
  $sql = "SELECT * FROM people_tbl WHERE strtoupper(name) = strtoupper($indiv&#1111;$i])";
  $result1 = mysql_query($sql);
  if (!$result1) &#123;
     // put your name was NOT found code here
  &#125; else&#123;
    // put your name was found code here
  &#125;
&#125;
 
?>
Lite...
Post Reply