Put dropdown box and image link in table

人走茶凉 提交于 2019-12-12 03:07:33

问题


    <html>
        <style>
            table {
                border-collapse: collapse;
            }

            table,th,td{
                border: 1px solid black;
                border-collapse: collapse;
            }
            tr:hover {background-color: #f5f5f5}
            th,tr {
                text-align: center;
            }
            table {
                margin-left: auto;
                margin-right: auto;
                width: 75%;
            }
            th, td {
                padding: 15px;
            }
        </style>
    </html>
    <?php
include 'dbLink.php';

include 'navi.php';


$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";

$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>

How do I input a drop down list and a image link in each table row? Also, when i run this page, only the data in the first row is reflected in the table. The rest of the data is not in the table. Any fixes?

    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?php 
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
        {?>
        <tr>
                    <td><?php echo $row["id"];?></td>
                    <td><?php echo $row["first_name"];?></td>
                    <td><?php echo $row["last_name"];?></td>
                    <td><?php echo $row["username"];?></td>
                    <td><?php echo $row["role"];?></td>
                    <td>//dropdwon list with data from employeerole table</td>
                    <td>//image link</td>
                </tr>
                 </table>
    <?php}?>

<?php 
}
}
else
{
    echo "0 results";
}
?>

Screenshot


回答1:


You have mixed Object Oriented Style & Procedural Style for writing query. So, be particular about Object Oriented or Procedural way of writing query.

I've written a code for you in both way. Go Ahead.

1) Object Oriented Style

dbLink.php

<?
$link = new mysqli("localhost", "my_user", "my_password", "world");
?>

Edited Code

$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";

$result = $link->query($sql);
$result1 = $link->query($query);

$rowcount = $result->num_rows;

if ($rowcount > 0) 
{?>
    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?php 
    while ($row = $result->fetch_array(MYSQLI_ASSOC)) 
        {?>
        <tr>
                    <td><?php echo $row["id"];?></td>
                    <td><?php echo $row["first_name"];?></td>
                    <td><?php echo $row["last_name"];?></td>
                    <td><?php echo $row["username"];?></td>
                    <td><?php echo $row["role"];?></td>
                    <td></td>
                    <td></td>
                </tr>
    <?php}?>
    </table>
<?php } 

else
{
    echo "0 results";
}
?>

2) Procedural Style

dbLink.php

<?
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
?>

Edited Code

$sql = "SELECT * FROM employee";
$query = "SELECT * FROM employeerole ORDER BY role_id";

$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>
    <table>
            <tr>
                <th>Employee ID</th>
                <th>First name</th>
                <th>Last Name</th>
                <th>Username</th>
                <th>Employee Role</th>
                <th>Edit role</th>
                <th>Delete User</th>
            </tr>
        <?php 
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
        {?>
        <tr>
                    <td><?php echo $row["id"];?></td>
                    <td><?php echo $row["first_name"];?></td>
                    <td><?php echo $row["last_name"];?></td>
                    <td><?php echo $row["username"];?></td>
                    <td><?php echo $row["role"];?></td>
                    <td></td>
                    <td></td>
                </tr>
    <?php }?>
    </table>
<?php } 

else
{
    echo "0 results";
}
?>

User's Requirement : Updated Code

<?
$result = mysqli_query($link, $sql);
$result1 = mysqli_query($link, $query);

$rowcount = mysqli_num_rows($result);

if ($rowcount > 0) 
{?>
    <table>
        <tr>
            <th>Employee ID</th>
            <th>First name</th>
            <th>Last Name</th>
            <th>Username</th>
            <th>Employee Role</th>
            <th>Edit role</th>
            <th>Delete User</th>
        </tr>
        <?php 
            while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) 
        {?>
        <tr>
            <td><?php echo $row["id"];?></td>
            <td><?php echo $row["first_name"];?></td>
            <td><?php echo $row["last_name"];?></td>
            <td><?php echo $row["username"];?></td>
            <td><?php echo $row["role"];?></td>
            <td>//dropdwon list with data from employeerole table</td>
            <td>//image link</td>
        </tr>
            <?php }?>
        </table>
<?php 
} else {
    echo "0 results";
}
?>


来源:https://stackoverflow.com/questions/34396357/put-dropdown-box-and-image-link-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!