Display 5 array items per row in a HTML table - php

I have an array of products and I need to display them 5 items per row. How can I make it, because now if i do
<?php foreach($data as $entries) : ?>
<td><?php echo $entries->name; ?>
<?php endforeach; ?>
it doesn't work. Should I make a counter?

Something like:
$data = range(1, 17);
for($count = 0; $count < count($data);)
{
echo "<tr>\n";
for($i = 0; $count < count($data) && $i < 5; $count++, $i++) {
echo "\t<td>$data[$count]</td>\n";
}
for(; $i < 5; $i++) {
echo "\t<td>-</td>\n";
}
echo "</tr>\n";
}
Output
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
</tr>
<tr>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
</tr>
<tr>
<td>16</td>
<td>17</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>

Perhaps a bit more readable?
<table>
<tr>
<?php
$data = range(1, 17);
$counter = 1;
foreach($data as $value){
if (!(($counter++ ) % 5)){
echo "<td>$value</td></tr><tr>";
}else{
echo "<td>$value</td>";
}
}
?>
</table>
Produces :
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17

Hope this helps
echo"<table>";
echo"<tr>";
for($i = 0; $i < count($productsArray); $i++){
if($i % 4 ==0){
echo"</tr><tr>"
}
echo "<td>$products[$i]; </td>";
}

Here's a simple counter that's also easy to modify if layout requirements change
<?php
//initialize a counter
$count = 0;
foreach($data as $entries){
//if it's divisible by 5 then echo a new row
if($count > 0 && $count % 5 == 0){
echo("</tr><tr>\n");
}
$count++;
?>
<td>
<?php
// We're also creating a new cell for each item
echo $entries->name; ?>
</td>
<?php
}
?>

<div class="container-fluid">
<?php
$count = 1;
foreach ($home_products as $pro_img) {
$parent_cat_link = base_url('category/view/'.$pro_img['category_id']);
if ($count % 6 == 1) {
$class="cap1";
$class1="cap";
?>
<div class="row">
<?php }
if($i %2 == 0){?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class; } else{ echo 'cap'; } ?> ">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php }else{ ?>
<div class="col-md-2 col-sm-4 <?php if($count >=6){ echo $class1; } else{ echo 'cap1'; } ?>">
<div class="">
<img src="<?php echo base_url() . 'uploads/category/' . $pro_img['image_name'] ?>" class="img-responsive" alt="logo">
</div>
</div><?php } $i++;
if ($count % 6 == 0) {
?> </div> <?php } $count++;
} if ($count % 6 != 1) echo "</div>"; ?>
</div>

Related

Give Unique Class to each table row in loop

