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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了