Jump to content

[SOLVED] displaying data based on identifier in $row


zhangy

Recommended Posts

Hi, Im trying to display msql database info based on the content of a identifier row.

find the value of the row and display the content based on that value.

so for example: if the row colors = green then display all content from all rows where color = green.

but if the row color = red then display all content from all rows where color = red

or if the row color = blue ...

 

 

Link to comment
Share on other sites

I don't think i understand. I assume you didn't mean something like the following:

 

$color = 'green';
$sql = "SELECT * FROM yourtable WHERE color='$color'";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
//display the results

 

Or did you mean that the colour in question is selected from the colour of another row given by some criteria? Something like a "show similar articles (or colours)"?

 

In which case, you can either perform one query to extract the colour or do it in one:

 

SELECT * FROM yourtable WHERE colour=(SELECT colour FROM `incoming` WHERE ...)

Link to comment
Share on other sites

I'm still not sure i follow exactly, but did you want something like this then?

 

$colors = array($_GET['color1'],$_GET['color2'],$_GET['color3']);
$colors = implode("','",$colors);
$sql = "SELECT * FROM yourtable WHERE colors IN '$colors'";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
//display the results

Link to comment
Share on other sites

Ok so there is the id passed in the url which lets say is equal to 1.

if this row thats id is equal to 1 has red as the value in the (sorry) COLUMN "colors"

let all rows with the value "red" in the COLUMN "colors" be  dispayed

if blue is the value then let all blue be displayed.

or if its green then let green be displayed.

 

I hope this makes better sense.

 

$id = (int) $_GET['id']; 
$sql = "SELECT * FROM $table WHERE id=$id";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

Link to comment
Share on other sites

Well that's roughly what i posted here:

 

SELECT * FROM yourtable WHERE colour=(SELECT colour FROM `incoming` WHERE ...)

 

Fitting it in with what you have:

 

$id = (int) $_GET['id']; 
$sql = "SELECT * FROM yourtable WHERE colors=(SELECT colors FROM `incoming` WHERE id=$id)";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);

Link to comment
Share on other sites

ok sorry, what does 'incoming' mean here? is that a place holder for the table thats passed in the url? cause in this case they are the same table. so by your example 'incoming' should be "yourtable"?

 

 

Apologies, yes. I'm not really sure where incoming came from. I guess i must have been doing two things at once

Link to comment
Share on other sites

Ok great it works. Thanks GingerRobot!

One last question for this please!

Now I am displaying posts that are the color "red".

The list includes the post with the id that is passed in the url.

is there a way to pass all the red posts except for the one that has the same id as that which was passed in the url?  :confused: Thanks again for all the help!

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.