Jump to content

[SOLVED] display results by order of matching criteria


webent

Recommended Posts

I am running mysql version 4.1.22-standard-log on Cpanel version 11.23.4-RELEASE

 

First let me show you the code, it'll help explain what I'm trying to accomplish...

unset ($query);
$product_line_result = mysql_query ("SELECT product_line FROM $local_database.product_lines ORDER BY sort_order", $b_link);
    while ($product_line_row = mysql_fetch_assoc($product_line_result)) {
        $query=$query."'$product_line_row[product_line]', ";
    }
if (!empty($query)) {
    $query = substr_replace($query," ",-2);    
$vendor_result = mysql_query ("SELECT vendor_name FROM $local_database.vendor_accts", $b_link);
    while ($vendor_row = mysql_fetch_assoc($vendor_result)) {   
    $pre_query = "SELECT DISTINCT product_line FROM webwired_dropshipdirect.products WHERE product_vendor = '$vendor_row[vendor_name]' AND product_line IN ( $query )";            
        $pre_result = mysql_query($pre_query, $a_link);
        while ($pre_row = mysql_fetch_assoc($pre_result)) {
            $product_line = urlencode($pre_row['product_line']); 
            echo'<li><a href="index.php?product_line=' . $product_line . '">' . $pre_row['product_line'] . '</a></li>';

 

Ok, so basically, on the first part of the code, I'm building a query, using a "sort_order" field to put it in a user defined listing order... So what I'd like to accomplish is that the "pre_query" query, I'd like to make it spit out the results in the same order in which it receives the criteria from the built query...

 

So if the "pre_query" looks something like this,...

product_line IN ( Jewelry, Automotive )

It will list the relevant data with Jewelry first and not Automotive ...

 

I hope all of this makes sense, thanks in advance for any help...

Link to comment
Share on other sites

Fenway, the reason there are so many seperate queries is because some of the queries access a centralized database ($a_link) and others access settings on a local database ($b_link) ... The other reason there are so many queries is because the sub-categories get queried only after a parent category is selected... You can see how it works at http://imauser.webzcart.com/ , the categories section on the left...

Here is the entire piece of code...

 

<?  
echo '
<div id="categories">
<div id="categories-header">
Categories
</div>
<div id="product_lines">
<ul>';  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
unset ($query);
$product_line_result = mysql_query ("SELECT product_line FROM $local_database.product_lines ORDER BY sort_order", $b_link);
    while ($product_line_row = mysql_fetch_assoc($product_line_result)) {
        $query=$query."'$product_line_row[product_line]', ";
    }
if (!empty($query)) {
    $query = substr_replace($query," ",-2);    
$vendor_result = mysql_query ("SELECT vendor_name FROM $local_database.vendor_accts", $b_link);
    while ($vendor_row = mysql_fetch_assoc($vendor_result)) {   
    $pre_query = "SELECT DISTINCT product_line FROM webwired_dropshipdirect.products WHERE product_vendor = '$vendor_row[vendor_name]' AND product_line IN ( $query )";            
        $pre_result = mysql_query($pre_query, $a_link);
        while ($pre_row = mysql_fetch_assoc($pre_result)) {
            $product_line = urlencode($pre_row['product_line']); 
            echo'<li><a href="index.php?product_line=' . $product_line . '">' . $pre_row['product_line'] . '</a></li>';
            if ($categories_liststyle_showsubs == "yes") {  
            if (urldecode($_GET['product_line']) == "$pre_row[product_line]") { 
                echo '<ul class="sub">';
                $result = mysql_query("SELECT DISTINCT product_master_category FROM webwired_dropshipdirect.products WHERE product_vendor = '$vendor_row[vendor_name]' AND product_line = '$pre_row[product_line]'", $a_link);
                while ($row = mysql_fetch_assoc($result)) {
                    if (!empty($row['product_master_category'])) {
                    $product_master_category = urlencode($row['product_master_category']);
                    echo '<li><a href="index.php?product_line=' . $product_line . '&product_master_category=' . $product_master_category . '">' . $row['product_master_category'] . '</a></li>';
                    }       
                    if (urldecode($_GET['product_master_category']) == "$row[product_master_category]") {
                        echo '<ul class="sub_sub">';
                        $sub_result = mysql_query("SELECT DISTINCT product_category FROM webwired_dropshipdirect.products WHERE product_vendor = '$vendor_row[vendor_name]' AND product_line = '$pre_row[product_line]' AND product_master_category = '$row[product_master_category]'", $a_link);
                        while ($sub_row = mysql_fetch_assoc($sub_result)) {
                            if (!empty($sub_row['product_category'])) {
                            $product_category = urlencode($sub_row['product_category']);
                            echo '<li><a href="index.php?product_line=' . $product_line . '&product_master_category=' . $product_master_category . '&product_category=' . $product_category . '">' . $sub_row['product_category'] . '</a></li>';
                            }    
                        }
                        echo '</ul>';
                    }
                }
                echo '</ul>'; 
            }
            }
        }
    }
}                
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
echo '
</ul>
</div>
</div>       
    ';    
?>

Link to comment
Share on other sites

As in:

 

ORDER BY FIELD( product_line, 'Jewelry', 'Automotive' )

 

Just be aware that this assigns a value based on the 1-indexed position in the list you provided -- non-matching results will get a value of 0 --- so if you have non-matches, then flip the order:

 

ORDER BY FIELD( product_line, 'Automotive', 'Jewelry' ) DESC, product_line

Link to comment
Share on other sites

Thank you very much fenway, it worked perfectly... Here's the final query...

 

SELECT DISTINCT product_line FROM webwired_dropshipdirect.products WHERE product_vendor = 'DropShipDirect' AND product_line IN ( 'Jewelry', 'Automotive' ) ORDER BY FIELD( product_line, 'Jewelry', 'Automotive' ) 

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.