Jump to content

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()); 

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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