Why is this PHP loop rendering every row twice?

Posted by Christopher on Server Fault See other posts from Server Fault or by Christopher
Published on 2010-06-17T00:48:46Z Indexed on 2010/06/17 0:53 UTC
Read the original article Hit count: 638

Filed under:
|
|
|

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.

The page PHP is viewable at http://christopher.pastebin.com/DQkjjG3s (attempted to include in this post but it was horribly mangled, think it's important to have it all in context).

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.

© Server Fault or respective owner

Why is this PHP loop rendering every row twice?

Posted by Christopher on Stack Overflow See other posts from Stack Overflow or by Christopher
Published on 2010-06-17T00:48:46Z Indexed on 2010/06/17 3:13 UTC
Read the original article Hit count: 638

Filed under:
|
|
|

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.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php