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
https://forums.phpfreaks.com/topic/215714-if-value-0-ignore/
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
https://forums.phpfreaks.com/topic/215714-if-value-0-ignore/#findComment-1121528
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
https://forums.phpfreaks.com/topic/215714-if-value-0-ignore/#findComment-1122086
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
https://forums.phpfreaks.com/topic/215714-if-value-0-ignore/#findComment-1122409
Share on other sites

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.