Jump to content

[SOLVED] Is there an easy way


phpbeginner

Recommended Posts

Without getting into 4 different results or queries, is there an easy way to sort by status the way I want. For example I would like to order them by status as follows (Featured, Regular, Coming Soon and Sold).

 

 

$query = "SELECT * FROM tblproduct where type='Truck' ORDER BY 'status'";
$result = mysql_query("SELECT * FROM tblproduct where type='Truck' ORDER BY `status` LIMIT $limitvalue, $limit;");

 

Thanks So Much and Happy New Year!!

Link to comment
Share on other sites

almost

 

inside the post is the name of the variable you want to use so

 

$_POST['sortby']

 

where your html would look like

 

<form id="form1" name="form1" method="post" action="scriptname.php">
  <select name="sortby" id="select">
    <option value="Featured" selected="selected">Featured</option>
    <option value="Regular">Regular</option>
    <option value="Coming Soon">Coming Soon</option>
    <option value="Sold">Sold</option>
  </select>
</form>

 

understand?

Link to comment
Share on other sites

no you dont have this form insert anything into a database you have it run with a get property so that it sends the data to the next page and if

$sortby = $_GET['sortby'];  or you can post it i guess ( i just like to use gets... )

and the query should look like this

 

$query = "SELECT * FROM tblproduct where type='Truck' ORDER BY ".$sortby;

 

it will order them by whatever was selected on the form... does this make sence?

Link to comment
Share on other sites

dude u dont get it u are doing it wrong here do it like this and tell me if it works or not

 

put this on the page that querys the info

(at the top or w/e it will act like a filter)

<form id="form1" name="form1" method="get" action="">
  <select name="sortby" id="select">
    <option value="Featured" selected="selected">Featured</option>
    <option value="Regular">Regular</option>
    <option value="Coming Soon">Coming Soon</option>
    <option value="Sold">Sold</option>
  </select>
<input name="submit" value="Sort" >
</form>

Then we do the php code that lets this work when you hit submit

<?php
$sortby = "ORDER BY ".$_GET['sortby'];
// now if you look at the address bar you will see that it says sortby w/e was selected.
//now for the query
$query = "SELECT * FROM tblproduct where type='Truck' ".$sortby;
//get rid of the ORDER BY thats there and put it into the $sortby. So that when you load this page up without having a filter active it will work right.
?>

Link to comment
Share on other sites

Thanks, but not working for me.....and your absolutely right, I do not get it. I have no idea why the form. I don't want the end user to query results by Featured, Coming Soon, Regular or Sold. I am just trying to get the results to query in that order (All Featured 1st, All Coming Soon 2nd, All Regular 3rd, All Sold 4th).

 

I apologize for my ignorance on this.

Link to comment
Share on other sites

I dont think you were being ignorant. The question was pretty clear. Unfortunately i can't bring you an easy answer.

 

This is why you need to plan your database. You're gonna have to alter the way you store the status of each product. You need to have a number represent the status, which can then by ordered in the normal way. e.g:

 

1 = Featured

2 = Coming Soon

3 = Regular

4 = Sold

 

You could store the names of each status with their relevant ID in a separate table and join the results if you also want to pull the name.

 

If its really not an option, then AFAIK, your only option is 4 separate queries. You could make one use of the mysql_query() function and UNION the results, but this is still 4 separate mysql queries and hence pretty inefficient.

 

Edit: If there's a limit clause to deal with, then it becomes more complicated. You'd have to perform the separate queries and stick the results in an array, then output only the required results. I'd definitely recommend changing the status to an integer.

Link to comment
Share on other sites

I appreciate that and had thought about my options and changing to a number as unfortunately I have a limit clause also so that's why I was looking for some help in here as I knew it would be a bit more complicated.

 

I thought I had planned the database to the fullest as it was simply going to be order by price....however a change in plans on the client end.

 

I appreciate all your help and insight GingerRobot. All the best in 2008!

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.