Why is this PHP loop rendering every row twice? - mysql

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"><?php echo $customers[$i]['surname'].', '.$customers[$i]['first_name']; ?></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;
?>
Previous
<?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
{
?>
<?php echo $i; ?>
<?php
}
}
if(($totalrows % $limit) != 0)
{
if($i == $page)
{
?>
<div class="page_number_selected"><?php echo $i; ?></div>
<?php
}
else
{
?>
<?php echo $i; ?>
<?php
}
}
?>
</div>
<?php
if(($totalrows - ($limit * $page)) > 0)
{
$pagenext = $page+1;
?>
Next
<?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.

Like jayrub wrote, StackOverflow is probably more appropriate for this.
But since you've already asked it, I'll make these recommendations:
1.) Use foreach() to look through arrays like this:
foreach ($customers as $i => $customer) {
echo $customer['name'];
...
}
2.) Use var_dump() as Zoredache suggests, on $customers and $customers_check.
3.) Use semantic markup; so, for tables, you'll want something like:
<table>
<thead>
<tr>
<th>Name</th>
<th>Town</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<!-- your php code here -->
</tbody>
</table>
This will make it easier for screen readers and also make it easier to use certain scripts to generate rich web interfaces. See this page for more info: http://www.ferg.org/section508/accessible_tables.html
4.) Oh, and avoid inline CSS as much as possible. It's just bad form.

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"><?php echo $customer['surname'].', '.$customer['first_name']; ?></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;
?>
Previous
<?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
{
?>
<?php echo $i; ?>
<?php
}
}
?>
</div>
<?php
if(($totalrows - ($limit * $page)) > 0)
{
$pagenext = $page+1;
?>
Next
<?php
}
else
{
?>
<div class="pagination_right, page_grey">Next</div>
<?php
}
?>
</div>
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->
<!--// END PAGINATION-->
<!--///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////-->

Related

i want a array of queries in a single model function with pagination in Codeigniter

This is my model
I dont know it is correct or not. i have a view page with pagination. i have to show a field of count with new column. so i have to write array of queries in a single model funtion.please help. please tell how it correctly call in controller
Model
public function fetch_data($limit, $start) {
$query1= $this->db->limit($limit, $start);
$this->db->from('jil_requirements');
$this->db->join('jil_users', 'jil_requirements.rqm_userid=jil_users.usr_id', 'left');
$this->db->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left');
$this->db->where('jil_requirements.rqm_permission!=', '4');
$this->db->get();
$query2= $this->db->select('count(*)');
$this->db->from('jil_mrorfq');
$this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_mrorfq.rfq_requirementid', 'left');
$this->db->get();
return array(
'categories' => $query1,
'count' => $query2,
);
} public function fetch_data($limit, $start) {
$query1= $this->db->limit($limit, $start);
$this->db->from('jil_requirements');
$this->db->join('jil_users', 'jil_requirements.rqm_userid=jil_users.usr_id', 'left');
$this->db->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left');
$this->db->where('jil_requirements.rqm_permission!=', '4');
$this->db->get();
$query2= $this->db->select('count(*)');
$this->db->from('jil_mrorfq');
$this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_mrorfq.rfq_requirementid', 'left');
$this->db->get();
return array(
'categories' => $query1,
'count' => $query2,
);
}
Controller
public function managerequirement() {
$this->load->helper(array('form', 'url'));
$this->load->view('moderator/templates/header');
$this->load->view('moderator/templates/sidebar');
$this->load->library('pagination');
$config = array();
$config["base_url"] = base_url() . "moderator/Requirement/managerequirement";
$config["total_rows"] = $this->requirement_model->record_count();
$config["per_page"] = 20;
$config["uri_segment"] = 4;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = 'first';
$config['last_link'] = 'last';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '&laquo';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '&raquo';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) :0;
$query= $this->requirement_model->
fetch_data($config["per_page"], $page);
$data['results'] = $query['categories'];
$data['count'] = $query['count'];
print_r($data['results']);
print_r( $data['count']);
$data["links"] = $this->pagination->create_links();
$this->load->view('moderator/managerequirement', $data);
$this->load->view('moderator/templates/footer');
}
View
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Dashboard) -->
<section class="content-header">
<h1>
Requirement Management
<small></small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Dashboard</li>
<li class="active">Requirements</li>
<li class="active">View All Requirements</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box box-primary">
<div class="box-header">
<h3 class="box-title">View all Requirements</h3> <div style="float:right"><?php echo $links; ?> </div>
</div><!-- /.box-header -->
<!-- form start -->
<div class="box-body">
<div class="row">
<div class="col-xs-12">
<table class="table table-bordered table-hover table-striped">
<tr role="row">
<th class="sorting" width="5%">#</th>
<th class="sorting" width="30%">Requirement Service</th>
<th class="sorting" width="10%">Posted By</th>
<th class="sorting" width="10%">Managed By</th>
<th class="sorting" width="15%">Quantity</th>
<th class="sorting" width="15%">Posted On</th>
<th class="sorting" width="15%">Status</th>
</tr>
<?php
if (!empty($results)) {
foreach ($results as $row) {
?><tr>
<td class=" "><?php echo $row->rqm_id; ?></td>
<td class=" "> <?php echo $row->rqm_service; ?></td>
<td class=" "><?php
echo $row->usr_name;
?></td>
<td class=" "><?php
echo $row->mer_name;
?></td>
<td class=" "><?php
echo $row->rqm_quantity;
?></td>
<td class=" "><?php echo date('d-M-Y', $row->rqm_dated); ?></td>
<td class=" "><?php
if ($row->rqm_permission == '0') {
echo "In-Active";
} else if ($row->rqm_permission == '1') {
echo "Active";
} else if ($row->rqm_permission == '2') {
echo "Pending";
} else if ($row->rqm_permission == '3') {
echo "Suspend";
} else if ($row->rqm_permission == '4') {
echo "Delete";
}
?></td>
</tr>
<?php
}
}
?>
</table>
<?php echo $links; ?>
</div>
<div class="col-xs-6">
</div>
</div>
</diV>
</form>
</div><!-- /.box-body -->
</div>
</div>
</section>
<!-- /.content -->
</div><!-- /.content-wrapper -->
Try this coding
public function fetch_data($limit, $start) {
$this->db->limit($limit, $start);
$this->db->from('jil_requirements');
$this->db->join('jil_users', 'jil_requirements.rqm_userid=jil_users.usr_id', 'left');
$this->db->join('jil_merchants', 'jil_requirements.rqm_createdempid=jil_merchants.mer_id', 'left');
$this->db->where('jil_requirements.rqm_permission!=', '4');
$get_query1 = $this->db->get();
$query1 = $get_query1->result();
$this->db->select('rfq_requirementid');
$this->db->from('jil_mrorfq');
$this->db->join('jil_requirements', 'jil_requirements.rqm_id=jil_mrorfq.rfq_requirementid', 'left');
$get_query2 = $this->db->get();
$field_count = $get_query2->num_rows();
return array(
'categories' => $query1,
'count' =>$field_count
);
}

I cannot apply CSS in html table

I cannot apply the CSS in the table. For example I want the table to be displayed in the middle and to change the font color and size as well. But I can't. Should I use the tbody tag since I have the body one? Also I generate the table data from database.
<?php
include 'connect.php';
$year = $_POST['year'];
$lecturer = $_POST['lecturer'];
$years = array(
2005,
2006,
2007
);
$lecturers = array(
'lec1',
'lec2',
'lec3',
'lec4'
);
if (in_array($lecturer, $lecturers) && in_array($year, $years)) {
$sql = "SELECT unit_name,a1,a2,a3,l1,l2,r1,r2,u1,u2,u3 FROM $lecturer WHERE year=$year";
$result = mysql_query($sql);
}
else {
echo "No data found";
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../../statistics/style.css">
</head>
<body>
<div id="mytable">
<table width="900" border="1" cellspacing="1">
<tr>
<tbody>
<td>Unit Name</td>
<td>A1 </td>
<td>A2 </td>
<td>A3 </td>
<td>L1 </td>
<td>L2 </td>
<td>R1 </td>
<td>R2 </td>
<td>U1 </td>
<td>U2 </td>
<td>U3 </td>
</tbody>
</tr>
<?php
while($unit=mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$unit['unit_name']."</td>";
echo "<td>".$unit['a1']."</td>";
echo "<td>".$unit['a2']."</td>";
echo "<td>".$unit['a3']."</td>";
echo "<td>".$unit['l1']."</td>";
echo "<td>".$unit['l2']."</td>";
echo "<td>".$unit['r1']."</td>";
echo "<td>".$unit['r2']."</td>";
echo "<td>".$unit['u1']."</td>";
echo "<td>".$unit['u2']."</td>";
echo "<td>".$unit['u3']."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
css:
<!-- principalLecturers
-->
#body{
color:white;
padding-bottom: 20px;
}
#table{
margin:0;
border-collapse: collapse;
color:#b50a1e;
font-family:verdana,arial,sans-serif;
font-size:10px;
}
#table#mytable{
display: table-row-group;
vertical-align: middle;
border-color: inherit
}
I can't see anywhere in your table <table id="table"...>. That's why the styles for #table {...} does not apply.

Google Map not fully load in OSClass

I'm having following codes in osclass\oc-content\themes\modern\item.php,
<strong class="share"><?php _e('Map', 'modern'); ?></strong>
<strong class="share"><?php _e('Photo Gallery', 'modern'); ?></strong>
<div class="map" style="display:none;">
<?php osc_run_hook('location'); ?>
</div>
<div class="slider-wrapper theme-light image" >
<div id="slider" class="nivoSlider">
<?php if( osc_images_enabled_at_items() ) { ?>
<?php if( osc_count_item_resources() > 0 ) { ?>
<?php for ( $i = 0; osc_has_item_resources(); $i++ ) { if (osc_resource_for() == 2 ) { ?>
<img src="<?php echo osc_resource_url(); ?>" width="100%" height="240px;" alt="<?php echo osc_item_title(); ?>" title="<?php echo osc_item_title(); ?>" />
<?php } } ?>
<?php } ?>
<?php } osc_reset_resources(); ?>
</div>
</div>
and JavaScript code is,
$("#photo").click(function() {
$(".image").css("display","block");
$(".map").css("display","none");
});
$("#map").click(function() {
$(".map").css("display","block");
$(".image").css("display","none");
});
here photo is loading as good. But map makes following problem when I click #map.
If I load map at 1st and set display:none; to .image means, map will be loaded as fully and good.
Where is the problem?
I just tested version 3.1 and it's working on my localhost

Boxes are tripping if I insert text in them

