dongzg2006 2010-06-17 00:48
浏览 70
已采纳

为什么这个PHP循环每次渲染两次?

I'm working on a real frankensite here not of my own design. There's a rudimentary CMS and one of the pages shows customer records from a MySQL DB.

For some reason, it has no probs picking up the data from the DB - there's no duplicate records - but it renders each row twice.

<?php
$limit = 500;
$area = 'customers_list';
$prc = 'customer_list.php';

if($_GET['page'])
{
    include('inc/functions.php');
    $page = $_GET['page'];
}
else 
{
    $page = 1;
}

$limitvalue = $page * $limit - ($limit);

$customers_check = get_customers();
$customers = get_customers($limitvalue, $limit);
$totalrows = count($customers_check);

?>
<!-- pid: customer_list -->

<table border="0" width="100%" cellpadding="0" cellspacing="0" style="float: left; margin-bottom: 20px;">
    <tr>
        <td class="col_title" width="200">Name</td>
        <td></td>

        <td class="col_title" width="200">Town/City</td>
        <td></td>

        <td class="col_title">Telephone</td>

        <td></td>
    </tr>

    <?php
    for ($i = 0; $i < count($customers); $i++)
    {
    ?>
    <tr>
        <td colspan="2" class="cus_col_1"><a href="customer_details.php?id=<?php echo $customers[$i]['customer_id']; ?>"><?php echo $customers[$i]['surname'].', '.$customers[$i]['first_name']; ?></a></td>
        <td colspan="2" class="cus_col_2"><?php echo $customers[$i]['town']; ?></td>
        <td class="cus_col_1"><?php echo $customers[$i]['telephone']; ?></td>

        <td class="cus_col_2">
            <a href="javascript: single_execute('prc/customers.prc.php?delete=yes&id=<?php echo $customers[$i]['customer_id']; ?>')" onClick="return confirmdel();" class="btn_maroon_small" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_maroon_small_left">
                <div class="btn_maroon_small_right">Delete Account</div>
            </div></a>
            <a href="customer_edit.php?id=<?php echo $customers[$i]['customer_id']; ?>" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_black_left">
                <div class="btn_black_right">Edit Account</div>
            </div></a>
            <a href="mailto: <?php echo $customers[$i]['email']; ?>" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_black_left">
                <div class="btn_black_right">Email Customer</div>
            </div></a>
        </td>
    </tr>
    <tr><td class="col_divider" colspan="6"></td></tr>
    <?php
    };
    ?>
</table>

<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
<!--// PAGINATION-->
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
<div class="pagination_holder">

<?php
if($page != 1)
{
    $pageprev = $page-1;
?>
    <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $pageprev; ?>');" class="pagination_left">Previous</a>
<?php
}
else
{
?>
    <div class="pagination_left, page_grey">Previous</div>
<?php
}
?>
<div class="pagination_middle">
<?php
$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++)
{
    if($i == $page)
    {
    ?>
        <div class="page_number_selected"><?php echo $i; ?></div>
    <?php
    }
    else
    {
    ?>
        <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $i; ?>');" class="page_number"><?php echo $i; ?></a>
    <?php
    }
}

if(($totalrows % $limit) != 0)
{
    if($i == $page)
    {
    ?>
        <div class="page_number_selected"><?php echo $i; ?></div>
    <?php
    }
    else
    {
    ?>
        <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $i; ?>');" class="page_number"><?php echo $i; ?></a>
    <?php
    }
}
?>
</div>
<?php
if(($totalrows - ($limit * $page)) > 0)
{
    $pagenext = $page+1;
?>
    <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $pagenext; ?>');" class="pagination_right">Next</a>
<?php
}
else
{
?>
    <div class="pagination_right, page_grey">Next</div>
<?php
}
?>

</div>
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
<!--// END PAGINATION-->
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->

I'm not the world's best PHP expert but I think I can see an error in a for loop when there is one... But everything looks ok to me. You'll notice that the customer name is clickable; clicking takes you to another page where you can view their full info as held in the DB - and for both rows, the customer ID is identical, and manually checking the DB shows there's no duplicate entries. The code is definitely rendering each row twice, but for what reason I have no idea.

