duan47676379 2017-10-25 13:41
浏览 103

如何将php值显示到html表中?

I have almost did the displaying of values.But i am getting an extra td I hope the complete code is right. Can anyone see it once.

php

<?php 
    session_start();
    $link = mysqli_connect('localhost','root','','hoteldetails');
    $sno[]="";
    $roomImage[]="";
    $roomNo[] = "";
    $hotelName[]="";
    $roomPrice[]="";
    $loc[]="";
    if(isset($_POST['sub']))
    {
        // mysqli_s(elect_db($link, "hotels");
        $location=$_POST['searchVal'];
        $sql = "select * from roomdetails where Location = '$location'";
        $sqldata= mysqli_query($link ,$sql);

        while($row = mysqli_fetch_array($sqldata)){

            $sno[]=$row['S.No'];
            $roomImage[] = $row['RoomImage'];
            $roomNo[] = $row['RoomNo'];
            $hotelName[] = $row['HotelName'];
            $roomPrice[] = $row['RoomPrice'];
            $loc[] = $row['Location'];
        }
       //two arrays to display ,$combine = array_combine($one,$two);
    }


    ?>

html

<?php 
         echo "<table border='1'>";
        echo "<tr><th>Hotel</th><th>Location</th></tr>";


                        foreach($sno as $id => $key):

                           echo "<tr>";
                             echo "<td>";?><img src="<?php echo $roomImage[$id];?>" height="100" width="100"><?php echo "</td>";
                            echo "<td>".$roomNo[$id]."</td>";
                            echo "<td>".$hotelName[$id]."</td>";
                           echo "<td>".$roomPrice[$id]."</td>";
                            echo "<td>".$loc[$id]."</td>";
                            echo "</tr>";

                        endforeach;
                        echo "</table>";

                        //echo $html;
                            ?>

Is there anything wrong in it . Please is this process is right to display the values from mysql into html.

  • 写回答

1条回答 默认 最新

  • duanduoding2238 2017-10-25 13:55
    关注

    First, format your code, using tools like http://phpbeautifier.com for example.

    Next, you have lines that contain both an echo and a ?> ... <?php instruction that tells php to echo stuff without any PHP parsing. This is quite unreadable and unmaintainable.

    You should better go for some "templating-like" practices using only HTML and using PHP only to output some vars, like this:

    <?php
    foreach ($data as $item) {
        ?>
        <tr>
            <td>Name: <?php echo $item['name']; ?></td>
            <td>Price: <?php echo $item['price']; ?></td>
        </tr>
        <?php
    }
    

    This would drastically improve readability, maintainability, and especially your capacity of debugging the code.

    And finally, you should take care of your php.ini configuration and check that display_errors is set to 1 and error_reporting is set to E_ALL.
    This way, any error like PHP - Notice : Undefined index ... will be shown in the output so you can debug it.

    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源