View Full Version : Newbe....Can not find an explanation for one line of code

07-11-2012, 08:38 PM
Can someone please explain this line of code, in particular the syntax, quotes, dots slashes, ?, etc.

<select name="venue" onChange="autoSubmit();">
<option value="null">Select Venue</option>



$sql1 = "SELECT vid, vname FROM venue WHERE re_id = $region";
$venues = mysql_query($sql1,$conn);

while($row = mysql_fetch_array($venues))

echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" : "") . ">$row[vname]</option>");



echo $row["vname"];

It is this line that I am having problems with

echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" : "") . ">$row[vname]</option>");

it works but I need to extract the "$row[vname]" into the $_GET to use in the next page, if someone could explain this line of code it would be great

07-11-2012, 08:43 PM
It's shorthand for an if statement, if $venue == vid then the option will be the default selected one
You just need the name of the select it belongs to

07-11-2012, 09:05 PM
echo $var == 'value' ? 1 : 0

Is exactly equivalent to

if ($var == 'value') echo 1;
else echo 0;


$venue == $row["vid"] ? " selected" : ""

is the same as

if ($venue == $row['vid']) echo ' selected';

The nice thing is you can do it inline, as you see, without needing to break. So you can store stuff into a string based on other variables, without having to do multiline concatenation.

07-11-2012, 09:28 PM
So how would I extract the selected "$row[vname]" for use on the next page?

echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" : "") . ">$row[vname]</option>");

The selection is made by the "$row[vid]", which is required elsewhere in the code, and is required by the next Drop Down, I also need to $_GET the "$row[vname]"for use in the post to data base

And what does the slashes do in the following

echo ("<option value=\"$row[vid]\" " . ($venue ........


07-11-2012, 09:34 PM
The term you are looking for is "ternary operator" "?:". As was mentioned it is shorthand way of writing if/else statements and is not always easily readable or easy to follow.


07-12-2012, 02:01 AM
your select name is 'venue' , so $_POST['venue'] should be available on the next page (or $_GET['venue'] if you use GET method in your form)

the slashes 'escape' the quotes e.g.

echo "hello from "me" ";
will issue a parse error but
echo "hello from \"me\" ";
does not ... see strings in the manual (http://au2.php.net/manua/en/language.types.string.php)

also ...

echo ("<option value=\"$row[vid]\" " . ($venue == $row["vid"] ? " selected" : "") . ">$row[vname]</option>");

should really be....

echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] ? " selected" : "") . ">{$row['vname']}</option>");

$row[vid] will work but issue a warning
$row['vid'] will work as expected
{braces} further tell PHP to expect a more complex variable within, say an array value or object property

07-12-2012, 04:02 PM
Well I must say that I am learning, I have another question?

I need to extract a second column from the selected

Origional code

echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] ? " selected" : "") . ">{$row['vname']}</option>");

I need to do something like this...but it will not work no matter how I configure the code, see the "&& $ven....."

echo ("<option value=\"{$row['vid']}\" " . ($venue == $row['vid'] && $ven == $row['vname'] ? " selected" : "") . ">{$row['vname']}</option>");

Is there any way to get both the "$row['vid']" and the "$row['vname']" that is selected to further process, the select name "venue" is stored in "$_GET [venue]", and works fine, and is needed for the next drop down, I also need the "$row['vname']" that is selected.

07-13-2012, 04:19 PM
Anyone have any suggestions ??

07-13-2012, 04:34 PM
Perhaps you mean to use an || instead of an &&? The ternary is otherwise correct, the first condition must match in its entirety to execute the true condition, which is why I suspect you are looking for OR and not AND.