Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if statement inside an echo? 1

Status
Not open for further replies.

coolicus

Programmer
May 15, 2007
50
GB
In my edit page of my cms I am writing the form and populating it if there is content existing, but how do I check if the checkbox has a value of 1, if so then echo 'checked'?

Here is what I am doing

Code:
$query1=" SELECT * FROM properties where id = '{$_GET['id']}' ";
$result1=mysql_query($query1);
$rs = mysql_fetch_array($result1, MYSQL_ASSOC);

echo "<form method='post' action='editscript.php'>" .
"<input type='hidden' name='id' value='{$rs['id']}'>" .
"<span class='MainText'>Property Name: </span><br /><input type='text' name='name' value='{$rs['name']}' class='admininputs'>" .

"<br /><span class='MainText'>Property Address: </span><br /><input type='text' name='address' value='{$rs['address']}' class='admininputs'>" .

"<br />pool <input type='checkbox' value='1' name='pool' />.

"<br /><input type='submit' value='submit'></form>" ;

So I need to check if pool has a value of 1, then echo checked but can I do this inside an echo?
 
Break out of the echo, do your conditional check, echo something else based on the result of the check.


You may find it easier to do a check, set a variable and then write that variable into your echo.

I do it like this

Code:
$check = "";
if($data['checkbox']==true){
   $check = " checked=\"checked\" ";
}

echo("<input type="checkbox" {$check} name="blah" />");


You might also find it simpler to break out of PHP alltogether when writing the HTML then just echo the value where needed by using

Code:
<input type="checkbox" <?=$check;?> name="blah" />

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top