unistake Posted October 12, 2010 Share Posted October 12, 2010 Hi all, I have a simple html form, when the values are posted to the php page I want to be able to bring up particular products based on specific details. However if the user does not enter a value, I want the script below to ignore that particular field and show all the results not taking in to consideration that field. Hope you understand. I was thinking maybe I could use something like if $_POST == "" IGNORE!! but I can not find an example on the internet. The script I have made so far is below. <?php $sql = "SELECT * FROM sales WHERE category='$_POST[category]' AND manufacturer='$_POST[manufacturer]' AND model='$_POST[model]' AND purchasetype='$_POST[type]'"; // IF VALUE OF SAY MODEL IS LEFT BLANK - I WANT THE PHP TO INGORE THE TYPE OF MODEL AND SHOW THE RESULTS JUST INCLUDING THE OTHER FIELDS. $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $num = mysqli_num_rows($result); if ($num >0) // Login Name found { while($row = mysqli_fetch_assoc($result)) { ?> Quote Link to comment Share on other sites More sharing options...
micah1701 Posted October 12, 2010 Share Posted October 12, 2010 wrapp all your search code in this: if($_POST['category'] != ""){ // all your code here } then it will only run your code if the post "category" value is NOT blank Quote Link to comment Share on other sites More sharing options...
unistake Posted October 12, 2010 Author Share Posted October 12, 2010 Hi micah1701, thanks for your reply. Any of those post fields above could be empty, is there like a universal code I could use to wrap the code in. Thanks Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 12, 2010 Share Posted October 12, 2010 You're going at it backwards. Start with just the basic SELECT . . . FROM . . . WHERE query, and for each form field that does have a value entered, concatenate it to the query string. Make sense? Quote Link to comment Share on other sites More sharing options...
micah1701 Posted October 12, 2010 Share Posted October 12, 2010 <?php $columns = array("category","manufacturer","model","type"); $where = ""; foreach($columns as $col){ if($_POST[$col] != ""){ $where.= "$col = '$_POST[$col]' AND "; } $where = $rtrim($where, " AND"); //trim off that last "AND" $sql = "SELECT * FROM sales $where"; ?> Quote Link to comment Share on other sites More sharing options...
unistake Posted October 12, 2010 Author Share Posted October 12, 2010 ok thanks for both your help. I will have a fiddle around - its a new area of php I have not yet grasped with arrays. Will come back if I cant get it to work Quote Link to comment Share on other sites More sharing options...
unistake Posted October 14, 2010 Author Share Posted October 14, 2010 I have been trying to get this script working for a couple of days and the php is not showing any output. The full code I have is: Any help would be greatly appreciated <?php $cxn = mysqli_connect($host, $user, $passwd,$dbname) or die ("Unable to connect!"); $columns = array("category","manufacturer","model","purchasetype"); $where = ""; foreach($columns as $col){ if($_POST[$col] != ""){ $where.= "$col = '$_POST[$col]' AND "; } $where = $rtrim($where, " AND"); //trims off that last "AND" $sql = "SELECT * FROM sales $where"; $result = mysqli_query($cxn,$sql) or die ("Couldn't execute query"); $num = mysqli_num_rows($result); if ($num >0) // Car sale found { while($row = mysqli_fetch_assoc($result)) { extract($row); echo "$title, $email, £$price"; } } else { echo "no cars could be found!"; } ?> Quote Link to comment Share on other sites More sharing options...
Pawn Posted October 14, 2010 Share Posted October 14, 2010 That code generates a fatal parse error. Set your scripts to report errors during development! You're not closing the foreach loop. foreach($columns as $col){ if($_POST[$col] != ""){ $where.= "$col = '$_POST[$col]' AND "; } } Quote Link to comment Share on other sites More sharing options...
unistake Posted October 15, 2010 Author Share Posted October 15, 2010 I got it to work thanks, I also have this linked problem, the { echo " AND"; } is meant to be included in the variable $model. If purchasetype has a value. <?php if ($_POST[model] !="") { $model = "model='". $_POST['model'] ."'"; if ($_POST['purchasetype'] !="") { echo " AND "; } } else { $model = ""; } ?> Quote Link to comment 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.