Jump to content

if value = 0 ignore


unistake

Recommended Posts

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)) {
?>

Link to comment
Share on other sites

<?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";

?>

Link to comment
Share on other sites

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  :D

<?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!"; }
?>

 

Link to comment
Share on other sites

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 ";
    }   
}

Link to comment
Share on other sites

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 = ""; }
?>

Link to comment
Share on other sites

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.