Forum Moderators: coopster

Message Too Old, No Replies

php shorthand used within code, not html?

php

         

sukebe

10:11 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



hello, I have this common snippet of code for a select list being generated by php/mysql query:
<option value="<?=$db->col["short_name"]?>" <?=($state)==($db->col["short_name"])?"selected":""?>>

So, I wanted to transpose this for a query, where we're already in php:
$db->query("SELECT * FROM emails_archive WHERE eid='".$eid."'");

I want to replace $eid with something like:
<?=($var)=="users"?"uid":"eid"?>

However, I don't need the <? since I'm already in PHP at this point of the code. How can I take advantage of the shorthanded code? Would this work:
[=($var)=="users"?"uid":"eid"]

Thanks so much!

penders

8:12 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(I may have misread, but...)

The shorthand notation "<?= ... ?>" is simply a quick way to echo a value to the page. (Or, are you referring to the ternary operator "?:" ?) If you don't want to echo that value, but use it as part of an expression, assign it to a variable and use that instead.

So, something like:
$eid = ($var)=="users"?"uid":"eid"; 
$db->query("SELECT * FROM emails_archive WHERE eid='".$eid."'");


Although, having said that, you don't need to assign it to anything in this case...

$db->query("SELECT * FROM emails_archive WHERE eid='".(($var)=="users"?"uid":"eid")."'");


Note the parentheses around the expression.

Matthew1980

10:21 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there sukebe,

<option value="<?php echo $db->col['short_name']; ?>" <?php echo (($state == $db->col["short_name"]) ? 'selected' : ''); ?> >


That's how you should be expressing that, it should do what you want it to now.

Cheers,
MRb

sukebe

10:40 am on Sep 29, 2010 (gmt 0)

10+ Year Member



Thanks penders, your reply was right on. I was able to extend my new knowledge to other parts of the code.

Thanks for the shot, Matthew1980.