Display results of one column based on value of another column in same table.

Discussion in 'MySQL' started by hightide13, Jun 21, 2010.

  1. #1
    Can anyone help me figure this one out...it is probably very simple but I can't figure it out.

    I have joined several tables in a MySQLi query to help display records representing available rental units that my company manages.

    There is one table called 'userdefinedvalues' that is structured like this:

    unitid userdefinedid value


    userdefinedid is a numerical record that represents a characteristic of the rental unit (ex. 11=bedrooms' and 12='bathrooms') .

    value is of course the value of the userdefinedid (ex '3 or 2.4')

    unitid is the id of the rental unit and is not unique because the same unit may have several 'userdefinedid' associated to it.

    Here is an example of how records display from this table:

    unitid userdefinedid value
    1 5 No
    1 6 No
    1 8 2 Car
    1 10 2
    1 11 3
    1 12 2.5
    3 5 Yes
    3 6 Yes
    3 11 3
    3 12 2.5
    3 9 No
    5 5 Yes
    5 6 Yes
    5 11 5
    5 12 3.5

    My question is how do I display 'value' if 'userdefinedid' is 'x'...

    ie. How do I display 'Yes' (value) if userdefinedid is 6 (which represents 'does unit allow pets?)

    I have tried the following but it isn't working:

    if ($row[userdefinedid] = '6') { echo "$row[value]"; }
    Code (markup):
    Does anyone have any thoughts or do I need to provide more information?
     
    hightide13, Jun 21, 2010 IP
  2. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i forgot to mention that this is my SELECT statement where I already use one id and value to narrow the results...

    if ($result = mysqli_query($link, "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid)
    		JOIN marketrent ON unituserdefinedvalues.unitid = marketrent.unitid
    		WHERE units.propid ='8' AND units.unittypeid IN (1, 11, 12, 17, 25, 16, 13, 15)
    		AND unituserdefinedvalues.userdefinedid ='9'
    		AND unituserdefinedvalues.value ='Yes'
    		ORDER BY marketrent.amount DESC")) {
    Code (markup):
     
    hightide13, Jun 21, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    You need:

    if ($row['userdefinedid'] == '6') { echo $row['value']; }
    PHP:
    Or you can do it shorthand to save space (means the same as above):

     echo ($row['userdefinedid'] == '6') ? $row['value'] : ''; 
    PHP:
     
    jestep, Jun 22, 2010 IP
  4. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hey jestep.

    i appreciate the response.

    i tried both of those and neither worked.

    do you think it has something to do with me using

    AND unituserdefinedvalues.userdefinedid ='9'
    		AND unituserdefinedvalues.value ='Yes'
    Code (markup):
    in the SELECT statement? Am I predetermining the values throughout the rest of the query by doing it that way?

    FYI
    userdefinedid ='9' -> Show on Website?
    value ='Yes' -> Yes show on website


    Below is the entire statement:
    if ($result = mysqli_query($link, "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid)
    		JOIN marketrent
    		ON unituserdefinedvalues.unitid = marketrent.unitid
    		WHERE units.propid ='8'
    		AND units.unittypeid IN (1, 11, 12, 17, 25, 16, 13, 15)
    		AND unituserdefinedvalues.userdefinedid ='9'
    		AND unituserdefinedvalues.value ='Yes'
    		ORDER BY marketrent.amount DESC")) {
    
        while ($row = mysqli_fetch_array($result)) {
    
    
    
    			echo "<tr>";
    			echo "<td valign=\"top\" width=\"40%\">$row[street1]</td>";
                echo "<td valign=\"top\">";
                echo ($row['userdefinedid'] == '11') ? $row['value'] : '';
                echo ($row['userdefinedid'] == '12') ? $row['value'] : '';
                echo "- apprx $row[sqft] sqft</td>";
                echo "<td valign=\"top\">$$row[amount]</td>";
    			echo "</tr>";
        }
    
        /* Destroy the result set and free the memory used for it */
        mysqli_free_result($result);
    
    } else {
        print("results not found");
    }
    Code (markup):
    and the results currently look like this:

    
    112 Beresford Creek Street	- apprx 3100 sqft	$3800
    
    Code (markup):
    where my intention is to have it look like this


    
    112 Beresford Creek Street	4 BR - 3.5 BA - apprx 3100 sqft  	$3800
    
    Code (markup):

    I really appreciate your help.
     
    hightide13, Jun 22, 2010 IP
  5. hightide13

    hightide13 Peon

    Messages:
    127
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    tried it this way too with no results.

    
    
    echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" width=\"45%\">\n";
    
    if ($result = mysqli_query($link, "SELECT * FROM (units JOIN unituserdefinedvalues ON units.unitid = unituserdefinedvalues.unitid)
    		JOIN marketrent	ON unituserdefinedvalues.unitid = marketrent.unitid
    		WHERE units.propid ='8'
    		AND units.unittypeid IN (41,42,43)
    		ORDER BY marketrent.amount DESC")) {
    
        while ($row = mysqli_fetch_array($result)) {
    
    if ($row['userdefinedid'] == '9' AND $row['value'] == 'Yes' ) {
    
    			echo "<tr>";
    			echo "<td valign=\"top\" width=\"40%\">$row[street1]</td>";
                echo "<td valign=\"top\">";
                if ($row['userdefinedid'] == '11') { echo "<font color=ff0000>$row[value]</font> Br -"; }
                if ($row['userdefinedid'] == '12') { echo "<font color=ff0000>$row[value]</font> Ba "; }
                echo "- apprx $row[sqft] sqft</td>";
                echo "<td valign=\"top\">$$row[amount]</td>";
    			echo "</tr>";
    			}
    
        }
    
        /* Destroy the result set and free the memory used for it */
        mysqli_free_result($result);
    
    
    } else {
        print("results not found");
    }
    echo "</table><br>\n";echo "<br>\n";echo "<br>\n";
    
    
    Code (markup):
     
    hightide13, Jun 23, 2010 IP