Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
"Select All", then "Select Where" in single query?
Old 10-04-2009, 10:51 PM "Select All", then "Select Where" in single query?
downliner's Avatar
Extreme Talker

Posts: 209
Location: Orkney
Trades: 1
I know this is possible to do but struggling to find the answer through Google After selecting all rows from a table I need to show certain additional information from a single row.

Currently I'm selecting all rows from the table to populate a dropdown as below:

Code:
        $wherestr = "";
        $couriers = "SELECT * FROM couriers ".$wherestr." ORDER BY id";
    
        $rs1 = mysql_query($couriers);
            while($row = mysql_fetch_array($rs1))
            {
            // Print each courier as an option in the dropdown
                echo "<option ";
            if($result[0]['ship_courier']==$row['id']) echo "selected=\"selected\"";
                echo "value=\"".$row['id']."\">".$row['name']."\n  ";    
            }
        ?>
Works great. Depending on whichever courier has been chosen for the order I'm now needing to use that information to display the correct tracking URL (stored in a seperate column, same table):

Code:
        $wherestr .= "where id=".$result[0]['ship_courier']."";
        $couriers2 = $db->select($couriers);

        // Grab the tracking URL
        if ($result['0']['tracking_no']<>"") {
                $track="<a target=\"_blank\" href=\"";
                $track.=$couriers2[0]['tracking_url_1'].$result[0]['tracking_no'];
                $track.="\">Track online</a>";
        } else {
                $track="Not Tracked";
            }
    echo $track;
The $wherestr is what I'm needing to add to the original Select query once it has initially run through to grab the names for my dropdown.

Currently it just grabs the first tracking URL found, and not the tracking URL of the selected courier. If I move my $wherestr to execute before the SELECT statement runs it successfully grabs the courier and correct URL, however it doesn't populate the remainder of the dropdown options.

Hope someone can put me out of my misery and tell me where I need to look
__________________

Please login or register to view this content. Registration is FREE
downliner is offline
Reply With Quote
View Public Profile Visit downliner's homepage!
 
 
Register now for full access!
Old 10-05-2009, 05:42 AM Re: "Select All", then "Select Where" in single query?
lizciz's Avatar
Webmaster Talker

Posts: 744
Name: Mattias Nordahl
Location: Sweden
Trades: 0
If I understand you correctly, you are only doing a small mistake. You declare the variables

$wherestr = "";
$couriers = "SELECT * FROM couriers " . $wherestr . " ORDER BY id";


Then later, you change the $wherestr and think that the $courier variable will also change. php doesn't remeber that you declared $couriers by using $wherestr and automatically updates it's value when $wherestr changes.

So you need to redeclare them both.

$wherestr = "WHERE id=" . $result[0]['ship_courier'];
$couriers = "SELECT * FROM couriers " . $wherestr . " ORDER BY id";



And then I see you're storing the new database result in $couriers2, but you try to retreive the result from $result. So this row

$couriers2 = $db->select($couriers);

should probably be

$result= $db->select($couriers);

unless you want to keep both results for later.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to "Select All", then "Select Where" in single query?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46926 seconds with 12 queries