Jump to content

PHP/Mysql - AND/OR help


herghost

Recommended Posts

Evening everybody!

 

What I am trying to do is display infomation from a database, each item has a  catid and a subid.

 

I can display the correct information when I am looking at a particular subid by using:

 

$catid = $_GET['catid'];
if(isset($_GET['subid'])){
$subid = $_GET['subid'];
}
else $subid = '';?>

//other code


$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid'");

 

But I cant figure a way to change this so if the AND is not met it will still display the info based on $catid?

 

Basically I want to display all the records with a particular catid, regardles of subid, but only if subid does not exits?!

 

Does this make sense?

 

Basically I think I am after and AND/OR

 

I hope you can see what I am on about!

 

Cheers

 

 

Link to comment
Share on other sites

$catid = $_GET['catid'];
if(isset($_GET['subid'])){
$subid = $_GET['subid'];
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid'");
}
else { 
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'");
} ?>

 

Couldn't you just do something like that?

Link to comment
Share on other sites

Hi MatthewJ,

 

Thanks for your reply, I have tried the following

 

if(isset($subid)){
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid'");
echo mysql_error() ;
}

else{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'");
echo mysql_error() ;
}





while($row = mysql_fetch_assoc($query)){

$_SESSION['name'] = $row['name'];
$_SESSION['short'] = $row['short'];
$_SESSION['per'] = $row['percent'];
$_SESSION['m_id'] = $row['m_id'];


echo $_SESSION['name'];

}

 

However nothing displays for the second query which is meant to show everything with a catid :(

 

It does still show when both elements are their

Link to comment
Share on other sites

$query = mysql_query("SELECT * FROM merchants WHERE subid = '$subid' AND (catid = '$catid' OR catid = "")"); - I did not test this, but I think this is what you are trying to do:

 

Find all results where $subid and $catid have a match BUT also include $catids with no value (null)?

Link to comment
Share on other sites

Back to square one with that one, displays when both are present but nothing when just catid is present

 

Can you please rephrase what you are trying to do?

 

Do you want to show all results regardless if either field is empty?

 

OR

 

What is the exact output you expect? Please clarify.

Link to comment
Share on other sites

Ok,

 

Basically say I have 3 items in my database, all with a catid of 1, 2 of the items have a subid of 2 and the remaing has a subid of 3

 

Basically I then have a menu tree of one top cat and 2 sub cats, when I click on the top cat I wnat to display all three results, or when I click on a sub cat then just the results for that subcat will appear.

 

All this is contained on one page. in my real world application this is called view.php.

 

So if you are viewing the top cat then the url is view.php?catid=2 and when using a sub cat its view.php?catid=2&subid=3

 

Does that make more sense?

 

Thanks for all your help thus far

Link to comment
Share on other sites

Well I had a think and came up with this:

 

$catid = $_GET['catid'];
if(isset($_GET['subid'])){
$subid = $_GET['subid'];
}
else $subid = '0';

if($subid > 0)
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid' AND subid= '$subid'");
echo mysql_error() ;
}

else
{
$query = mysql_query("SELECT * FROM merchants WHERE catid= '$catid'");
echo mysql_error() ;

}

 

Which does the trick nicely :)

 

 

 

 

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.