在表格中显示用户

时间:2014-02-09 03:13:18

标签: php mysql mysqli

我正在关注基本php表的教程以向用户显示 http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

我遇到的问题是我的平台不支持MySQL所以我将其更改为MySQL

第二个问题是我在桌面上方显示的用户

这是一张更好描述的图片: http://store2.up-00.com/2014-02/1391915459381.jpg

这是我正在使用的代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('config.php');

        // number of results to show per page
        $per_page = 1;

        // figure out the total pages in the database
        $result = mysqli_query($connecDB,"SELECT * FROM users");
        $total_results = mysqli_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination

        echo "<p> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";

        // display data in table
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
                echo "<tr>";

                 while($row = mysqli_fetch_assoc($result)) {
                 $id = $row['id'];
                 $fname = $row['first_name']; 

                 echo  $id; 
                 echo $fname;


                 }

                   // echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
                   // echo '<td><a href="delete.php?id=' . mysqli_result($result, $i, 'id') . '">Delete</a></td>';
                echo "</tr>"; 
        }
        // close table>
        echo "</table>"; 

        // pagination

?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>

</body>
</html>

谢谢!

1 个答案:

答案 0 :(得分:0)

编辑#3的附录

如果您想稍后添加额外的单元格,请遵循相同的约定。

// echo out the contents of each row into a table
echo "<tr>";

while($row = mysqli_fetch_assoc($result)) {
    $id = $row['id'];
    $fname = $row['first_name']; 
    $lname = $row['last_name'];

    echo "<tr><td>"; // do not modify
    echo  $id; 
    echo "</td>"; // do not modify
    echo "<td>";
    echo $fname;
    echo "<td>";
    echo $lname;
    // there should not be anything below this
    // besides the echo "</td></tr>";
    echo "</td></tr>"; // do not modify
}

编辑3

我为$lname = $row['lname'];列和Last Name以及其他一些内容添加了echo $lname;。因此,您可能需要修改$lname = $row['lname'];以适合您的用途。

立即尝试:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('config.php');


        // number of results to show per page
        $per_page = 1;

        // figure out the total pages in the database
        $result = mysqli_query($db,"SELECT * FROM animals");
        $total_results = mysqli_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination

        echo "<p> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";

        // display data in table
        echo "<table border='1' cellpadding='10'>";
        // echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
                 echo "<tr>";

                 while($row = mysqli_fetch_assoc($result)) {
                 $id = $row['id'];
                 $fname = $row['name']; 
                 $lname = $row['lname'];

                  echo "<tr><td>";
                 echo  $id; 
                  echo "</td>"; 
                  echo "<td>";
                 echo $fname;
                  echo "<td>";
                 echo $lname;
                  echo "</td></tr>"; 

                 }

                   // echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
                   // echo '<td><a href="delete.php?id=' . mysqli_result($result, $i, 'id') . '">Delete</a></td>';
         echo "</td></tr>"; 
        }
        // close table>
        echo "</table>"; 

        // pagination

?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>

</body>
</html>

编辑2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('config.php');

        // number of results to show per page
        $per_page = 1;

        // figure out the total pages in the database
        $result = mysqli_query($connecDB,"SELECT * FROM users");
        $total_results = mysqli_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination

        echo "<p> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";

        // display data in table
        echo "<table border='1' cellpadding='10'>";
        // echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
                 echo "<tr>";

                 while($row = mysqli_fetch_assoc($result)) {
                 $id = $row['id'];
                 $fname = $row['first_name']; 

                 echo "<td>";
                 echo  $id; 
                 echo "</td>";
                 echo "<td>";
                 echo $fname;
                 echo "</td>"; 

                 }

                   // echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
                   // echo '<td><a href="delete.php?id=' . mysqli_result($result, $i, 'id') . '">Delete</a></td>';
         echo "</tr>"; 
        }
        // close table>
        echo "</table>"; 

        // pagination

?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>

</body>
</html>

编辑1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('config.php');

        // number of results to show per page
        $per_page = 1;

        // figure out the total pages in the database
        $result = mysqli_query($connecDB,"SELECT * FROM users");
        $total_results = mysqli_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination

        echo "<p> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";

        // display data in table
        echo "<table border='1' cellpadding='10'>";
        // echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
                 echo "<tr>";

                 while($row = mysqli_fetch_assoc($result)) {
                 $id = $row['id'];
                 $fname = $row['first_name']; 

                 echo "<td>";
                 echo  $id; 
                 echo $fname;
                 echo "</td>"; 

                 }

                   // echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
                   // echo '<td><a href="delete.php?id=' . mysqli_result($result, $i, 'id') . '">Delete</a></td>';
         echo "</tr>"; 
        }
        // close table>
        echo "</table>"; 

        // pagination

?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>

</body>
</html>

原始答案

请尝试以下完整代码。

我在<td>echo "<tr>";中添加</td> echo "</tr>";

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
        <title>View Records</title>
</head>
<body>

<?php
/* 
        VIEW-PAGINATED.PHP
        Displays all data from 'players' table
        This is a modified version of view.php that includes pagination
*/

        // connect to the database
        include('config.php');

        // number of results to show per page
        $per_page = 1;

        // figure out the total pages in the database
        $result = mysqli_query($connecDB,"SELECT * FROM users");
        $total_results = mysqli_num_rows($result);
        $total_pages = ceil($total_results / $per_page);

        // check if the 'page' variable is set in the URL (ex: view-paginated.php?page=1)
        if (isset($_GET['page']) && is_numeric($_GET['page']))
        {
                $show_page = $_GET['page'];

                // make sure the $show_page value is valid
                if ($show_page > 0 && $show_page <= $total_pages)
                {
                        $start = ($show_page -1) * $per_page;
                        $end = $start + $per_page; 
                }
                else
                {
                        // error - show first set of results
                        $start = 0;
                        $end = $per_page; 
                }               
        }
        else
        {
                // if page isn't set, show first set of results
                $start = 0;
                $end = $per_page; 
        }

        // display pagination

        echo "<p> | <b>View Page:</b> ";
        for ($i = 1; $i <= $total_pages; $i++)
        {
                echo "<a href='admin_user_list.php?page=$i'>$i</a> ";
        }
        echo "</p>";

        // display data in table
        echo "<table border='1' cellpadding='10'>";
        echo "<tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th></th> <th></th></tr>";

        // loop through results of database query, displaying them in the table 
        for ($i = $start; $i < $end; $i++)
        {
                // make sure that PHP doesn't try to show results that don't exist
                if ($i == $total_results) { break; }

                // echo out the contents of each row into a table
                echo "<tr><td>";

                 while($row = mysqli_fetch_assoc($result)) {
                 $id = $row['id'];
                 $fname = $row['first_name']; 

                 echo  $id; 
                 echo $fname;

                 }

                   // echo '<td><a href="edit.php?id=' . mysql_result($result, $i, 'id') . '">Edit</a></td>';
                   // echo '<td><a href="delete.php?id=' . mysqli_result($result, $i, 'id') . '">Delete</a></td>';
                echo "</td></tr>"; 
        }
        // close table>
        echo "</table>"; 

        // pagination

?>
<p><a href="admin/admin_user_add.php">Add a new record</a></p>

</body>
</html>