Jump to content

Using if to auto increment


1bigbear

Recommended Posts

I have build a script that is very long and because of this it has problems. The script searches a data base and does something if it finds it. But it has to verify multiple values and I have written the code manually for each one.

 

I want to modify the script so that it is searching a value and them move to the next using auto increment. How to do this?

Link to comment
https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/
Share on other sites

to search it is using a mysql_query,

 

This is how it work:

 

$name1="joh";

$name2="php";

$name3="data";

$name4="value";

//Searching the data base mysql_query returning $databasename for $name1

 

if($name1==$databasename){

 

// nothing

}

else

{ // add to database $name1

}

 

Searching the data base returning $databasename for $name2

if($name2==$databasename){

 

// nothing

}

else

{ // add to database  $name2

 

}

 

and it continues.

 

I want to modify the script so that the script has only one if, something like this:

 

i=1;

 

if($namei==$databasename){

 

// auto increment i

}

else

{ // add to database $namei

}

 

// continue to the next value?

 

 

 

 

 

 

 

 

you would want to make the $name variable into an array, IE

$name = array("john", "doug", "Phil");

 

and then you could use a foreach loop

 

foreach ($name as $each){
if ($each == $databasename){
//do whatever
}
else {
//do whatever else 
}
}

There is a link in my sig that has a beginning tutorial on arrays. If you read that, and then read the next tutorial after it, you should have a much better idea of what to do. But in short, $each is the value that each iteration through the array will have.

 

so if the array is

 

john

charly

david

etc.

 

then the first run of the loop, $each will have "John" second run, it will have "Charly" etc.

so it will work automatically, I just have to give write

 

foreach ($name as $each){

if ($each == $databasename){

//do whatever

}

else {

//do whatever else

}

}

 

and $each will take the next value of the array until it reaches the end?  and I dont have to use auto increment?

I got it working but how to use it in a mysql query

 

$verify="a";
foreach ($value as $each)


if ($each == $verify)
{

print_r ($each);

}

 

But how to use it in the search script, to take each name and search, becasue I have to take a value from the array to compare with the database

 

$crit='client';

switch($crit)
{
case 'client':
$sql="select * from client where client.name like '%".$value."%'";

$rez=mysql_query($sql);

while($rd=mysql_fetch_array($rez))
      {

      $seno=$rd['value'];
      }
        $reswwwult = mysql_query($sql) or die(mysql_error()); 

}

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.