Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is this PHP loop rendering every row twice?
    text
    copied!<p>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.</p> <p>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.</p> <pre><code>&lt;?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); ?&gt; &lt;!-- pid: customer_list --&gt; &lt;table border="0" width="100%" cellpadding="0" cellspacing="0" style="float: left; margin-bottom: 20px;"&gt; &lt;tr&gt; &lt;td class="col_title" width="200"&gt;Name&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td class="col_title" width="200"&gt;Town/City&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td class="col_title"&gt;Telephone&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php for ($i = 0; $i &lt; count($customers); $i++) { ?&gt; &lt;tr&gt; &lt;td colspan="2" class="cus_col_1"&gt;&lt;a href="customer_details.php?id=&lt;?php echo $customers[$i]['customer_id']; ?&gt;"&gt;&lt;?php echo $customers[$i]['surname'].', '.$customers[$i]['first_name']; ?&gt;&lt;/a&gt;&lt;/td&gt; &lt;td colspan="2" class="cus_col_2"&gt;&lt;?php echo $customers[$i]['town']; ?&gt;&lt;/td&gt; &lt;td class="cus_col_1"&gt;&lt;?php echo $customers[$i]['telephone']; ?&gt;&lt;/td&gt; &lt;td class="cus_col_2"&gt; &lt;a href="javascript: single_execute('prc/customers.prc.php?delete=yes&amp;id=&lt;?php echo $customers[$i]['customer_id']; ?&gt;')" onClick="return confirmdel();" class="btn_maroon_small" style="margin: 0px; float: right; margin-right: 10px;"&gt;&lt;div class="btn_maroon_small_left"&gt; &lt;div class="btn_maroon_small_right"&gt;Delete Account&lt;/div&gt; &lt;/div&gt;&lt;/a&gt; &lt;a href="customer_edit.php?id=&lt;?php echo $customers[$i]['customer_id']; ?&gt;" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"&gt;&lt;div class="btn_black_left"&gt; &lt;div class="btn_black_right"&gt;Edit Account&lt;/div&gt; &lt;/div&gt;&lt;/a&gt; &lt;a href="mailto: &lt;?php echo $customers[$i]['email']; ?&gt;" class="btn_black" style="margin: 0px; float: right; margin-right: 10px;"&gt;&lt;div class="btn_black_left"&gt; &lt;div class="btn_black_right"&gt;Email Customer&lt;/div&gt; &lt;/div&gt;&lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td class="col_divider" colspan="6"&gt;&lt;/td&gt;&lt;/tr&gt; &lt;?php }; ?&gt; &lt;/table&gt; &lt;!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--&gt; &lt;!--// PAGINATION--&gt; &lt;!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--&gt; &lt;div class="pagination_holder"&gt; &lt;?php if($page != 1) { $pageprev = $page-1; ?&gt; &lt;a href="javascript: change('&lt;?php echo $area; ?&gt;', '&lt;?php echo $prc; ?&gt;?page=&lt;?php echo $pageprev; ?&gt;');" class="pagination_left"&gt;Previous&lt;/a&gt; &lt;?php } else { ?&gt; &lt;div class="pagination_left, page_grey"&gt;Previous&lt;/div&gt; &lt;?php } ?&gt; &lt;div class="pagination_middle"&gt; &lt;?php $numofpages = $totalrows / $limit; for($i = 1; $i &lt;= $numofpages; $i++) { if($i == $page) { ?&gt; &lt;div class="page_number_selected"&gt;&lt;?php echo $i; ?&gt;&lt;/div&gt; &lt;?php } else { ?&gt; &lt;a href="javascript: change('&lt;?php echo $area; ?&gt;', '&lt;?php echo $prc; ?&gt;?page=&lt;?php echo $i; ?&gt;');" class="page_number"&gt;&lt;?php echo $i; ?&gt;&lt;/a&gt; &lt;?php } } if(($totalrows % $limit) != 0) { if($i == $page) { ?&gt; &lt;div class="page_number_selected"&gt;&lt;?php echo $i; ?&gt;&lt;/div&gt; &lt;?php } else { ?&gt; &lt;a href="javascript: change('&lt;?php echo $area; ?&gt;', '&lt;?php echo $prc; ?&gt;?page=&lt;?php echo $i; ?&gt;');" class="page_number"&gt;&lt;?php echo $i; ?&gt;&lt;/a&gt; &lt;?php } } ?&gt; &lt;/div&gt; &lt;?php if(($totalrows - ($limit * $page)) &gt; 0) { $pagenext = $page+1; ?&gt; &lt;a href="javascript: change('&lt;?php echo $area; ?&gt;', '&lt;?php echo $prc; ?&gt;?page=&lt;?php echo $pagenext; ?&gt;');" class="pagination_right"&gt;Next&lt;/a&gt; &lt;?php } else { ?&gt; &lt;div class="pagination_right, page_grey"&gt;Next&lt;/div&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--&gt; &lt;!--// END PAGINATION--&gt; &lt;!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////--&gt; </code></pre> <p>I'm not the world's best PHP expert but I think I can see an error in a <code>for</code> 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.</p> <p>All pointers / advice appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload