2010-06-17 73 views
0

我正在一个真正的法兰克工作,而不是我自己的设计。有一个基本的CMS,其中一个页面显示来自MySQL DB的客户记录。这个PHP循环为什么每行渲染两次?

由于某些原因,它没有从数据库中提取数据的probs - 没有重复的记录 - 但它将每行渲染两次。

<?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--> 
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--> 

我不是世界上最好的PHP专家,但我认为我可以在for循环看到一个错误,当有一个......但是,一切都看起来不错给我。您会注意到客户名称是可点击的;点击可以进入另一个页面,您可以在DB中查看其完整信息 - 对于这两行,客户ID是相同的,并且手动检查数据库显示没有重复条目。代码肯定会渲染每行两次,但是由于什么原因我不知道。

所有的指针/建议表示赞赏。

+1

错误的网站bro,请尝试stackoverflow.com – JeremyWeir 2010-06-17 01:02:45

+1

$ customers_check如何填充?你有没有试过做一个简单的'var_dump($ customers_check)'来确保你没有重复? – Zoredache 2010-06-17 01:15:27

回答

0

请尝试以下操作,这是您的代码的更新版本。

请阅读我在其中提出的评论,这只是一些良好做法的示例。

<?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

像jayrub写道,StackOverflow可能更适合于此。

但既然你已经问了,我要让这些建议:

1)使用foreach()通过阵列看起来像这样:

foreach ($customers as $i => $customer) { 
    echo $customer['name']; 
    ... 
} 

2)使用var_dump()作为Zoredache建议在$customers$customers_check

3.)使用语义标记;所以,对于表,你会想是这样的:

<table> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Town</th> 
      <th>Phone</th> 
     </tr> 
    </thead> 
    <tbody> 
     <!-- your php code here --> 
    </tbody> 
</table> 

这将使它更容易为屏幕阅读器,并使其更容易使用某些脚本来产生丰富的网络接口。看到这个页面的更多信息:http://www.ferg.org/section508/accessible_tables.html

4.)哦,并尽量避免内联CSS。这只是不好的形式。

+0

绝对同意 - 我不会使用内联CSS,但我没有设计这个网站! (只是试图通过它来解决所有问题,并逐个解决它) 也注意到了语义标记建议,我总是试图在可能的情况下设计符合建议的可访问性标准。幸运的是,由于CMS目前仅由少数人内部使用,因此我对此特定设计并不十分关注。 – 2010-07-20 17:23:46