Jump to content

Table Sort vs. ?type=car Conflict


gamerzfuse

Recommended Posts

That same one line of code. Just tack on the ORDER BY clause after the WHERE clause.

 

$sSql = (!empty($_GET['type'])) ? $vehicles->SqlSelect() . " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "'" : $vehicles->SQL() "AND ORDER BY '" . $_GET['order'] . "'";

 

That line of code?

If so, could you possibly tell me how to properly add it to the end of that URL as mine is clearly incorrect.

 

I know this is getting annoying for you I'm sure and I hate to be a pain in the ass.. I'm just trying to learn this stuff and it's alot at once and I've read more tutorials and guides in the past week then I care to remember.

 

Link to comment
Share on other sites

You're just thinking too much. Don't add it after the : because the is the empty $_GET['type'] case. You don't need to worry about that one because you know it did the right thing. It only doesn't work if $_GET['type'] is empty.

 

I know you're learning. This is actually one of the more simple things you can do, which is why I am intentionally *NOT* writing this for you. :)

Link to comment
Share on other sites

You're just thinking too much. Don't add it after the : because the is the empty $_GET['type'] case. You don't need to worry about that one because you know it did the right thing. It only doesn't work if $_GET['type'] is empty.

 

Ok so.. I'm very close.

I got the right output from the variable: SELECT * FROM `vehicles` WHERE type = 'car' AND ORDER BY 'make'

By using the code:

$sSql = (!empty($_GET['type'])) ? $vehicles->SqlSelect() . " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "' AND ORDER BY '" . $_GET['order'] . "'" : $vehicles->SQL();

(thanks Ken)

 

Now I'm still getting the errors

1) Failed to execute SQL. Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY 'make'' at line 1

 

2) Fatal error: Call to a member function fields() on a non-object in /home8/craighoo/public_html/goodwills/final/inventory.php on line 913      (Line 913 just calls on the field Status, and since it's not coming up due to the previous error, this error comes up too)

 

Do I need an order type in my syntax too?

Link to comment
Share on other sites

change

$sSql = (!empty($_GET['type'])) ? $vehicles->SqlSelect() . " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "' AND ORDER BY '" . $_GET['order'] . "'" : $vehicles->SQL();

t0

 

$sSql = (!empty($_GET['type'])) ? $vehicles->SqlSelect() . " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "' ORDER BY '" . $_GET['order'] . "'" : $vehicles->SQL();

Link to comment
Share on other sites

$sSql = (!empty($_GET['type'])) ? $vehicles->SqlSelect() . " WHERE type = '" . mysql_real_escape_string($_GET['type']) . "' ORDER BY '" . $_GET['order'] . "'" : $vehicles->SQL();

 

The code you supplied fixed all the errors and gives me this output from the variable:

SELECT * FROM `vehicles` WHERE type = 'car' ORDER BY 'model'

 

By clicking column headers I can change the output from the variable correctly, but it doesn't actually sort the table..

Link to comment
Share on other sites

OK!

Last issue:

 

I took out the whole line of code and set it back to:

$sSql = $vehicles->SelectSQL();

 

Then 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']) . "'";
}

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

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

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

 

Now it works perfect IF I specify a type in the URL.

If I navigate to just inventory.php then it won't display any records because the output of the execution is:

SELECT * FROM `vehicles` WHERE type= '' ORDER BY `model` ASC

 

Any chance I can use something like this empty() in order to avoid having that information added if there is no type specified?

Link to comment
Share on other sites

I'm actually giving you better advice. If you inline the $_GET['type'], then the function isn't that great because if you want a different parameter say $_GET['name'], then you'll have to write a bunch of if and else statements. Google PHP Functions Optional Parameters.

Link to comment
Share on other sites

I'm actually giving you better advice. If you inline the $_GET['type'], then the function isn't that great because if you want a different parameter say $_GET['name'], then you'll have to write a bunch of if and else statements. Google PHP Functions Optional Parameters.

 

Ok.. googling now.

 

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

 

I know that this may make it more difficult in the future, but this is the only thing I'll ever need to get from the URL.

Link to comment
Share on other sites

So basics 101 -

1. If you put things in quotes, they are treated as a string, excluding interpolation of variables.

2. PHP code should not be in quotes unless you're running it via eval.

 

So if you put (!empty($_GET['type']))? inside quotes, then that's what you get. It'll be treated as a string, not as PHP code.

Link to comment
Share on other sites

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

This one gives me an error of unexpected "", but if I take one away it gives me unexpected ;

 

This post from PHP.net may be more like what I need?

 

function myFunction($myArgument=null) {

    if($myArgument===null)

        $myArgument = $GLOBALS["myVar"];

    echo $myArgument;

}

 

Although that has no return statement.. so probably not.

 

Link to comment
Share on other sites

The unexpected error is because you need to watch your quotes. See the coloring up there? The red part at the end? Well, that means you're not closing the quotes correctly.

 

That PHP.net function is fine. I suggest you read up PHP 101 because you really don't have a clue on what you're doing. I mean the codes on PHP.net are examples. You're not going to find something to copy and paste and expect it to work for every situation. If it doesn't have a return statement, then you can put one in. The example is just to show you how things work.

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.