Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL inline if statement

Tags:

php

mysql

I have a query that selects the name, description, date, etc of an item from a single table. This table has a field named "archived". If a row has a 1 in the archived field, it means that it's archived.

I currently use this code here to display my list of items, whether it's archived or not:

<?php
        // Request the text for zz
        $result = @mysql_query("SELECT oc_sysitem,oc_item,oc_itemdesc,oc_genre,oc_star,oc_alttxt,oc_iearchived,oc_url FROM catalog WHERE oc_sysitem LIKE('zz') ORDER BY oc_sysitem DESC, oc_item, oc_setnum +0 ASC");

        if (!$result) {
            exit('<p>Error performing query: ' . mysql_error() . '</p>');
        }
        //creating the table w/ headers
        echo '
        <table cellpadding="3px">
        <tr>
            <td style="width:360px;font-weight:bold"><p>Title</p></td>
            <td style="width:90px;font-weight:bold"><p>Genre</p></td>
            <td style="width:70px;font-weight:bold"><p>System</p></td>
            <td style="width:51px;font-weight:bold"><p>Rating</p></td>
        </tr>';     
        // Display each zz
        while ($row = mysql_fetch_array($result)) {
        echo '
        <tr title="' . $row['oc_item'] . ' ' . $row['oc_itemdesc'] . ', ' . $row['oc_alttxt'] . '" class="tblhover">
            <td style="width:360px"><a href="' . $row['oc_url'] .'" rel="nofollow">' . $row['oc_item'] . ' ' . $row['oc_itemdesc'] . '</a></td>
            <td style="width:90px">' . $row['oc_genre'] . '</td>
            <td style="width:70px">' . $row['oc_sysitem'] . '</td>
            <td style="width:51px"><img src="../../media/ocdb/' . $row['oc_star'] . '" alt="' . $row['oc_alttxt'] . '" title="' . $row['oc_alttxt'] . '" /></td>
        </tr>
        ';
        }       
        echo '</table>';        
        ?>

I want to be able to display an asterisk next to the title if it is archived. I have not been able to figure out how to add an if statement or if that right path to take. Any advice would be welcomed, thank you.

like image 443
James Avatar asked Jul 30 '12 17:07

James


3 Answers

There is the IF function for MySQL:

if(expr1, expr2, expr3)

if expr1 is TRUE it returns expr2, else it returns expr3

see here for more details

like image 106
grinch Avatar answered Oct 04 '22 03:10

grinch


you can do this in sql

example:

CASE WHEN Archived = 1 THEN Title + '*' ELSE Title END As Title

In your case

CASE WHEN oc_iearchived = 1 THEN oc_item + ' ' + oc_itemdesc + '*' 
ELSE oc_item + ' ' + oc_itemdesc END As [Title]
like image 44
rs. Avatar answered Oct 04 '22 03:10

rs.


Using the Ternary if statement can make this simple in PHP.

 $title = $row['oc_item'] . ' ' . $row['oc_itemdesc'] . ', ' . $row['oc_alttxt'] . $row['oc_iearchived'] == 1 ? '*' : '';
 echo '
 <tr title="' . $title . '" class="tblhover">  
 //etc
like image 35
Vincent Ramdhanie Avatar answered Oct 04 '22 03:10

Vincent Ramdhanie