Jump to content

[SOLVED] will my array search the database yes no?


redarrow

Recommended Posts

I made this all up and want to no would it be possable to use this kind of

search with the arrary values?

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

  if($_POST['submit']){

   $key=($_POST['key']);

    for ($i=0; $i<count($key); $i++){

       $query="select * from users where name like "%'".key[0]."'%" and age like "%'".key[1]."'%" ";
      
      $result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


  echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}
   }
     }

$x=array("name","age");


  echo"<form action='test_array.php' method='POST'>";

   foreach($x as $key){

     echo"<input type='text' name='key[]' >$key<br>";
    
      }

     echo"<br>";

         echo"<input type='submit' name='submit' value='Search'>";

            echo"</form>";

?>

please give a textual example ok cheers.

 

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

  if($_POST['submit']){

   $key=($_POST['key']);

    for ($i=0; $i<count($key); $i++){

$query="select * from users where name like '%$key[0]%' and age like '%$key[1]%' ";
      
      $result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


  echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}
   }
     }

$x=array("name","age");


  echo"<form action='test_array.php' method='POST'>";

   foreach($x as $key){

     echo"<input type='text' name='key[]' >$key<br>";
    
      }

     echo"<br>";

         echo"<input type='submit' name='submit' value='Search'>";

            echo"</form>";

?>

this is the correct way becouse you dont have the data or querys in the loop but cheers?

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

if($_POST['submit']){

$key=($_POST['key']);

for($i=0; $i<count($key); $i++){
 
$a=$key[0];
 
$b=$key[1];

}

$query="select * from users where name like '%$a%' and age like '%$b%' ";
      
$result=mysql_query($query);

while($x=mysql_fetch_assoc($result)){

echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
   
}

}
   
$p=array("name","age");

echo"<form action='test_array.php' method='POST'>";

foreach($p as $key){

echo"<input type='text' name='key[]' >$key<br>";
    
}

echo"<br>";

echo"<input type='submit' name='submit' value='Search'>";

echo"</form>";

?>

i got this now but becouse i am assigning the array key singlely is that why i dont need a loop?

 

 

Ok i understand but brushing up so any help is good for coding cheers.

 

<?php

$db=mysql_connect("localhost","username","password");

mysql_select_db("my_database",$db);

if($_POST['submit']){

$key=($_POST['a']);
$key=($_POST['b']);

$a=$key[0];
$b=$key[1];

$query="select * from users where name like '%$a%' and age like '%$b%' ";
     
$result=mysql_query($query);


while($x=mysql_fetch_assoc($result)){


echo " Username: ".$x['name']."  Age: ".$rec['age']." ";
  }
}

$x=array("name","age");

echo"<form action='test_array.php' method='POST'>";

foreach($x as $key){

echo"<input type='text' name='key[]' >$key<br>";
   
}

echo"<br>";

echo"<input type='submit' name='submit' value='Search'>";

echo"</form>";

?>

i got this now but becouse i am assigning the array key singlely is that why i dont need a loop?

 

Yes.

 

But this code won't work. The assignment of $_POST['b'] to $key overwrites the previous assignment. The second 2 lines of code won't work because $key isn't an array.

<?php
$key=($_POST['a']);
$key=($_POST['b']);

$a=$key[0];
$b=$key[1];
?>

 

You would need this (note the "[]")

 

<?php
$key[]=($_POST['a']);
$key[]=($_POST['b']);

$a=$key[0];
$b=$key[1];
?>

 

but you might just as well do

 

<?php
$a=($_POST['a']);
$b=($_POST['b']);
?>

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.