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
Changing $_GET Id to $_GET Name?
Old 06-26-2009, 10:35 AM Changing $_GET Id to $_GET Name?
downliner's Avatar
Extreme Talker

Posts: 208
Location: Orkney
Trades: 1
I've been trying to figure this out for a few days now so would appreciate a nudge in the right direction for solving it I have a select/option dropdown populated with colours from a database, each colour has a unique ID.

Code:
    <form method="get" action="index.php" enctype="multipart/form-data">
         <select name="colour" id="colour">
            <option value="0">Search By Colour</option>
            <option value="1">Blue</option>
            <option value="2">Green</option>
            <option value="3">Red</option>
          </select>
     </form>
The search query works as follows:

Quote:
if ( isset($_GET['colour'] )
{
$query="SELECT C.*, I.* FROM colour_ids AS C INNER JOIN products_inventory AS I ON C.productId=I.productId where C.Colour_Id=".intval($_GET['colour'])." GROUP BY C.productId";
}
If the URL contains a colour ID it displays all products of that color.

The above works great, but what I would like to do is pass the colour name to the URL, instead of the ID. When I change the form as follows:

Code:
<option value="Blue">Blue</option>
The colour name is successfully passed to the URL, however the search fails because the query is looking for an ID in isset($_GET['colour']) - bold in above query.

I believe I have to pass the value as value="{NAME}" (like above), instead of using {ID}. If so, just prior to the query executing how do I go about changing {NAME} back to its equivelent {ID} for the query to be successful?

Any pointers or help appreciated Thanks (above code clipped for easier reading)
__________________

Please login or register to view this content. Registration is FREE

Last edited by downliner; 06-26-2009 at 10:37 AM..
downliner is offline
Reply With Quote
View Public Profile Visit downliner's homepage!
 
 
Register now for full access!
Old 06-26-2009, 11:12 AM Re: Changing $_GET Id to $_GET Name?
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Would this work?

WHERE C.Color_Name LIKE '" . mysql_real_escape_string($_GET['colour']) . "'
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 06-26-2009, 12:23 PM Re: Changing $_GET Id to $_GET Name?
downliner's Avatar
Extreme Talker

Posts: 208
Location: Orkney
Trades: 1
What you suggested is essentially what I believed to be the solution, only the Colour_Names are stored in a seperate table and I don't understand how I can use a Join within the same query. The DB format is like below:

colour_ids
| ID | Product_ID | Colour_ID
----------------------
| 1 | 10123 | 3
| 2 | 10123 | 6
| 3 | 10124 | 3
| 4 | 10124 | 3

colours
| ID | Colour_Name
----------------------
| 1 | Blue
| 2 | Green
| 3 | Red

Code:
$query="SELECT O.*, C.*, I.* FROM colours AS O INNER JOIN colour_ids AS C ON O.Id=C.Colour_Id Inner Join products_inventory AS I ON C.productId=I.productId WHERE O.Name=".mysql_real_escape_string($_GET['colour'])." GROUP BY C.productId";
The above doesn't work, though am I on the right track in using another Join?
__________________

Please login or register to view this content. Registration is FREE

Last edited by downliner; 06-26-2009 at 12:25 PM..
downliner is offline
Reply With Quote
View Public Profile Visit downliner's homepage!
 
Old 06-26-2009, 08:55 PM Re: Changing $_GET Id to $_GET Name?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Out of curiosity, how does that one works ?
PHP Code:
$colour=mysql_real_escape_string($_GET['colour']);
$query=<<<SQL
select *
from products_inventory
where Product_ID in (
  select i.Product_ID
  from products_inventory i
    inner join colour_ids cid
      on cid.Product_ID=i.Product_ID
    inner join colours c
      on c.ID=cid.Colour_ID
      and c.colour_Name='
{$colour}'
    group by i.Product_ID
)
SQL; 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Changing $_GET Id to $_GET Name?
 

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.13635 seconds with 12 queries