从sql转移到mysqli

时间:2015-03-30 05:20:26

标签: php mysql sql mysqli pagination

我有一个代码以表格形式显示数据以及分页,但代码是在mysql中

<?php
include('config.php');    //include of db config file
include ('paginate.php'); //include of paginat page

$per_page = 10;         // number of results to show per page
$result = mysql_query("SELECT * FROM subcategory");
$total_results = mysql_num_rows($result);

$row = mysql_fetch_assoc($result);

$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    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
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;

?>

<!doctype html>
<html lang="en">

<head>
</head>
<body>
<?php
    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

        // display data in table
        echo "<table class='tablesorter' cellspacing='0'> ";
            echo "<thead>
                        <tr> 
                            <th></th>
                            <th>Subcategory Name</th> 
                            <th>Category Name</th> 
                            <th>Edit</th> 
                            <th>Delete</th> 
                        </tr> 
                  </thead>";

                for ($i = $start; $i < $end; $i++) 
                    {
                        if ($i == $total_results) 
                            {
                                break;
                            }

                            // echo out the contents of each row into a table
                            echo "<tr " . $cls . ">";
                                echo '<td></td>';
                                echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';
                                echo '<td>' . mysql_result($result, $i, 'catname') . '</td>';    
                            echo "</tr>";
                    }       
                // close table>
        echo "</table>";
        // pagination

echo '<div class="pagination" style="margin-left:300px;" >';
        if ($total_pages > 1) 
            {
                echo paginate($reload, $show_page, $total_pages);
            }
    echo "</ul>
</div>";
?>
</body>

整个代码在同一页面上,现在我正在尝试将其转换为mysqli,并为表格中的每一行插入编辑和删除功能。

我的新代码是

<?php
include('config.php');    //include of db config file
include ('paginate.php'); //include of paginat page

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

$result = mysqli_query($con,"SELECT * FROM subcategory ");
$total_results = mysqli_num_rows($result);
$row = mysqli_fetch_assoc($result);
$total_pages = ceil($total_results / $per_page);//total pages we going to have

//-------------if page is setcheck------------------//
if (isset($_GET['page'])) {
    $show_page = $_GET['page'];             //it will telles the current page
    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
$page = intval($_GET['page']);

$tpages=$total_pages;
if ($page <= 0)
    $page = 1;

?>

<!doctype html>
<html lang="en">

<head>
</head>
<body>

<?php
    $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;

        // display data in table
        echo "<table class='tablesorter' cellspacing='0'> ";
            echo "<thead>
                        <tr> 
                            <th></th>
                            <th>Subcategory Name</th> 
                            <th>Category Name</th> 
                            <th>Edit</th> 
                            <th>Delete</th> 
                        </tr> 
                  </thead>";

                for ($i = $start; $i < $end; $i++) 
                    {
                        if ($i == $total_results) 
                            {
                                break;
                            }

                            // echo out the contents of each row into a table
                            echo "<tr " . $cls . ">";
                                echo '<td></td>';
                                echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';
                                echo '<td>' . mysql_result($result, $i, 'catname') . '</td>';

                                echo "<td><a href=\"a_edit_subcategory.php?id=".$row['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

                                echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$row['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

                            echo "</tr>";
                    }       
                // close table>
        echo "</table>";
        // pagination

echo '<div class="pagination" style="margin-left:300px;" >';
        if ($total_pages > 1) 
            {
                echo paginate($reload, $show_page, $total_pages);
            }
    echo "</ul>
</div>";
?>
</body>

但问题是我无法正确转换它,编辑和删除功能也无效,即

echo '<td>' . mysql_result($result, $i, 'subcatname') . '</td>';

echo "<td><a href=\"a_edit_subcategory.php?id=".$row['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$row['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

1 个答案:

答案 0 :(得分:0)

试试这个:

for ($i = $start; $i < $end; $i++) 
{
    if ($i == $total_results) 
        {
            break;
        }

        mysqli_data_seek($result, $i);

        $data = mysqli_fetch_assoc($result);

        // echo out the contents of each row into a table
        echo "<tr " . $cls . ">";
            echo '<td></td>';
            echo '<td>' . $data['subcatname'] . '</td>';
            echo '<td>' . $data['catname'] . '</td>';

            echo "<td><a href=\"a_edit_subcategory.php?id=".$data['id']."\"><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

            echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_subcategory.php?id=".$data['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";

        echo "</tr>";
}