Page 1 of 1

Break the loop if value is encountered

Posted: Wed Apr 21, 2004 11:04 pm
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.

Posted: Wed Apr 21, 2004 11:09 pm
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 :)

Posted: Wed Apr 21, 2004 11:17 pm
by feyd
uhm......

Code: Select all

if($t_value = $row['name']){
== not =

Posted: Thu Apr 22, 2004 3:17 am
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()); 
} 
?>

Posted: Thu Apr 22, 2004 10:01 am
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.

Posted: Tue May 11, 2004 7:21 am
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...