php自动回显到表中

时间:2014-04-20 21:01:34

标签: php html echo

我知道有类似的问题,但我没有找到具体的答案。 我是PHP的新手,所以道歉。

目前列出了这些信息,但我希望它在表中:

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );

if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {


        echo 'Venue Name:' . $row["venue_name"] ."<br />";
        echo 'Venue Description:' . $row["venue_description"] ."<br />";
        echo 'Venue Address:' . $row["venue_adress"] ."<br />";
        echo 'Venue Type:' . $row["venue_type"] ."<br />";
        echo '<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>';

        echo '<hr>';
        }
    }

?>

我还没有得到类似案例的任何建议,并尝试手动回显每个元素。它可以实现自动化吗?从句法上讲,我对此并不擅长。任何帮助将不胜感激,谢谢。

5 个答案:

答案 0 :(得分:0)

要在表格中加入,您需要在表格中添加HTML标记:

<?php

    include ('connect.php');

    $sql ="SELECT * FROM tbl_venues";
    $res = mysql_query ($sql) or die( mysql_error() );

    if ( mysql_num_rows($res) > 0 ) {
        echo '<table>';

        while ( $row = mysql_fetch_assoc($res) ) {

            echo '<tr>';
            echo '<td> Venue Name:' . $row["venue_name"] ."</td>";
            echo '<td>Venue Description:' . $row["venue_description"] ."</td>";
            echo '<td>Venue Address:' . $row["venue_adress"] ."</td>";
            echo '<td>Venue Type:' . $row["venue_type"] ."</td>";
            echo '<td><a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a</td>';
            echo '</tr>';
        }
        echo '</table>'
    }

?>

答案 1 :(得分:0)

尝试使用 EOF 。在大多数情况下,这是一个非常大的节省时间,并且当你将php代码片段插入到HTML文件中时非常有用。

 <?php
    include ('connect.php');

    $sql ="SELECT * FROM tbl_venues";
    $res = mysql_query ($sql) or die( mysql_error() );

    if ( mysql_num_rows($res) > 0 ) {

        echo "<table>";

        while ( $row = mysql_fetch_assoc($res) ) {

            echo <<<EOF
                <tr>
                    <td>Venue Name: {$row['venue_name']}</td>
                    <td>Venue Description: {$row['venue_description']}<td>
                    <td>Venue Address: {$row['venue_adress']}</td>
                    <td>Venue Type: {$row['venue_type']}</td>
                    <td><a href="venueedit.php?venueid={$row['venue_id']}">Edit</a></td>
                </tr>
EOF;
        }

        echo "</table>";
    }
?>

答案 2 :(得分:0)

<?php

include ('connect.php');

$sql ="SELECT * FROM `tbl_venues`";
$res = mysql_query ($sql) or die( mysql_error() );

if ( mysql_num_rows($res) > 0 ) {
    echo '<table>';

    while ( $row = mysql_fetch_assoc($res) ) {

        echo '<tr>';
        echo '<td> Venue Name:' . $row["venue_name"] ."</td>";
        echo '<td>Venue Description:' . $row["venue_description"] ."</td>";
        echo '<td>Venue Address:' . $row["venue_adress"] ."</td>";
        echo '<td>Venue Type:' . $row["venue_type"] ."</td>";
        echo '<td><a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a</td>';
        echo '</tr>';
    }
    echo '</table>'
}

?>

你可以在PHP中使用HTML,你可以使用回声(&#34;&#34;);。

另外我建议您使用PDO或MySQLi,因为MySQL现已弃用,这意味着它不安全。

答案 3 :(得分:0)

此代码应将您的数据输出到格式正确的表格中:

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );
?>
<table>
    <tr>
        <th>Venue Name</th>
        <th>Description</th>
        <th>Address</th>
        <th>Type</th>
        <th>Edit</th>
    </tr>
<?php
if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {

?>
    <tr>
        <td><?php echo $row["venue_name"]; ?></td>
        <td><?php echo $row["venue_description"]; ?></td>
        <td><?php echo $row["venue_address"]; ?></td>
        <td><?php echo $row["venue_type"]; ?></td>
        <td><?php echo '<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>'; ?></td>
    </tr>
<?php
        }
    }
?>
</table>

答案 4 :(得分:0)

我建议你附上这样的表格 它更容易和更清洁..加上组织良好

<?php

include ('connect.php');

$sql ="SELECT * FROM tbl_venues";
$res = mysql_query ($sql) or die( mysql_error() );
$result_table = "";
if ( mysql_num_rows($res) > 0 ) {
    while ( $row = mysql_fetch_assoc($res) ) {

//collect rows from database
$name = $row["venue_name"] ;
$v_desc = $row["venue_description"] ;
$v_add =$row["venue_adress"] ;
$v_type=$row["venue_type"] ;

//append table with results and echo the variables
$result_table .="<table><tr><td>$name</td>.    <td>$v_desc</td> <td>$v_add</td> <td>$v_type</td><td>'<a href="venueedit.php?venueid='.$row["venue_id"].'">Edit</a>'</td><tr></table>";
        }
    } Else { $result_table .= "No data found";}
?>

<html>
<body>
<div>
       <?php echo $result_table;  ?>
</div>
</body>
</html>