Jump to content

Different ORDER BY setting for different mySQL columns.


strago
Go to solution Solved by strago,

Recommended Posts

How would I have one query with the ORDER BY settings based on columns??

 

One has +0 DESC so the numbers show up in the correct order and decending, one is just DESC, and one is just ASC.

 

Gravity, InitialEarningsPerSale, and AverageEarningsPerSale having...

$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order +0 DESC LIMIT $start_from, 50";

 

ActivateDate having...

$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order DESC LIMIT $start_from, 50";

 

Links having...

$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order ASC LIMIT $start_from, 50";
 

 

Link to comment
Share on other sites

How do you have all of these in the same query?

 

Gravity, InitialEarningsPerSale, and AverageEarningsPerSale having...

 

ORDER BY $order +0 DESC LIMIT $start_from, 50";

 

ActivateDate having...

 

ORDER BY $order DESC LIMIT $start_from, 50";

 

Links having...

 

ORDER BY $order ASC LIMIT $start_from, 50";

Link to comment
Share on other sites

$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY Links $order ASC LIMIT $start_from, ActivateDate $order DESC LIMIT $start_from, Gravity $order +0 DESC LIMIT $start_from, InitialEarningsPerSale $order +0 DESC LIMIT $start_from, AverageEarningsPerSale $order +0 DESC LIMIT $start_from, 50";
 

and even trying out just a simple version...

 

$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY Gravity DESC, InitialEarningsPerSale DESC, AverageEarningsPerSale DESC, Links ASC";
 

spits out...

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/site8/public_html/CB/search.php on line 36

 

 

$default_sort = 'Gravity';
$allowed_order = array ('Link', 'Gravity','ActivateDate', 'InitialEarningsPerSale', 'AverageEarningsPerSale');
$search = $_GET['q'];

/* if order is not set, or it is not in the allowed
 * list, then set it to a default value. Otherwise,
 * set it to what was passed in. */
if (!isset ($_GET['order']) ||
    !in_array ($_GET['order'], $allowed_order)) {
    $order = $default_sort;
} else {
    $order = $_GET['order'];
}

/* construct and run our query */
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * 50;
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY Gravity DESC, InitialEarningsPerSale DESC, AverageEarningsPerSale DESC, Links ASC";

$result = mysql_query ($query);

/* make sure data was retrieved '$' */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}
Link to comment
Share on other sites

  • Solution

NOW I understand why it made no sense. I was trying to get the order by settings the wrong way. This does it, using an if statement based on the URL...

 

 

if($_GET['order'] == 'Link') {
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order ASC LIMIT $start_from, 50";
}
if($_GET['order'] == 'Gravity') {
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order +0 DESC LIMIT $start_from, 50";
}
if($_GET['order'] == 'ActivateDate') {
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order DESC LIMIT $start_from, 50";
}
if($_GET['order'] == 'InitialEarningsPerSale') {
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order +0 DESC LIMIT $start_from, 50";
}
if($_GET['order'] == 'AverageEarningsPerSale') {
$query = "SELECT Link,Gravity,ActivateDate,CONCAT('$', InitialEarningsPerSale) as InitialEarningsPerSale,CONCAT('$', AverageEarningsPerSale) as AverageEarningsPerSale FROM clickbank WHERE `Title` LIKE '%{$search}%' ORDER BY $order +0 DESC LIMIT $start_from, 50";
}
$result = mysql_query ($query);

/* make sure data was retrieved '$' */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
    echo "No data to display!";
    exit;
}

Edited by strago
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.