0

I'm trying to add a checkbox to the table column but can't figure out how to manage it.

Here is my html and php code :

<table  border="1">
  <tr>
    <th>Select</th>
    <th>ID</th>
    <th>Title</th>
    <th>Author's Last Name</th> 
    <th>Author's First Name</th>
    <th>Format</th>
    <th>Price</th>
    <th>ISBN Code</th>
  </tr>

<?php
  while ( $row = mysqli_fetch_array($result) )

  echo '<tr>';
  echo '<td>' . <input type="checkbox" value ="''" name="todelete[]" /> . $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';

   </tr>
 </table> 

?>

The code works fine. It shows everything in the table perfectly.

I just can't seem to type the "input type checkbox" in the echo statement. When I attempt to add the input the code stops working.

3 Answers 3

1

This is the way to do it :

echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';

and also, add curly braces to your while loop {}.

<?php
  while ( $row = mysqli_fetch_array($result) ) {
      echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';    
      echo '</tr>';
 }
?>
 </table> 
Sign up to request clarification or add additional context in comments.

Comments

1

You can show input type='checkbox' in php in two ways :

CASE 1 : instead close the php tag and put the html code

<?php
  while ( $row = mysqli_fetch_array($result) )
  {
    ?>
    <tr>
      <td><input type="checkbox" value ="''" name="todelete[]" />
          <?php echo $row['id']; ?> </td>
      <td><?php echo $row['title']; ?></td>
      <td><?php echo $row['author_last']; ?></td>
      <td><?php echo $row['author_first']; ?></td>
      <td><?php echo $row['format']; ?></td>
      <td><?php echo $row['price']; ?></td>
      <td><?php echo $row['isbncode']; ?></td>
   </tr>
 </table> 
<?php 
}
?>

CASE 2 : Put your html code in php with the of echo

<?php
  while ( $row = mysqli_fetch_array($result) ) {
      echo '<td><input type="checkbox" value ="" name="todelete[]" /> '. $row['id'] . '</td><td>' . $row['title'] . '</td><td>' . $row['author_last'] . '</td><td>' . $row['author_first'] . '</td><td>' . $row['format'] . '</td><td>' . $row['price'] . '</td><td>' . $row['isbncode'] . '</td>';    
      echo '</tr>';
 }
?>

2 Comments

i like the way of explanation
thanx @devpro, I just hope it solves the users issue, and is very clear to them
0
$id=$row['id'];
$str = <<<EOD
<table>
<tr>
    <td>
        <input type="checkbox" value ='' name='todelete[]' /> $id 
    </td>
</tr>
</table>
EOD;
echo $str;

Here is more example on "Heredoc string quoting example"
http://php.net/manual/en/language.types.string.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.