1bigbear Posted November 2, 2009 Share Posted November 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/ Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 Im not quite sure what you mean. Can you expain a little better? what does it do to search the data base? what are you auto incrementing? some code examples (even pseudo code) would help Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949530 Share on other sites More sharing options...
1bigbear Posted November 2, 2009 Author Share Posted November 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949540 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 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 } } Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949541 Share on other sites More sharing options...
1bigbear Posted November 2, 2009 Author Share Posted November 2, 2009 what is $each? what is it do? does it take take a value from the array? and how to make it take the value of $name[0][1]? Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949546 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949549 Share on other sites More sharing options...
1bigbear Posted November 2, 2009 Author Share Posted November 2, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949556 Share on other sites More sharing options...
mikesta707 Posted November 2, 2009 Share Posted November 2, 2009 nope, as long $name is an array. Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949572 Share on other sites More sharing options...
1bigbear Posted November 2, 2009 Author Share Posted November 2, 2009 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()); } Quote Link to comment https://forums.phpfreaks.com/topic/179990-using-if-to-auto-increment/#findComment-949629 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.