redarrow Posted March 10, 2007 Share Posted March 10, 2007 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/ Share on other sites More sharing options...
genericnumber1 Posted March 10, 2007 Share Posted March 10, 2007 yes, but your syntax is wrong $query="select * from users where name like "%'".key[0]."'%" and age like "%'".key[1]."'%" "; probably should be $query="select * from users where name like '%$key[0]%' and age like '%$key[1]%'"; Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-203994 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-203998 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204003 Share on other sites More sharing options...
Barand Posted March 10, 2007 Share Posted March 10, 2007 Can you explain why this bit is "correct" ? for($i=0; $i<count($key); $i++){ $a=$key[0]; $b=$key[1]; } Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204009 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 becouse i have assign the varable a to name and b to age and therefore name and age will search within the query from the array form. am i wrong ? dont no? Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204011 Share on other sites More sharing options...
Barand Posted March 10, 2007 Share Posted March 10, 2007 But why the the for() loop. All it does is make the same assignments several times. $a=$key[0]; $b=$key[1]; is all you need Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204015 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 i thort you needed to use a loop to get the array from the key[] but you dont? Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204016 Share on other sites More sharing options...
Barand Posted March 10, 2007 Share Posted March 10, 2007 You don't use the loop to extract the key, you just use it to repeat the code. All it does is $a=$key[0]; $b=$key[1]; $a=$key[0]; $b=$key[1]; Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204017 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204019 Share on other sites More sharing options...
Barand Posted March 10, 2007 Share Posted March 10, 2007 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']); ?> Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204022 Share on other sites More sharing options...
redarrow Posted March 10, 2007 Author Share Posted March 10, 2007 Thank you barand for you grate help cheers m8. Link to comment https://forums.phpfreaks.com/topic/42063-solved-will-my-array-search-the-database-yes-no/#findComment-204048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.