All pointers / advice appreciated.

  • 写回答

2条回答 默认 最新

  • dongtuji0992 2010-07-18 01:44
    关注

    Try the following, this is an updated version of your code.

    Please read the comments that I made in it, this is just a sample of some good practices.

    <?php
    $limit = 500;
    $area = 'customers_list';
    $prc = 'customer_list.php';
    
    if($_GET['page'])
    {
        include('inc/functions.php');
        $page = (int)$_GET['page']; // safety (page always needs to be an integer
    }
    else 
    {
        $page = 1;
    }
    
    $limitvalue = $page * $limit - ($limit);
    
    $customers_check = get_customers();
    $customers = get_customers($limitvalue, $limit);
    $totalrows = count($customers_check);
    
    ?>
    <!-- pid: customer_list -->
    
    <table border="0" width="100%" cellpadding="0" cellspacing="0" style="float: left; margin-bottom: 20px;">
        <tr>
            <td class="col_title" width="200">Name</td>
            <td></td>
    
            <td class="col_title" width="200">Town/City</td>
            <td></td>
    
            <td class="col_title">Telephone</td>
    
            <td></td>
        </tr>
    
        <?php
        foreach($customers as $i => $customer) // foreach is easier to use, manage and troubleshoot
        {
        ?>
        <tr>
            <td colspan="2" class="cus_col_1"><a href="customer_details.php?id=<?php echo $customer['customer_id']; ?>"><?php echo $customer['surname'].', '.$customer['first_name']; ?></a></td>
            <td colspan="2" class="cus_col_2"><?php echo $customer['town']; ?></td>
            <td class="cus_col_1"><?php echo $customer['telephone']; ?></td>
    
            <td class="cus_col_2">
                <a href="javascript: single_execute('prc/customers.prc.php?delete=yes&id=<?php echo $customer['customer_id']; ?>')" onClick="return confirmdel();" class="btn_maroon_small" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_maroon_small_left">
                    <div class="btn_maroon_small_right">Delete Account</div>
                </div></a>
                <a href="customer_edit.php?id=<?php echo $customer['customer_id']; ?>" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_black_left">
                    <div class="btn_black_right">Edit Account</div>
                </div></a>
                <a href="mailto: <?php echo $customer['email']; ?>" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"><div class="btn_black_left">
                    <div class="btn_black_right">Email Customer</div>
                </div></a>
            </td>
        </tr>
        <tr><td class="col_divider" colspan="6"></td></tr>
        <?php
        }
        ?>
    </table>
    
    <!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
    <!--// PAGINATION-->
    <!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
    <div class="pagination_holder">
    
    <?php
    if($page != 1)
    {
        $pageprev = $page-1;
    ?>
        <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $pageprev; ?>');" class="pagination_left">Previous</a>
    <?php
    }
    else
    {
    ?>
        <div class="pagination_left, page_grey">Previous</div>
    <?php
    }
    ?>
    <div class="pagination_middle">
    <?php
    $numofpages = $totalrows / $limit;
    if(($totalrows % $limit) != 0) // using this avoids the second for-loop (avoids redundant code)
      $numofpages++;
    
    for($i = 1; $i <= $numofpages; $i++)
    {
        if($i == $page)
        {
        ?>
            <div class="page_number_selected"><?php echo $i; ?></div>
        <?php
        }
        else
        {
        ?>
            <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $i; ?>');" class="page_number"><?php echo $i; ?></a>
        <?php
        }
    }
    
    ?>
    </div>
    <?php
    if(($totalrows - ($limit * $page)) > 0)
    {
        $pagenext = $page+1;
    ?>
        <a href="javascript: change('<?php echo $area; ?>', '<?php echo $prc; ?>?page=<?php echo $pagenext; ?>');" class="pagination_right">Next</a>
    <?php
    }
    else
    {
    ?>
        <div class="pagination_right, page_grey">Next</div>
    <?php
    }
    ?>
    
    </div>
    <!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
    <!--// END PAGINATION-->
    <!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable