Show 3 products in a Column & scroll Rows - php

I have products list,i have tried to align three products in a column and scroll towards right.how to shows only 9 ( 3 rows * 3 columns) products .this is my code.
<div style="border:solid 1px red;width:230px;overflow-x:scroll;">
<h4>Items/Products</h4>
<hr>
<table border="0">
<?php $count =0;
for($pr=0;$pr < count($items_list);$pr++){ ?>
<?php if($count % 3 == 0) { ?> <tr> <?php } ?>
<td style="border:solid 1px black;width:100px;height:40px;text-align:center">
<?php echo $items_list[$pr]->category;?>
</td>
<?php if($count % 3 == 0) { ?> </tr> <?php } ?>
<?php $count++;
} ?>
</table>
</div>
I have tried exactly look like this see here. How to change my code?

You need to use Ajax Pagination or Jquery Scolling to become (http://postimg.org/image/eifr7azob/)

Try this,
<div style="border:solid 1px red;width:430px;overflow-x:scroll;">
<h4>Items/Products</h4>
<hr>
<table border="0">
<tr>
<?php $count =1; //$countItems = count($items_list);
$countItems =11;
for($pr=0;$pr < $countItems;$pr++){ ?>
<td style="border:solid 1px black;width:100px;height:40px;text-align:center"> <?php echo $pr;?>
<?php //echo $items_list[$pr]->category;?>
</td>
<?php if($count % 3 == 0) { ?> </tr><tr><?php } ?>
<?php $count++; } ?>
</table>
</div>
3 Rows x N columns
<div style="border:solid 1px red;width:430px;overflow-x:scroll;">
<h4>Items/Products</h4>
<hr>
<table border="0">
<tr>
<?php $count =1;
//$items_list_count = count($items_list);
$items_list_count = 25;
$Rows = 3;
$coulmns =#ceil($items_list_count/$Rows);
for($pr=0;$pr < $items_list_count;$pr++){ ?>
<td style="border:solid 1px black;width:100px;height:40px;text-align:center">
<?php //echo $items_list[$pr]->category;?> <?php echo $pr;?>
</td>
<?php if($count % $coulmns == 0) { ?> </tr><tr><?php } ?>
<?php $count++; } ?>
</table>
</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 Through Ten, displaying whether Even Or Odd and Writing To a HTML Table.

I'm currently in an introductory course into PHP and I'm having trouble with my current assignment. Its rather simple in logic, but I can't find where my error is. The abstract is to loop from one to ten, display whether the number is even or odd, and display these facts in a table. So, row one would be 1 - odd
This is my current Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>1 To 10 Even Odd</title>
<link rel="stylesheet" type="text.css" href="common.css" />
<style type="text/css">
th { text-align: center; background-color: #999; }
th, td ( padding: 0.6em; )
tr.alt td { background: #ddd }
</style>
<h3>1 To 10</h3>
<table cellspacing="1" border="1" style="width: 20em; border: 1px solid #999;">
<tr>
<th>Number</th>
<th>Even Or Odd?</th>
</tr>
<?php
$max = 10 ;
$intCounter = 0;
while ( $intCounter < $max){
$intCounter++
?>
<tr <?php if ( $intCounter % 2 == 0 ) echo ' class="alt"' ?> >
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "even" ?> </td>
</tr>
<tr <?php if ( $intCounter % 2 == 1 ) ?> >
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "odd" ?> </td>
</tr>
<?php
}
?>
</body>
</html>
This current code is displaying all the numbers twice. So I'll get 1, odd, 1, even.
Thanks for the help in advance! I appreciate all the help!
row one would be 1 - odd
Put this statement $intCounter++ at the end of while loop, otherwise it would print only 9 rows. Also, your second <tr <?php if ( $intCounter % 2 == 1 ) ?> > ... </tr> is redundant here.
If you want to start your table with the odd styled row, then change your code in the following way,
// your code
$max = 10 ;
$intCounter = 1;
while($intCounter <= $max){
?>
<tr <?php if($intCounter % 2 == 0){ echo ' class="alt"'; } ?>>
<td> <?php echo $intCounter; ?> </td>
<td> <?php if($intCounter % 2 == 0){ echo "even"; }else{ echo "odd"; } ?> </td>
</tr>
<?php
$intCounter++
}
// your code
Since you want to display nos from 1 to 10, you should do the following modifications:
Set $intCounter = 1 instead of $intCounter = 0
Set while ($intCounter <= $max) instead of while ($intCounter < $max)
Mention the increment operation $intCounter++; just before the loop ends
Try this:
<?php if ($intCounter % 2 == 0 ) { ?>
<tr class='alt'>
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "even" ?> </td>
</tr>
<?php } else { ?>
<tr>
<td> <?php echo $intCounter ?> </td>
<td> <?php echo "odd" ?> </td>
</tr>
<?php } ?>

How to enter a word in looping <td> in php

I have table data which is taking data from foreach and looping through a for loop.(looping 3 times with different values from foreach)
I want to know that how can I put a word in last <td> ?
my code;
<?php
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php } ?>
foreach($halls_all_array AS $row){
for($j=$start;$j<$limit;$j++){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left"> time1-<?php echo format_time($row[$j]['time'])
if($j==($limit-1)){ echo "word";}
?>
</td>
<?php
}
}
?>
View
<?php foreach(... ) { ?>
<td>loop td</td>
<?php } ; ?>
Use :
if($j == ($limit - 1)) {
// last <td> here. You can add the text here.
}
EDIT
<?php
$count = count($halls_all_array);
$i = 0;
foreach($halls_all_array AS $row){
?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-<?php echo format_time($row[$start]['time'])?>
<?php
if(++$i === $count){
//echo 'WORD TO PRINT';
}
?>
</td>
<?php
}
?>
Try the following
// first of all check $halls_all_array and $start are defined or not
<?php
$count=count($halls_all_array);
$i=1;
foreach($halls_all_array AS $row){
echo'<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">time1-'.format_time($row[$start]['time']);
if($i == $count){
//echo 'WORD TO PRINT';
}
echo'</td>';
$i++;
}
?>
Try this code
<?php
$length = count($halls_all_array); // TAKE THE LENGTH OF ARRAY
$i = 1;
foreach($halls_all_array as $row){ ?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
<?php if($i < $length){ ?>
time1- <?php echo format_time($row[$start]['time']);?>
<?php }else{
echo "YOUR WORD..."; // Last Looping..
} ?>
</td>
<?php
$i++; // INCREMENT THE COUNTER
} ?>
first get the last key from array
end($halls_all_array); // move the internal pointer to the end of the array
$last_key = key($halls_all_array);// fetches the key of the element pointed to by the internal pointer
try this code:
<?php
/*get the last key*/
end($halls_all_array);
$last_key = key($halls_all_array);
foreach($halls_all_array as $key => $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
</td>
<?php if ($last_key == $key): ?>
<td><?php echo $row ?></td>
<?php endif ?>
<?php } ?>
Please write code like this:
<?php
$counter = 0; // start a counter
$total_count = count($halls_all_array); //total Counts
foreach($halls_all_array AS $row){?>
<td nowrap class="auto-style34" style="height: 30px; width: 20%" align="left">
time1-<?php echo format_time($row[$start]['time'])?>
<?php if($total_count==$counter) echo 'word'; ?> // you condition for last and word
</td>
<?php
$counter++;
}
?>

PHP foreach loop is taking time to load thousands of table data in Google Chrome

On a page I have 8500 Employees shown in table data in the form of <tr> and <td>.
The Name of the Employees shown with a checkbox in front of each name of employee.
When I click on checkboxes the I insert the employees data (Employee Name and Employee Id) session.
Everything is working fine but the problem is when I click on check All checkbox then all the employees checkboxes are selected then there is a button named as "View Selected". On the click of this button I want to all the selected employees. When user click on this button a new child window will be opened with selected employee data in the form table row and data.
I am doing this but using session which I have created on the click of employees checkboxes.
Everything is working on Mozilla Firefox but when I check this of Google Chrome then it is not working and I am getting the browser message KillPages or Wait. The loader image of Chrome is shown but data is not loading.
My new child window page code is this where I am reading the session and running the for-each loop to print the data in the form of table data.
<?php require_once("../../includes/global.php");
$sessionName = rq('sessionName');
$employees = $session->read($sessionName);
?>
<script type="text/javascript" src="<?php echo SITEURL_PAGE; ?
>configuration/js/attendancePolicy.js"></script>
<div style="width: 100%;">
<?php if(strpos($sessionName, 'location') !== false) {?>
<h3 style="padding-left:10px;">View <?php echo LOCATION_DISPLAY_NAME?>s</h3>
<?php } else {?>
<h3 style="padding-left:10px;">View <?php echo
us(substr(str_replace('ot_','',$sessionName), 0, -3))?></h3>
<?php }?>
<?php
$totalEmployees = $session->check($sessionName) ? (int)count($session->read($sessionName)) : 0;
?>
<form id="updateEmployeesForm" name="updateEmployeesForm" method="post" action="saveAttendancePolicy.php">
<input type="hidden" name="hidAction" value="addNewPolicy_step3" />
<input type="hidden" name="sessionName" id="sessionName" value="<?php echo $sessionName?>" />
<?php
$styleTab = '';
$style='';
if($totalEmployees > 30){
$styleTab = 'border-bottom: none;';
$style = 'overflow:auto; height: 230px !important; border-bottom: 4px solid #2C90D3;';
}
//for over time policy only
$functionSuffix = '';
if($sessionName == 'ot_locations_cb' || $sessionName == 'ot_divisions_cb' || $sessionName == 'ot_departments_cb' || $sessionName == 'ot_employees_cb') {
$functionSuffix = 'overTimePolicy';
}
$where = rq('where');
$employeeLoadPage = ($sessionName == 'ot_employees_cb')?'otpolicy_ajax':'';
if(stripos($sessionName, 'employee') > -1) {
$js = "closeClildWindow('', 'employeeDiv', 'yes','".$employeeLoadPage."');";
} else if(stripos($sessionName, 'location') > -1) {
$js = "searchPolicySpecificNew('', 'locations', 'locationDiv', {'session':'yes'},'".$functionSuffix."'); updateChildPolicyNew('".$sessionName."', 'yes','".$functionSuffix."');";
} else if(stripos($sessionName, 'division') > -1) {
$js = "searchPolicySpecificNew('', 'divisions', 'divisionDiv', {'session':'yes'},'".$functionSuffix."'); updateChildDepartmentPolicyNew('".$sessionName."', 'locations_cb', 'yes','".$functionSuffix."');";
} else if(stripos($sessionName, 'department') > -1) {
$js = "searchPolicySpecificNew('', 'departments', 'departmentDiv', {'session':'yes'},'".$functionSuffix."'); updateChildjobTitlePolicy('".$sessionName."', 'divisions_cb', 'locations_cb', '','".$functionSuffix."');";
}
if($where == 'viewEmp') {
if($sessionName == 'ot_employees_cb') {
$js="getSelectedEmployeesNew('OT');";
} else {
$js="getSelectedEmployeesNew();";
}
}
?>
<div class="totalRecord" style="float:right; width:99%;text-align:right; margin-top: 5px;">
<label>Total Record(s) :<?php echo $totalEmployees ?></label>
</div>
<div style="margin: 0 2% 0 2%; width: 96%;" class="div_row">
<table cellpadding="0" border="0" cellspacing="0" width="100%" class="bdrtable" style="border-bottom: 0px;">
<thead>
<tr>
<th align="left" scope="col" colspan="5"> <input type="checkbox" name="viewCheckAllName" id="viewCheckAllName" <?php if($totalEmployees > 0) {?> checked="checked"<?php }?> class="class_parent_pop" onClick="sessionCheckBox('class_parent_pop', '<?php echo $sessionName?>_pop', 'parent', this);" />
Check All </th>
</tr>
</thead>
</table>
</div>
<div style="margin-bottom: 2%; margin-left: 2%; margin-right: 2%; width:96%;<?php echo $style;?>" class="div_row">
<table cellpadding="0" border="0" cellspacing="0" width="100%" class="bdrtable" style="<?php echo $styleTab?>">
<?php if($totalEmployees > 0) { ?>
<tr>
<?php $i=1;
foreach($employees as $key=>$employeeArr) {
?>
<td align="left" width="33%"><?php echo $i; ?> </td>
<?php
if($i%3 == 0 && $i != $totalEmployees) {
echo '</tr><tr>';
}
$i++;
}
if($totalEmployees%3 != 0) {
for ($x=($totalEmployees%3); $x < 3; $x++) {
echo '<td align="left" width="33%"> </td>';
}
}
?>
</tr>
<?php }else{ ?>
<tr>
<td colspan="3" align="center"><?php echo "No Data Found"; ?></td>
</tr>
<?php }?>
</table>
</div>
<div class="clear"></div>
<div class="div_row" style="text-align: right; width: 96%; margin:0 2%;">
<?php if($totalEmployees > 0) {?>
<input type="button" name="updateEmp" id="updateEmp" value="Update" class="submit" onClick="sessionCheckBoxPopupUpdate('<?php echo $sessionName?>_pop', '<?php echo $sessionName?>');<?php echo $js; ?>" />
<?php } ?>
<input type="button" name="cancel" id="cancel" value="Cancel" class="submit" onClick="javascript:window.close();" />
</div>
</form>
It may not be the foreach loop that is slow, 8500 is good number of data to load, you might want to page the results.
Why don't you try the same query using phpmyadmin and see the time taken.

iterate a html table of two row and 3 column using one php query

I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table.
I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row.
this is what i have tried but didn't work
<div id="maincontent">
<!-- class one -->
<?php
$getSection = getSection();
$i=0;
while($allSection = mysql_fetch_array($getSection)){
?>
<div class="subconent">
<table width="937" border="0">
<tr>
<td>
<div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div>
</td>
</tr>
</table>
</div>
<?php
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td> </td>
<td> </td>
<td> </td></tr>
<tr><div class="sub_image">
<img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink" onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" />
</div>
<div class="cont_txt">
<h3><?php echo $allSection['name_full']; ?></h3>
<p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
<br />
<img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" />
</div><td>
<?php }
} ?>
</div>
Use modulo operator (%):
http://www.devchunks.com/web-development/using-the-php-modulus-operator/
something like this:
<table>
<?php
$i = 0;
while ( $row = mysql_fetch_array($result) ){
if ($i % 3 == 0){
echo '<tr>';
}
echo '<td>'.$row['column_name'].'</td>';
if ($i % 3 == 2){
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
echo '</tr>';
}
?>
</table>
At its base, you'll need something like this:
<table>
<tr>
<?
$count = 0;
foreach ($row) {
echo "<td>" . $row["value"] ."</td>";
$count++;
if (($count % 3) == 0) && ($count > 0) {
echo ("</tr><tr>");
}
}
?>
</tr>
</table>
Start printing out the header of your table, and then begin iterating through the dataset. Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. (I've used %, so it'll wrap on every third entry, not just the first one)
Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ).
Or simply fetch everything into PHP arrays.
Oldschool: mysql_query() and while( $row = mysql_fetch_array() )
Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php )
Awesome! Thanks a lot. It works for me, Zend. You can try something like this.
<table width="1024px" border="0" cellspacing="2" cellpadding="2">
<?php
$i = 0;
foreach ($this->rows as $row )
{
$img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
if ($i % 3 == 0)
{
echo '<tr>';
}
?>
<td align="center">
<img src="<?php echo $img; ?>" width="300" height="215"><br/>
<?php echo $row->gly_title; ?>
</td>
<?php
if ($i % 3 == 2)
{
echo '</tr>';
}
$i++;
}
//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0)
{
echo '</tr>';
}
?>
</table>
<?php if ($i++%$_columnCount==0): ?>
<tr>
<?php endif ?>
<td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
<?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
</tr>
<?php endif; ?>
<?php } ?>

Categories