Re: Reg. Dynamically populate a checkbox



Dennis de Wit wrote:
mootmail-googlegroups@xxxxxxxxx wrote:
Mumia W. wrote:
Within an echo statement, <? php-code ?> does not work. Just
output a variable's value:

$event_publish_checked = ($event_publish == 1 ? 'CHECKED' : '');

echo "<tr>
<td Class='navText' align='center' class=$c>$event_id</td>
<td class='navText' class=$c>$event_name</td>
<td class='navText' align='center' class=$c><INPUT TYPE=CHECKBOX
NAME='publish' VALUE=1 $event_publish_checked </td>
<td class='navText' align='center' class=$c><a
href='delete_event.php?event_id=$event_id'>$take_action</a></td></tr>";

Alternatively, instead of setting a variable, you can just return the
result of the ternary operator as follows:

<?=($event_publish == 1 ? 'CHECKED' : '')?>

Note the '=' rather than 'echo'.


I think someone just noted that you cannot use the <? ?> markers in an
ECHO statement. Knowing that, this example is, sorry if I bring this
quite rudely, useless.

Ah, yes. I admit that I only skimmed the post and failed to notice
that the entire HTML segment was, itself, being output from an echo
statement. I had assumed that the snippet was in html mode, and could
drop into php mode legally. You are correct in my example being
invalid. If, however, the OP were to drop out of php mode to output
the html rather than using echo, it would be valid.


Besides, I think it's not smart to teach this method to someone who just
started using PHP. Using plain style 'if' statements is a way better
method to start learning how to program.

So again, I think the OP should close the echo statement, start a new
line, place the IF statement there, and then continue echo-ing whatever
he wants. The ternary operator (i didn't know they call them that way)
is something a starting programmer should learn if the time is right. I
don't think that's now for OP.


I will, however, defend my suggestion of the ternary operator, if only
because the OP him/her self used it in their second post. So either
they already know it, or they copied it from somewhere else. And if
they copied code from somewhere, I would hope that they at least took a
moment to understand what it does, first.

.



Relevant Pages

  • Re: can php replace javascript approach?
    ... Why switch to PHP mode when you are still outputting pure HTML? ... I personally don't echo such a large strings. ... An OT HTML issue here but that is confusing. ...
    (comp.lang.php)
  • Re: [PHP] generating an html intro text ...
    ... > html and htmlentities that may be part of the content. ... > original string: ... HTML special characters ... echo $str."\n"; ...
    (php.general)
  • Secure feedback form
    ... Now I have rewrite the feedback form. ... I am not using echo in functions anymore. ... I use return $html instead. ... user can select to "edit" the information" and will then find ...
    (alt.php)
  • Re: Script Need to check disk space on remote servers
    ... You might find this easier using WMIC (PowerShell would be even easier). ... A command like this will check one computer, myComputer and create a simple HTML table with the name, size and freespace. ... echo Checking drives on Server %%S... ... REM and then launches it using the default browser. ...
    (microsoft.public.windows.server.scripting)
  • Re: Getting ID from a presented table
    ... Jerry Stuckle ... JDS Computer Training Corp. ... Same idea - just generated with a PHP echo statement instead. ... I do not know how to do that in HTML. ...
    (comp.lang.php)