Jump to content

[SOLVED] Organize By Type Only if Type Specified


gamerzfuse

Recommended Posts

The old forum post solved my problem entirely, but it just introduced another one.

Rather than having a 3 page post with a new problem, I decided to start over here.

 

Situation:

 

I have the code:

$sSql = $vehicles->SelectSQL();

 

I edited that function to include:

// Table level SQL

function SqlSelect() { // Select
return "SELECT * FROM `vehicles`";


}

function SqlWhere() { // Where Type
return "type= '". mysql_real_escape_string($_GET['type']) . "'";              //Used to say just return "";

}

function SqlGroupBy() { // Group By
return "";

}
function SqlHaving() { // Having
return "";

}
function SqlOrderBy() { // Order By
return "";

}

 

Now I have a working sort on all of my pages that have a specified type (in example: http://www.example.com/inventory.php?type=car )

 

The problem is that if I have no type, then it lists no vehicles.

How can I use an if/then statement or the empty() to make it not display a type if there is no type listed?

 

Thanks again everyone!

Link to comment
Share on other sites

How can I use an if/then statement or the empty() to make it not display a type if there is no type listed?

 

Pretty much as you describe there. The logic is: if the variable is not empty, search using the variable. If it's not, dont add a where clause.

Link to comment
Share on other sites

How can I use an if/then statement or the empty() to make it not display a type if there is no type listed?

 

Pretty much as you describe there. The logic is: if the variable is not empty, search using the variable. If it's not, dont add a where clause.

 

The closest I can come is:

function SqlWhere() { // Where Type	

return (!empty($_GET['type'])) ? "type = '". mysql_real_escape_string($_GET['type']) . "' : "";

}

Link to comment
Share on other sites

What gamerzfuse means, is, below...

function SqlWhere() { // Where Type
  return (!empty($_GET['type'])) ? "type = '". mysql_real_escape_string($_GET['type']) . "'" : "";
//return (!empty($_GET['type'])) ? "type = '". mysql_real_escape_string($_GET['type']) . "' : "";
}

Look closely, you weren't closing out your final single quote after the mysql_real_escape_string().

I've left yours in as commented out so you can see what you missed.

Hope that helps.

Link to comment
Share on other sites

What gamerzfuse means, is, below...

function SqlWhere() { // Where Type
  return (!empty($_GET['type'])) ? "type = '". mysql_real_escape_string($_GET['type']) . "'" : "";
//return (!empty($_GET['type'])) ? "type = '". mysql_real_escape_string($_GET['type']) . "' : "";
}

Look closely, you weren't closing out your final single quote after the mysql_real_escape_string().

I've left yours in as commented out so you can see what you missed.

Hope that helps.

 

I used that and now I get an unexpected T_Var?

Thanks though, if you could just advise me on this one last thing.. I'll be done with this site almost.

 

Noob mistake. Left in an extra }

Works great.

Hopefully this fixes all my problems!

Thanks for the tolerance Ken ;)

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.