1

I want to know how should we use div tags inside a table in PHP code. This is my code. Please tell me how to use it properly.

<tbody>
    <?php
    $result = mysqli_query($conn,$sql);
    while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {  

        echo "<tr>

        <td>{$row['id']}</td>
        <td><img  width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
        <td>{$row['item_name']}</td>
        <td>{$row['description']}</td>
        <td>{$row['quantity']}</td>
        <td>echo '<div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">

            <div class="c-input-group c-spinner">
                <input type="text" class="form-control c-item-1" value="1">
                <div class="c-input-group-btn-vertical">
                    <button class="btn btn-default" type="button" data_input="c-item-1">
                        <i class="fa fa-caret-up"></i>
                    </button>
                    <button class="btn btn-default" type="button" data_input="c-item-1">
                        <i class="fa fa-caret-down"></i>
                    </button>
                </div>
            </div>
        </div> 
        </td>';                                                 
        <td> <button>submit</button>  </td>

        </tr>";


    }?>
</tbody>

I have tried using inverted comma's and braces but still, it shows error. What needs to be done here?

4
  • Possible duplicate of How to make echo table with two div tag? Commented Aug 14, 2017 at 5:42
  • @Magnus Eriksson: Dear Mr. Eriksson, Please check the entire code and purpose of posting question first and then mark it duplicate. Not Every question is similar to other, Everyone has their own different doubts and that's why they come here and post a question . It's a kind request please Stop doing Just for the sake of your badges and pointers. Commented Aug 14, 2017 at 5:51
  • @user8380237 Dear user, please check the username after the comment and you'll see that it wasn't me that wrote it. Commented Aug 14, 2017 at 5:57
  • @MagnusEriksson: I owe an apology then Commented Aug 14, 2017 at 6:01

2 Answers 2

4

You can write the div inside table. Don't put all table inside php tag. remove html outside the <?php ?> php code. after that whenever you want to echo any value just used the php.

Customize your code like below,

<table>
    <tbody>
        <?php
        $result = mysqli_query($conn, $sql);
        while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
            ?>
            <tr>
                <td><?php echo $row['id']; ?></td>
                <td><img  width='90px' height='90px' src='imageView.php?id=<?php echo $row["id"]; ?>' /> </td>
                <td><?php echo $row['item_name']; ?></td>
                <td><?php echo $row['description']; ?></td>
                <td><?php echo $row['quantity']; ?></td>
                <td>
                    <div class="col-md-1 col-sm-3 col-xs-6 c-cart-qty">

                        <div class="c-input-group c-spinner">
                            <input type="text" class="form-control c-item-1" value="1">
                            <div class="c-input-group-btn-vertical">
                                <button class="btn btn-default" type="button" data_input="c-item-1">
                                    <i class="fa fa-caret-up"></i>
                                </button>
                                <button class="btn btn-default" type="button" data_input="c-item-1">
                                    <i class="fa fa-caret-down"></i>
                                </button>
                            </div>
                        </div>
                    </div> 
                </td>                                             
                <td> <button>submit</button>  </td>
            </tr>
        <?php } ?>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

4 Comments

A good answer includes an explanation, not just a chunk of code. What have you changed? Why? How did it solve the issue?
@Narayan: Thank You. It Helped and I understood my mistake also. I was including <td> inside PHP which makes no sense. It was a stupid mistake but thanks
@MagnusEriksson I have update my code with explanation
@user8380237 if it is working for you please accept this as answer :)
0

Within a double quote you can use a single quote

<tbody>
                                         <?php
                                          $result = mysqli_query($conn,$sql);
                                               while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {  

                                            echo "<tr>
                                               
                                                <td>{$row['id']}</td>
                                                <td><img  width='90px' height='90px' src='imageView.php?id=".$row["id"]."' /> </td>
                                                <td>{$row['item_name']}</td>
                                                 <td>{$row['description']}</td>
                                                <td>{$row['quantity']}</td>
                                                 <td><div class='col-md-1 col-sm-3 col-xs-6 c-cart-qty'>
                                                      
                                                        <div class='c-input-group c-spinner'>
                                                         <input type='text' class='form-control c-item-1' value='1'>
                                                           <div class='c-input-group-btn-vertical'>
                                                             <button class='btn btn-default' type='button' data_input='c-item-1'>
                                                               <i class='fa fa-caret-up'></i>
                                                              </button>
                                                             <button class='btn btn-default' type='button' data_input='c-item-1'>
                                                                <i class='fa fa-caret-down'></i>
                                                              </button>
                                                            </div>
                                                       </div>
                                                     </div> 
                                                 </td>;                                                 
                                                <td> <button>submit</button>  </td>
                                
                                              </tr>";
                                           
                                           
                                            }?>
                                        </tbody>

1 Comment

Thanx K-square.

Your Answer

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