Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to PHP and I have problem. I have a table with check boxes. I need to add if statement inside the check box.

  echo "<div class='table1'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
echo "<form action='confirmsubmission.php' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'  
value='".$row['module_id']."' />" . "</td>";
echo "<td>" . $row['module_id'] .  "</td>"; 
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";
echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></div>";

Below is a If statement I need to add after value='".$row['module_id']."'

 if($row['module_choice']=='Mandatory'){ echo "checked=\"true\""; }
share|improve this question
    
checked='checked' i don't think html uses escape characters, so \" inside your string probably won't work –  Jeff Hawthorne Feb 20 '13 at 14:15
    
@JeffHawthorne checked="checked" is correct, but the escaping is there for PHP, not HTML. –  Colin Morelli Feb 20 '13 at 14:15
    
@Colin Morelli it's inside an echo string though, will php even see it? –  Jeff Hawthorne Feb 20 '13 at 14:18
    
@JeffHawthorne If it were echo "checked="true"";, PHP would complain about a parse error because of the quotes. Using echo "checked=\"true\"" tells PHP that the quotes should be echoed as well, and shouldn't be interpreted as the end of the string. –  Colin Morelli Feb 20 '13 at 14:20
    
gotcha, i usually just mix the quote types for simplicity because php doesn't make a huge distinction between single and double quotes –  Jeff Hawthorne Feb 20 '13 at 14:25
add comment

2 Answers

up vote 0 down vote accepted

A bit of a mouthful, but try:

...    
echo "<td>" . "<input type='checkbox' name='check[]' value='".$row['module_id']."'".($row['module_choice']=='Mandatory' ? 'checked="true"' : "")." />" . "</td>";
...
share|improve this answer
    
Read thanks so much...that worked. I've spend almost two days to fix it.....Thank you!!!!!! –  dimitiy Feb 20 '13 at 17:26
    
@dimitiy - In that case a click on the tick would do nicely, please! –  Raad Feb 20 '13 at 17:27
    
Done. Thanks again ) –  dimitiy Feb 20 '13 at 17:31
add comment

Even though I'll advise you to use a templating system (Smarty, etc.), you can do the following code:

echo "<div class='table1'>
<form action='confirmsubmission.php' method='post'>
<table>
<tr>
<td></td>
<td>Module code</td>
<td>Module Title</td>
<td>Option</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "<input type='checkbox' name='check[]'  
value='".$row['module_id']."'";
if($row['module_choice']=='Mandatory'){ 
    echo " checked='checked' "; 
}
echo "/>" . "</td>";
echo "<td>" . $row['module_id'] .  "</td>"; 
echo "<td>" . $row['module_title'] . "<a href=# content='".$row['description']."'
class='tooltip'><span title='Module Description'><img src='images/i.png'/></span>
</a>". "</td>";

echo "<td>" . $row['module_choice'] . "</td>";
echo "</tr>";
}
echo "</table></form></div>";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.