This is a discussion on Reg. Dynamically populate a checkbox within the PHP General forums, part of the PHP Programming Forums category; HI, First of all im vry new to PHP, i like to retrieve a value from DB, accordingly i like ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
HI,
First of all im vry new to PHP, i like to retrieve a value from DB, accordingly i like to display the checkbox, either checked or not. Now im able to retrieve the value from DB, i can show even show the checkbox, but i cant make it checked???? Can anyone help, plzzzzzzz!!! the Code i used to display as follows, #Require the database class require_once('../dbinfoinc.php'); #Get an array containing the resulting record $query = "SELECT * FROM event"; $result = mysql_query($query); $num=mysql_numrows($result); mysql_close(); $i=0; while ($i < $num) { if(($i % 2) == 0) { $c = '"TableDetail"'; } else { $c = '"TableDetail2"'; } $event_id=mysql_result($result,$i,"event_id"); $event_name=mysql_result($result,$i,"event_name"); $event_publish=mysql_result($result,$i,"publish"); // if(($event_publish) == 0) { $event_publish = 'No'; } // else { $event_publish = 'YES'; } $take_action = 'Delete'; 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' if($event_publish==1){'CHECKED';}?></td> <td class='navText' align='center' class=$c><a href='delete_event.php?event_id=$event_id'>$take_a ction</a></td></tr>"; $i++; } ?> |
|
|||
|
On 08/29/2006 02:04 AM, calms wrote:
> HI, Hi > > First of all im vry new to PHP, i like to retrieve a value from DB, > accordingly i like to display the checkbox, either checked or not. > > Now im able to retrieve the value from DB, i can show even show the > checkbox, but i cant make it checked???? > [...] It's a good idea to learn HTML before learning PHP. HTML is a complicated language with one purpose, and PHP is another complicated language with a slightly different purpose. Learning both at the same time can be overwhelming. The "checked" attribute, in HTML, is used to make a checkbox checked. http://www.w3.org/TR/html4/interact/forms.html#checkbox |
|
|||
|
thks for your reply, but as you can see i used the 'checked' attribute, still i cant make it. I knw tht i have done some mistake in the echo statement. as follows, 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 <?php echo ($event_publish == 1 ? 'CHECKED' : ''); ?></td> <td class='navText' align='center' class=$c><a href='delete_event.php?event_id=$event_id'>$take_a ction</a></td></tr>"; kindly correct me... thks Mumia W. wrote: > On 08/29/2006 02:04 AM, calms wrote: > > HI, > > Hi > > > > > First of all im vry new to PHP, i like to retrieve a value from DB, > > accordingly i like to display the checkbox, either checked or not. > > > > Now im able to retrieve the value from DB, i can show even show the > > checkbox, but i cant make it checked???? > > [...] > > It's a good idea to learn HTML before learning PHP. HTML is a > complicated language with one purpose, and PHP is another > complicated language with a slightly different purpose. > Learning both at the same time can be overwhelming. > > The "checked" attribute, in HTML, is used to make a checkbox > checked. > > http://www.w3.org/TR/html4/interact/forms.html#checkbox |
|
|||
|
On 08/29/2006 03:01 AM, calms wrote:
> Mumia W. wrote: >> On 08/29/2006 02:04 AM, calms wrote: >>> HI, >> Hi >> >>> First of all im vry new to PHP, i like to retrieve a value from DB, >>> accordingly i like to display the checkbox, either checked or not. >>> >>> Now im able to retrieve the value from DB, i can show even show the >>> checkbox, but i cant make it checked???? >>> [...] >> It's a good idea to learn HTML before learning PHP. HTML is a >> complicated language with one purpose, and PHP is another >> complicated language with a slightly different purpose. >> Learning both at the same time can be overwhelming. >> >> The "checked" attribute, in HTML, is used to make a checkbox >> checked. >> >> http://www.w3.org/TR/html4/interact/forms.html#checkbox > > thks for your reply, > > but as you can see i used the 'checked' attribute, still i > cant make > it. I knw tht i have done some mistake in the echo > statement. as follows, It's difficult to understand what you write. Please spell words completely if you can. > > 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 <?php echo ($event_publish == 1 ? 'CHECKED': > ''); ?></td> > <td class='navText' align='center' class=$c><a > href='delete_event.php?event_id=$event_id'>$take_a ction</a></td></tr>"; > > kindly correct me... > > thks > 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_a ction</a></td></tr>"; |
|
|||
|
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_a ction</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'. |
|
|||
|
mootmail-googlegroups@yahoo.com 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_a ction</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. 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. Dennis (OP = Original Poster) |
|
|||
|
Dennis de Wit wrote:
> mootmail-googlegroups@yahoo.com 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_a ction</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. |