I have an PHP for loop; which looks at a number in database and outputs the tables rows to number of times.
Here is my HTML:
<?php
for($x=0; $x < 5; $x++){
$row_count =1;
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="yellow-sign" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>
The above code display this:
How I can assign unique class to each of the rows?
Labelled Image
Example:
<?php foreach($data as $row) { ?>
<tr id="data<?php echo $row['id']; ?>"> </tr>
<?php } ?>
I hope it will help you.
If you are trying to get unique class to tr then do this
for ($x = 0; $x < 5; $x++) {
$row_count = 1;
$randClass = rand(1111,9999).$row_count;
?>
<table class="table table-bordered" id="ticktable">
<tr class="<?php echo $randClass; ?>">
<?php
for ($w = 0; $w < 5; $w++) {
?>
<td>
<img class="yellow-sign <?php echo $randClass; ?>" src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count++;
}
rand(1111,9999) will generate random number between 1111 and 9999
EDIT
i have also added $row_count so that the number can not be repeated even if there is large data. What I've done is: <tr class="<?php echo rand(1111,9999).$row_count; ?>">
UPDATE
first store the random string in a variable $randClass = rand(1111,9999).$row_count;
then echo out where you want the variable like this:
$randClass = rand(1111,9999).$row_count;
and <img class="yellow-sign <?php echo $randClass; ?>" >
Here is the updated code for you. Check this
<?php
$color_class[] = array("yellow-sign","red-sign","green-sign","blue-sign","black-sign");
$row_count = 0;
for($x=0; $x < 5; $x++){
?>
<table class="table table-bordered" id="ticktable">
<tr class=" <?php echo $row_count; ?>">
<?php
for($w=0; $w < 5; $w++){
?>
<td>
<img class="<?php echo $color_class[$row_count]; ?>"
src="http://www.backgroundsy.com/file/large/yellow-sign.jpg" width="100">
</td>
<?php
}
?>
</tr>
</table>
<?php
$row_count ++;
}
?>

PHP looping in table

I'm trying create loop in my table, there is 4 item, when column is 3 then create new row . The current output is like this:
x
x
x
x
Here's my code:
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if ($i <= 3) { ?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php }
}
echo "</tr>";
?>
</table>
What i expected is like this:
x|x|x
x
Thank you.
In the comment section of your question, Sirko is right.
Anyway you can do this like below;
<?php
$i = 0;
foreach ($list_items as $item) {
if($i % 3 == 0)
echo '<tr>';
echo '<td> bla bla bla </td>';
if($i % 3 == 0)
echo '</tr>';
$i++;
}
Change your code to below, it should work.
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item){ // there is 4 item
$i++;
echo "<tr>";
if($i%3==0) echo echo "</tr><tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
</td>
<td>
<?php echo $item['qty'] ?>
</td>
<?php
}
if($i%3!=0)
echo "</tr>";
?>
</table>
use array_chunk()
<?php
foreach (array_chunk($list_items,3) as $items) {
echo '<tr>';
foreach($items as $item){
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
}
echo '</tr>';
}
?>
try this ==>
<table border="0">
<?php
$i = 0;
foreach ($list_items as $item) { // there is 4 item
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will open
echo "<tr>";
?>
<td class="text-center" style="width:83.14px; height:60.47px; font-size:0.6em">
<?php echo $item['productId'] ?>
<br>
<br>
<?php echo $item['qty'] ?>
</td>
<?php
if ($i % 3 == 0) // for i=0,3,6,9 <tr> tag will close
echo "</tr>";
$i++;
}
?>
</table>

Table Province District and Subdistrict with PHP