* RESOLVED BY INTENSE RESEARCHING *
[DOWNLOAD CODE, NO COPYRIGHT =)] .rar file ( always scan before opening .rar - F4LLCON
Problem is that without text the boxes are placed 2 on each row, but with text the boxes
appear under each other with spacing. To get the text in the boxes I used <style> in <head>.
How can I FIX this annoying problem?
IMAGE:
Without the p and h3, both echo text will appear under the box and not in the box
Code:
<style>
p {
position:relative;
top:-240px;
left:180px;
}
h3 {
position:relative;
top:-270px;
left:30px;
}
</style>
and
<div class="offers">
<div class="content_box">
<?php
while($products = mysql_fetch_array($result)) {
echo '<img src="includes/images/content_box.png" border=0/>';
// echo "<h3>" . $products['products'] . "</h3>";
// echo "<p>" . $products['description'] . "</p>";
}
?>
</div>
</div>
RESOLVED BY DOING:
<STYLE TYPE="text/css">
#title{color:red; padding-bottom: 240px; padding-left: 25px;}
#desc{color:blue; padding-bottom: 135px; padding-left: 5px;}
</STYLE>
</head>
and
<div>
<?php while($products = mysql_fetch_array($result)) {?>
<table align="left" background="includes/images/box.gif" width="473" height="285">
<tr>
<td width="35%" height="100%" id="title">
<?php echo $products['products'] . "&nbsp"; ?>
</td>
<td width="70%" height="100%" id="desc">
<?php echo $products['description'];?>
</td>
</tr>
</table>
<?php
}
?>
</div>
</body>
This might be easy for some, but I'm new to this.
Now working correctly.
Thank you for giving me directions and Thank you Google and Stackoverflow for so much help =)
I am asuming that content_box.png is the background for the box right?
instead of including it in a create it in the background of a div with a class like this
CSS:
.product_box {
bacground:url(includes/images/content_box.png);
width:xxxpx; /* the width of conrent_box.png */
height:xxxpx; /* the height of conrent_box.png */
position:relative;
float:left;
}
php:
<?php
while($products = mysql_fetch_array($result)) {
echo '<div class="product_box">';
// echo '<img src="includes/images/content_box.png" border=0/>';
echo "<h3>" . $products['products'] . "</h3>";
echo "<p>" . $products['description'] . "</p>";
echo '</div>';
}
?>
Hope it helps!

Print CSS - Avoid cutting DIV's

I am developing an application which generates barcodes, I want to print them but the barcode div is getting cutted in some pages.
CSS:
<style type="text/css">
* {
margin:0px;
}
#cod {
width: 180px;
height: 150px;
padding: 10px;
margin: 10px;
float: left;
border: solid black 1px;
text-align: center;
display: block;
}
</style>
Code:
<? foreach ($this->ouvintes as $ouvinte): ?>
<div id="cod">
<p><?= $ouvinte->nome ?></p>
<p><?= $ouvinte->instituicao ?></p>
<p>Cod. Barras: <?= $ouvinte->codigo_barras ?></p>
<p style="margin-top:5px;"><img src="<?= $this->baseUrl('/index/codigo-barras/code/' . $ouvinte->codigo_barras) ?>" alt="<?= $ouvinte->codigo_barras ?>" /></p>
</div>
<? endforeach; ?>
Does anyone know how could i avoid this cut?
Thanks
How about trying the page-break-after / page-break-before CSS properties?
You could set it up to break after every 9 barcodes like this:
<?
$i = 0;
foreach ($this->ouvintes as $ouvinte):
$i++;
$pageBreakStyle = ($i % 9 == 0) ? ' style="page-break-after:always"' : '';
?>
<div id="cod"<?= $pageBreakStyle ?>>
<p><?= $ouvinte->nome ?></p>
<p><?= $ouvinte->instituicao ?></p>
<p>Cod. Barras: <?= $ouvinte->codigo_barras ?></p>
<p style="margin-top:5px;"><img src="<?= $this->baseUrl('/index/codigo-barras/code/' . $ouvinte->codigo_barras) ?>" alt="<?= $ouvinte->codigo_barras ?>" /></p>
</div>
<? endforeach; ?>
I think the problem is in float:left.. this is a big problem, but you can find some tricks here
I ran into a similar problem... the (only) working solution is to place the barcodes in the table... because multi-paged content really only works (without cutting the content or other problems) with tables.
Try something like this:
<table>
<? foreach ($this->ouvintes as $ouvinte): ?>
<tr id="cod">
<td><?= $ouvinte->nome ?></td>
<td><?= $ouvinte->instituicao ?></td>
<td>Cod. Barras: <?= $ouvinte->codigo_barras ?></td>
<td style="margin-top:5px;"><img src="<?= $this->baseUrl('/index/codigo-barras/code/' . $ouvinte->codigo_barras) ?>" alt="<?= $ouvinte->codigo_barras ?>" /></td>
</tr>
<? endforeach; ?>
</table>
Try setting page-break-inside to avoid:
div {
page-break-inside: avoid;
}