Jump to content

[SOLVED] If(condition){and};


differ8

Recommended Posts

hey Guys this has been driving me nuts for days, im hoping someone can give me a hand with this code.....whats wrong with it...its obviously something with my If(condition){and}

 

is there a better way to do this??

 

code is as follows:

 

 

<?php 

 

$tileCode = $_POST["tileCode"];

$tileCategory = $_POST["tileCategory"];

$tileFactory = $_POST["tileFactory"];

$tileFinish = $_POST["tileFinish"];

$tileColour = $_POST["tileColour"];

$tileSize = $_POST["tileSize"];

 

$mysqli = new mysqli("localhost", "username", "password", "database");

$Tiles = "select *

From tiles

where tileCode = tileCode

 

if ($tileCode == "")

{and tileCode like '%$tileCode%';}

 

if ($tileCategory == "")

{and tileCategory = '$tileCategory';}

";

 

?>

Link to comment
https://forums.phpfreaks.com/topic/114067-solved-ifconditionand/
Share on other sites

Well basically i have a form with Select boxes , when the form is submitted it produces this page. Now the problem i have is that my Select boxes have an "any" position and if the any is selected i want it to ignore the "and" part of the sql but if its not i want it to do it? do u know what i mean??

 

if you have messenger could you please add me so i can speak about this a bit better and send u the files

 

EDIT: messenger details removed.

you are not making a legitimate sql query... your sql is literally:

 

"select *

From tiles

where tileCode = tileCode

 

if ($tileCode == "")

{and tileCode like '%$tileCode%';}

 

if ($tileCategory == "")

{and tileCategory = '$tileCategory';}

"

 

what you need is something like:

 

<?php
$tileCode = "select *
From tiles
where tileCode = tileCode ";

## ADDING AND CONDITIONS TO THE QUERY
if ($tileCode == "")
{$tileCode .= " and tileCode like '%$tileCode%'";}

if ($tileCategory == "")
{$tileCode .= " and tileCategory = '$tileCategory'";}

echo $tileCode;
?>

 

that should output a better query... then after you verify it outputs properly just execure it: mysql_query($tileCode)

yup - basically use your if's to BUILD your query, not to RUN it.

 

just append stuff to your $tileCode variable using .= within your if's and then execute it after all your conditionals. you can't intermingle the if's the way you did within a string, your code was actually being put INTO the string i believe. and as always, just echo your $tileCode variable before you start executing it to see if it looks like a valid SQL statement.

differ8:

He isn't being rude this is a public forum for people to work php problems out on if you want your problem solved then use the forum.

 

Also use the forums correctly put your code inside [ code] brackets.

 

thorpe is right you have your syntax all messed up.

 

<?php 

  $tileCode = $_POST["tileCode"];
  $tileCategory = $_POST["tileCategory"];
  $tileFactory = $_POST["tileFactory"];
  $tileFinish = $_POST["tileFinish"];
  $tileColour = $_POST["tileColour"];
  $tileSize = $_POST["tileSize"];

  $mysqli = new mysqli("localhost", "username", "password", "database");
  $Tiles = "SELECT * FROM `tiles` WHERE `tileCode` = '$tileCode' ";

if ($tileCode == "")
{$Tiles .= "AND `tileCode` LIKE '%$tileCode%'";}

if ($tileCategory == "")
{$Tiles .= "AND `tileCategory` = '$tileCategory'";}

?>

 

Dang got to it first... lol. But I want to get my first point across also you should use complete SQL syntax

well guess what buddy, ur not real usefull on here either...so wake up to urself hey and dont be so rude, i was just asking for help

 

No, you were asking for personal 1 on 1 live support. If it's offered... nice, but don't ask that of volunteers who come here to help you out of their own free time, and then call them a jerk for saying no.

 

slushpuppie

 

<?php
$tileCode = "select *
From tiles
where tileCode = tileCode ";

# Below will never get executed, due to it being defined above
if ($tileCode == "")
{$tileCode .= " and tileCode like '%$tileCode%'";}
?>

 

differ8

 

You should look up MySQL syntax... it differs quite a bit from PHP. Usually the if/thens are best left to PHP.

that outputs

 

select * From tiles where tileCode = tileCode and tileCode like '%select * From tiles where tileCode = tileCode %' and tileCategory = 'Any'

 

 

this is all of my code:

 

<?php  

$tileCode = $_POST["tileCode"];
$tileCategory = $_POST["tileCategory"];
$tileFactory = $_POST["tileFactory"];
$tileFinish = $_POST["tileFinish"];
$tileColour = $_POST["tileColour"];
$tileSize = $_POST["tileSize"];


$categoryCondition = 'if($tileCategory != "")and tileCategory = $tileCategory;';

$mysqli = new mysqli("localhost", "bradytiles", "oo7bro", "bradytiles"); 
$tileCode = "select *
From tiles
where tileCode = tileCode ";

## ADDING AND CONDITIONS TO THE QUERY
if ($tileCode != "")
{$tileCode .= " and tileCode like '%$tileCode%'";}

if ($tileCategory != "")
{$tileCode .= " and tileCategory = '$tileCategory'";}

echo $tileCode;	
?>




<html>
<head>
<title>Personal INFO</title>
</head>
<body>

<table border ="1">
<?php if ($result = $mysqli->query($Tiles)) {
	while ($row = $result->fetch_row()) { echo "<tr><td>".$row[0]."</td>"."<td>".$row[1]."</td>"."<td>".$row[2]."</td>"."<td>".$row[3]."</td>"."<td>".$row[4]."</td>"."<td>".$row[5]."</td>"."<td>".$row[6]."</td></tr>";}

	if (mysqli_num_rows($result) == 0) {
    echo "Sorry your search did not display and results!<br>Please click <a href='searchform.php'>Here</a> to return to the search page!";

}
	} ?>
</table>

im not dealing directly with the client, im just the sucker that has to fix the problem!!! but anyway its a good chance to learn php! im doin ok  i think for only 2 days into it! i got the solution for anyone who is interested, the final code is posted below, Sorry i snapped  im just frustrated and wasnt getting anywhere! thanks for eveyrones help.

 

<?php  

$tileCode = $_POST["tileCode"];
$tileCategory = $_POST["tileCategory"];
$tileFactory = $_POST["tileFactory"];
$tileFinish = $_POST["tileFinish"];
$tileColour = $_POST["tileColour"];
$tileSize = $_POST["tileSize"];




$mysqli = new mysqli("localhost", "username", "password", "database"); 
$Tiles = "SELECT * FROM tiles WHERE tileCode = tileCode ";

if ($tileCode != "")
{$Tiles .= "AND tileCode LIKE '%$tileCode%'";}

if ($tileCategory != "Any")
{$Tiles .= "AND tileCategory = '$tileCategory'";}

if ($tileFactory != "Any")
{$Tiles .= "AND tileFactory = '$tileFactory'";}


if ($tileFinish != "Any")
{$Tiles .= "AND tileFinish = '$tileFinish'";}



if ($tileColour != "Any")
{$Tiles .= "AND tileColour = '$tileColour'";}

if ($tileSize != "Any")
{$Tiles .= "AND tileSize = '$tileSize'";}


## echo $Tiles;	
?>




<html>
<head>
<title>Personal INFO</title>
</head>
<body>

<table border ="1">
<?php if ($result = $mysqli->query($Tiles)) {
	while ($row = $result->fetch_row()) { echo "<tr><td>".$row[0]."</td>"."<td>".$row[1]."</td>"."<td>".$row[2]."</td>"."<td>".$row[3]."</td>"."<td>".$row[4]."</td>"."<td>".$row[5]."</td>"."<td>".$row[6]."</td></tr>";}

	if (mysqli_num_rows($result) == 0) {
    echo "Sorry your search did not display and results!<br>Please click <a href='searchform.php'>Here</a> to return to the search page!";

}
	} ?>
</table>

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.