I have table Province District and Subdistrict in my database and i want to print as output in html table as below:
but i get this:
here are my code:
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
foreach ($QueryDistricts as $QueryDistrict ) {
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
Please help me,
Thanks a lot.
You need to close and then open a new row with each loop of foreach ($QueryDistricts as $QueryDistrict ). Try something like this -
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
$i = 0; // simple counter
foreach ($QueryDistricts as $QueryDistrict ) {
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td> </td>
<td> </td>
<?php
}
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
</tr>
<?php
$i++;
}
}
?>
Table Result:
html source code:
Complete code:
<table id="example1" class="table table-bordered table-condensed table-hover">
<thead>
<tr>
<th>No.</th>
<th width="20%">Provinsi</th>
<th>Kabupaten/Kotamadya</th>
<th>Kecamatan</th>
<th>Pendamping</th>
<th>Telepon</th>
<th>Email</th>
<th>Status Laporan</th>
</tr>
</thead>
<tbody>
<?php
$thecolor = array ("olive", "navy", "aqua", "maroon", "green", "yellow", "orange", "purple", "light-blue", "red");
//$infLokasis = Query;
$nomorProv = 1;
$thecolorProv = $thecolor;
$colorProv = 0;
foreach ($infLokasis as $infLokasi) {
# code... cetak nama provinsi
if ($colorProv >= 10) {
# code... warna set
$colorProv = 0;
}
?>
<tr>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $nomorProv.". ";?></strong></td>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $infLokasi->lokasi_nama;?></strong></td>
<?php
$i = 0; // simple counter
//$infKabkotas = Query;
$nomorKabkota = 1;
$thecolorKabkota = $thecolor;
$colorKabkota = 0;
foreach ($infKabkotas as $infKabkota) {
# code... cetak nama kabupaten
if ($colorKabkota >= 10) {
# code... warna set
$colorKabkota = 0;
}
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKabkota[$colorKabkota];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$infKabkota->lokasi_nama; ?></span></td>
<?php
$j = 0;
$infKecamatans = Query;
$nomorKecamatan = 1;
$thecolorKecamatan = $thecolor;
$colorKecamatan = 0;
foreach ($infKecamatans as $infKecamatan) {
if ($colorKecamatan >= 10) {
# code... warna set
$colorKecamatan = 0;
}
if($j>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKecamatan[$colorKecamatan];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$nomorKecamatan.". ".$infKecamatan->lokasi_nama; ?></strong></td>
<?php
$k = 0;
$idDaerah = substr($infKecamatan->lokasi_kode, 0,10);
$infPendampings = $db->get_results("SELECT id_p, nama_p, telepon_p, email_p FROM pendamping WHERE id_d LIKE '$idDaerah%' ");
$nomorPendamping = 1;
$thecolorPendamping = $thecolor;
$colorPendamping = 0;
if ($infPendampings) {
foreach ($infPendampings as $infPendamping) {
if($k>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td>
<?php echo "<span class=\"text-".$thecolorPendamping[$colorPendamping]." small\">".$nomorPendamping.". "; ?></span>
<?php echo "<a href=\"vpendamping.php?str=".encryptor('encrypt', $infPendamping->id_p)."\" class=\"text-".$thecolorPendamping[$colorPendamping]." small\" target=\"_blank\">".namaGelar($infPendamping->nama_p); ?>
</td>
<td class="small"><?php echo $infPendamping->telepon_p; ?></td>
<td class="small"><?php echo $infPendamping->email_p; ?></td>
<td class="small"><?php echo "<i class=\"fa fa-fw fa-square-o text-red\"></i> Kosong"; ?></td>
<?php
$nomorPendamping++;
$colorPendamping++;
$k++;
}
}
else {
# code... row kosong
#if i romove 4 line code below... Provinsi name and Kabupaten/Kotamadya name should appear
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
}
?>
</tr>
<?php
$nomorKecamatan++;
$colorKecamatan++;
$j++;
}
?>
<?php
$i++;
$colorKabkota++;
$nomorKabkota++;
} // End infKabkota
$nomorProv++;
$colorProv++;
}
?>
</tbody>
</table>

Displaying number of records in continuous fashion in php paging

I have a page which displays number of records on each page. I am displaying 5 records on each page and then next 5 on the next page and so on. Paging is working fine but the problem is on first page I'm displaying number serial wise next to each record i.e. from 1 to 5. Then on next page it should display numbers from 6 to 10 and on next page 11 to 15 and so on. But on every page numbers start from 1 to 5.
My code is below. I have tried different strategies but nothing worked. Please check code and tell me where to make changes so that it works properly. Thanks a ton in advance.
<div class="grid_12">
<div class="box first round fullpage mh500 grid">
<h2><?php echo $resource->_pageHead; ?></h2>
<?php $resource->displayMessage(); ?>
<?php
if($resource->_recordCount > 0)
{
?>
<div class="block">
<table class="listing" >
<thead>
<tr>
<th width="50" class="bdr-left">Sr. No.</th>
<th width="60">Name</th>
<th width="60">Email</th>
<th width="60">Address</th>
<th width="60">City</th>
<th width="60">State</th>
<th width="60">Phone Number</th>
<th width="60">Country</th>
<th width="60">Comment</th>
<th width="60">Inquiry Date</th>
<th width="60" class="bdr-right">Action</th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
$_SESSION['number'] = $i;
$perpage = 5;
$q = mysql_query("SELECT * FROM $resource->_table");
$total_record = mysql_num_rows($q);
$pages = ceil($total_record/$perpage);
$page = (isset($_GET['page']))?$_GET['page']:1;
$start = ($page-1) * $perpage;
$result = mysql_query("SELECT * FROM $resource->_table LIMIT $start, $perpage");
while($res = mysql_fetch_array($result))
{
?>
<tr class="odd gradeX">
<td><?php echo $i; ?></td>
<td><?php echo $res['name']; ?></td>
<td><?php echo $res['email'];?></td>
<td><?php echo $res['address'];?></td>
<td><?php echo $res['city'];?></td>
<td><?php echo $res['state'];?></td>
<td><?php echo $res['p_code']."-".$res['p_num'];?></td>
<td><?php echo $res['country'];?></td>
<td><?php echo substr($res['comments'], 0, 100);echo "...";?></td>
<td><?php echo $res['inquiry_date'];?></td>
<td align="center">
<a href="<?php echo $_SERVER['PHP_SELF'].'?action=delete&id='.$res['id'];?>" onclick="return confirm('Do you want to delete this record?');">
<img src="img/cross.png" alt="Delete" title="Delete"/>
</a>
</td>
</tr>
<?php
$i++;
}
}
?>
</tbody>
</table>
</div>
<div id="paging" style="padding-left:500px;">
<?php
$prev=$page-1;
$next=$page+1;
if($prev > 0)
{
echo "<a href='?page=$prev'>Prev</a>";
}
echo " ";
if($pages >= 1 AND $page <= $pages)
{
for($x=1;$x<=$pages;$x++)
{
echo " ";
echo ($x==$page) ?"$x":'<a href="?page='.$x.'" >'.$x.'</a>';
}
echo "&nbsp&nbsp";
if($page<$pages)
{
echo "<a href='?page=$next'>Next</a>";
}
}
?>
</div>
</div>
<div class="clear"></div>
</div>
I hope the following logic would be helpful to you, initialize a variable $i = 0;
Calculate the starting number of a record by the following logic,
$page_num = (int) (!isset($_GET['page']) ? 1 : $_GET['page']);
$start_num =((($page_num*$num_records_per_page)-$num_records_per_page)+1);
initialize a variable $i = 0;
Then inside the loop calculate the serial number like,
$slNo = $i+$start_num;
Then echo $slNo;
instead of echo $i; try this
$j= (($page-1) * $perpage) + i; echo $j;
You can go to this website and there is a simple example of pagination
http://snipplr.com/view/55519/
hope this works for you.

Need little PHP help

I need little PHP help, here is a code which generate a product list:
<table class="list">
<?php for ($i = 0; $i < sizeof($products); $i = $i + 1) { ?>
<tr>
<?php for ($j = $i; $j < ($i + 1); $j++) { ?>
<td width="100%"><?php if (isset($products[$j])) { ?>
<a class="prod_snimka" href="<?php echo $products[$j]['href']; ?>"><img width="200" src="<?php echo $products[$j]['thumb']; ?>" title="<?php echo $products[$j]['name']; ?>" alt="<?php echo $products[$j]['name']; ?>" /></a>
<?php } ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Output: http://d.pr/PPVq+
What I need to modify to produce output one TR and TD's inside, after 4 TD (products) make another TR. In one row I want 4 TD, in that case too, if there is only 3 product then the last one will be an empty TD. Huhh.. It is possible?
Thanks
It does not look like you need that inner loop. It will only execute once anyway.
Try this out:
<?php
$columns = 4;
$i = 0;
?>
<table class="list">
<?php foreach ($products as $product) {
if (!($i % $columns)) {
echo '<tr>';
} ?>
<td width="25%">
<a class="prod_snimka" href="<?php echo $product['href']; ?>"><img width="200" src="<?php echo $product['thumb']; ?>" title="<?php echo $product['name']; ?>" alt="<?php echo $product['name']; ?>" /></a>
</td>
<?php
$i++;
if (!($i % $columns)) {
echo '</tr>';
}
?>
<?php } ?>
<?php
if (!($i % $columns)) {
while (!($i % $columns)) {
echo '<td width="25%"></td>';
$i++;
}
echo '</tr>';
}
?>
</table>
<table class="list">
<?php $i = 0;
$cols = 4;
for each $products as $product {
if (!($i % $cols)) {
echo "<tr width='25%'>";
}
echo '<td><a class="prod_snimka" href="'.$product['href'].'"><img width="200" src="'.$product['thumb'].'" title="'.$product['name'].'" alt="'.$product['name'].'" /></a></td>';
if !($i % $cols) {
echo "</tr>";
}
$i++;
}
if ($i-1 % $cols) {
echo "</tr>";
}
</table>

Categories