Limit PHP foreach Results - php

I have a section in my footer from a script, that I am trying to customize and that displays the website categories.
Currently the results were limited to 9 results per column and continuing onto x amounts of columns. I need to be able to stop at 2 columns.
Here is the code I have
<div class="left">
<h3 style="padding-top:15px;"><?php echo BROWSE_CATEGORIES; ?></h3>
<?php $footer_category= $this->home_model->get_category(); ?>
<?php if($footer_category) { $ftr_cnt=1; ?>
<ul>
<?php foreach($footer_category as $cat) {
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt>9) { $ftr_cnt=1; echo "</ul><ul>"; }
$ftr_cnt++;
}
} ?>
</ul>
</div>

Use a break when you've reached 9 * 2, where 2 is the amount of columns you want.
$ftr_cnt = 1;
foreach($footer_category as $cat)
{
echo '<li>'.anchor('search/category/'.$cat->project_category_id,substr($cat->project_category_name,0,30)).'</li>';
if($ftr_cnt == 9)
{
echo "</ul><ul>";
}
$ftr_cnt++;
if($ftr_cnt >= (9 * 2))
{
echo "</ul>";
break;
}
}

Related

reducing number of php pagination links to 10

Ive been building a data driven website displaying general info about countries in the world. Its been made so that 1 country is displayed per page and you can move the next country by clicking a pagination link. Only problem that I am having is I cannot limit the amount of visible links. I did try this for loop with the first line as this: for ($i = $Page; $i <= min($Page + 9, $TotalRecords); $i++) { which does reduce it to the 10 records however this does result in the website breaking when I test the web address by entering index.php?page=aa.
<nav class="mt-5">
<ul class="pagination pagination-lg justify-content-center">
<?php
if( isset($Page) ) {
if ($Page > 1 ) {
?>
<li class="page-item">
«
</li>
<?php
}
}
?>
<?php
global $ConnectingDB;
$sql = "SELECT COUNT(*) FROM countriesinfo";
$stmt = $ConnectingDB->query($sql);
$RowPagination = $stmt->fetch();
$TotalRecords = array_shift($RowPagination);
$RecordPagination = $TotalRecords / 1;
$RecordPagination = ceil($RecordPagination);
for ($i = $Page; $i <= $RecordPagination; $i++) {
if( isset($Page) ) {
if ($i == $Page)
{
?>
<li class="page-item active">
<?php echo $i; ?>
</li>
<?php
} else {
?>
<li class="page-item">
<?php echo $i; ?>
</li>
<?php
}
}
}
?>
<?php if (isset($Page) && !empty($Page) ) {
if ($Page+1 <= $RecordPagination) {
?>
<li class="page-item">
»
</li>
<?php
}
}
?>
</ul>
</nav>
I test the web address by entering index.php?page=aa.
You can check to make sure the get variable is a number by using php is_numeric() bulit in function
and if it isn't force it to number 1.
This will stop the website from breaking when someone manipulate the page varible.
Here is a code to help you out, put at the beginning of the page.
if(is_numeric($page)){
$page = $page; // if $page is a number do nothing
}else{
$page = 1; //else set $page to 1 which will fetch data from beginning
}

dynamic grid using php and bootstrap

I want to make a dynamic grid using php and bootstrap.
For example
Case 1 => If i have total 6 records then row 1 contain col-md-4 grids and row 2 contain col-md-4 grids
Case 2 => If i have total 5 records then row 1 contain col-md-4 grids and row 2 contain col-md-6 grids
Case 3 => If i have total 4 records then row 1 contain col-md-4 grids and row 2 contain col-md-12 grids
How to do that i have no idea, any solution?
<?php foreach($Banners as $i=>$DATA){
if($count%3==1){
echo "<div class='row'>";
$lgclass = "col-lg-4";
}
if($count%4==0){
$lgclass = "col-lg-6";
}
if($count%5==0){
$lgclass = "col-lg-6";
}
?>
<div class="<?php echo $lgclass;?> col-xs-12 col-sm-12" <?php echo $count;?>>
<h2 class="section-title" > </h2>
<a href="http://mymegarealty.net/index.php?option=com_jointeam">
<div class="full-width">
<img src="<?php echo $Imageurl;?>" class="img-responsive" style="height:250px;;display:unset;">
</div>
</a>
</div>
<?php
if($count%3==0){
echo "</div>";
}
$count++;} ?>
it would be better a recursive solution, if you want you can do it. I wrote you at the beginning the logic if I follow an example overturned on your needs. my logic was the procedural one.
<?php
/*
logic
while myElements !=0
if myElements > 2
dispay row whith 3 elements
myElements remove firs 3
else if myElements == 2
dispay row whith 2 elements
myElements remove firs 2
else if myElements == 1
dispay row whith 1 elements
myElements remove firs 1
end while
*/
// init my data
$data=[];
$element=5;
for($i=0;$i<$element;$i++){array_push($data,$i);}
var_dump($data);
// do code
function displayElement($data, $classAdd){
echo "<div class='".$classAdd."'>";
echo $data; // your cell
echo "</div>";
}
while (sizeof($data)!=0){
if (sizeof($data)>2){
echo "<div class='row'>";
displayElement($data[0],"col-lg-4");
displayElement($data[1],"col-lg-4");
displayElement($data[2],"col-lg-4");
echo "</div>";
$data=array_slice($data,3);
}elseif (sizeof($data)==2){
echo "<div class='row'>";
displayElement($data[0],"col-lg-6");
displayElement($data[1],"col-lg-6");
echo "</div>";
$data= array_slice($data,2);
}elseif (sizeof($data)==1){
echo "<div class='row'>";
displayElement($data[0],"col-lg-12");
echo "</div>";
$data= array_slice($data,1);
}
}
You should be able to simply achieve this with flex. But with bootstrap try to use recursion: small example below
<?php
$elements = [1, 2, 3, 4];
function displ(&$el, $out = "") {
if (count($el) === 0) {
return $out;
} elseif (count($el) > 3) {
$i = 0;
foreach ($el as $k=>$v) {
$out .= "<div class=\"col-md-4\">" . $v . "</div>";
unset($el[$k]);
$i++;
if ($i === 3) {
return displ($el, $out);
}
}
return displ($el, $out);
} elseif (count($el) < 3) {
foreach ($el as $k=>$v) {
$out .= "<div class=\"col-md-" . 12 / count($el) . "\">" . $v . "</div>";
unset($el[$k]);
}
return $out;
}
}
echo displ($elements);

For loop is making 3600 database requests to load Products. PHP

Problem: One piece (That we can identify currently) is causing a clients product list to call the database over and over again (3600 times) at points when loading a longer list of products.
Code:
<?php foreach ($cats as $cat) :
if (in_array($cat->getId(), [68, 28, 27, 59, 79, 80, 119])) :
$cat_show = Mage::getModel('catalog/category')->load($cat->getId());
$children = $cat_show->getChildrenCategories($cat->getId());
$url1 = $cat_show->getURL();
$showAnyways = in_array(strtolower($cat_show->getName()), ["hats", "juniors", "accessories"]);
if ($cat_show->getShowSidebar() == 1 || $showAnyways) : ?>
<li class="current<?php if ($cat->getId() == $current_cat) { ?> active <?php } ?>">
<?php echo $cat->getName() ?>
<ul>
<?php if ($cat_show->getID() != 68 && $cat_show->getID() != 59) { ?>
<li class="current<?php if ($cat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>"><a class="view_all" href="<?php echo $url1 ?>"><?php echo $this->__("View All"); ?></a></li>
<?php } ?>
<?php foreach ($children as $subcat) {
$subcat_show = Mage::getModel('catalog/category')->load($subcat->getId());
if ($subcat_show->getShowSidebar() == 1 || in_array($subcat_show->getID(), [84])) {
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
if ($grand_children) {
$cats_displayed = 0;
foreach ($grand_children as $grand_child) {
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
?>
<li class="current<?php if ($grand_child->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $grand_child_show->getName() ?>
</li>
<?php $cats_displayed++;
}
}
}
if ($cats_displayed == 0 || !$grand_children) {
$url = Mage::getModel('catalog/category')->load($subcat->getId())->getURL();
?>
<li class="current<?php if ($subcat->getId() == $current_cat && $j == 0) {
$j++; ?> active<?php } ?>">
<?php echo $subcat->getName() ?>
</li>
<?php }
}
} ?>
</ul>
</li>
<?php endif;?>
<?php endif;?>
<?php endforeach; ?>
Can anyone provide me with some pointers on how to make this FAR more efficient and not make so many DB calls.
Should note, I am not an amazing php developer by trade. Main language is python so I am trying to get some advice on the best way to go about fixing this given my less that great knowledge of php itself.
You should never have a database query call inside a for loop. You need to build a query at the start that will get all the data required before the for loop.
Some instant pointers I can see are:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$url = Mage::getModel('catalog/category')->load($grand_child_show->getId())->getURL();
This is calling the database twice for no reason, you should be able to do this:
$grand_child_show = Mage::getModel('catalog/category')->load($grand_child->getId());
if ($grand_child_show->getShowSidebar() == 1) {
$grand_child_show->getURL();
You should be able to drop all these 'GetModel' functions if at the start of the script you call something like:
$all_grand_children = Mage::getModel('catalog/category')->getAllCategories();
This would return a hash array which you would be able to access relevant items by doing the following inside the for loop:
$grand_children = $all_grand_children[$subcat->getId()];
This would replace
$grand_children = Mage::getModel('catalog/category')->getCategories($subcat->getId());
You should also do a initial call for all of the grand_child and cat_show objects. If you are skilled at SQL you can call just the relevant information by joining the tables in one SQL query.

PHP getting <div> in between after row count divided by 4

I need to divided all data into 4 columns. i have used this method but its not coming column property
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
$tempcount = 1;
if ($stmt1->execute(array())) {
while ($row = $stmt1->fetch()) {
?>
<?php if ($tempcount == $pages) { ?>
<div class="column">
<div class="ui bulleted list">
<?php } ?>
<?php
if ($tempcount == $pages) {
$pages = $pages + $pages; ?>
</div>
</div>
<?php }
$tempcount++;
}
} ?>
how i can get this columns after getting count of data i have and add those divs in between.
for an Example 20 data rows 20/4 = 5 after each 5 data, i need start and end to be added
<div class="column">
<div class="ui bulleted list">
</div>
</div>
<div class="column">
<div class="ui bulleted list">
</div>
</div>
// And so on
result will be like this
thank you very much
Here's a proper code with comments
$tempcount = 0;
$pages = ceil($rowcount / 4);
while ($row = $stmt1->fetch()) {
// if result of % (Modulo) is zero - you need to start new column
if ($tempcount % $pages == 0) {?>
<div class="column">
<div class="ui bulleted list">
<?php
}?>
ITEM
<?php
// if result of % (Modulo) is (pages - 1) - you need to close previous column
if ($tempcount % $pages == ($pages - 1)) {?>
</div>
</div>
<?php
}
$tempcount++;
}
// check if you have to close previous column
// because it was not closed in a while loop
if (0 < $rowcount) {
if ($tempcount % $pages != 0) {?>
</div>
</div>
<?php
}
}
Whilst the modulo approach mentioned above will help in these situations, I often find that writing your loops from a different perspective can help in sorting out the situation, and making things more readable.
Rather than looping through every row, and then inserting the columns where you think they need to be. Instead loop every item, but in a controlled count, wrapped by the columns.
I've switched the code to using echo just due to personal preference, you can still use php-breakouts instead if preferred.
Please note this is untested example code, just to illustrate the point:
<?php
$stmt1 = $db->prepare($sql1);
$stmt5 = $db->prepare($sql1);
$stmt5->execute();
$rowcount = $stmt5->rowCount();
$pages = ceil($rowcount / 4);
if ($rowcount) {
$stmt1->execute(array());
do {
$group = '';
$group .= '<div class="column">';
$group .= '<div class="ui bulleted list">';
for ( $i=0; $i<$pages; $i++ ) {
$row = $stmt1->fetch(); if ( !$row ) { break; }
// presumably something would be done with $row here
$group .= '';
}
$group .= '</div>';
$group .= '</div>';
echo $group;
} while ( $row );
}
% modulo will help you PHP operators. While you fetch, use it to create your divider. If using div and if needed, I usually add a tiny div with clear: both to make sure it breaks the line after the 4/8/12... results.
$cell++;
$jumpline = ($cell % 4) ? "" : "<div class=\"spacer\"></div><br />";
$endit = ($cell % 4) ? "" : "</div>";

PHP pagination with multiple GET variables

I'm struggling with pagination with 2 get variables - 1 of which being the page number.
My second $_GET variable I have is $_GET['cat'] which is getting the category of products I want to filter on.
Everything works with the pagination DISPLAY, number of pages etc., however, if I am filtered on a particular category, the first page of results displays fine, but when i try to get to the next page or any page number, nothing is displayed product wise.
Any help would be appreciated, please see code below.
<?php include("header.php");
echo '<div id="content_header"><center><h1> • Products • </h1></center></div>';
echo '<div id="products_left">
Show:<br />
<ul style="list-style-type:none; margin:0; padding:0;">
<li>Wedding Stationery</li>
<li>Frames</li>
<li>Nappy Cakes</li>
<li>Miscellaneous</li>
<li>All</li>
</ul>
</div>';
echo '<div id="clear"></div>';
echo '<div style="padding-left:150px; margin-top:-70px">';
$connect = mysqli_connect("localhost","root","") or die("Failed to connect to database");
mysqli_select_db($connect,"magical_moments") or die("Unable to find database");
if(empty($_GET['cat'])){
$query = mysqli_query($connect,"SELECT * FROM products");
} else {
$cat = $_GET['cat'];
$query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'");
};
$products = mysqli_fetch_array($query);
$start = 0;
$limit = 9;
if(isset($_GET['page']))
{
$pid=$_GET['page'];
$start=($pid-1)*$limit;
}
else{
$pid=1;
}
//Fetch from database first 10 items which is its limit. For that when page open you can see first 10 items.
if(empty($_GET['cat'])){
$query = mysqli_query($connect,"SELECT * FROM products LIMIT $start, $limit");
} else {
$cat = $_GET['cat'];
$query = mysqli_query($connect,"SELECT * FROM products WHERE type='$cat' LIMIT $start, $limit");
};
?>
<ul>
<?php
//print 10 items
while($result=mysqli_fetch_array($query))
{
echo '<div id="display_product">
<table>
<tr>
<td><div id="product_image_holder" style="background-image:url('.$result[5].')"></div></td>
</tr>
<tr>
<td>'.$result[1].'</td>
</tr>
<tr>
<td>'.$result[3].'</td>
</tr>
<tr>
<td>£ '.$result[4].'</td>
</tr>
</table>
</div>';
}
echo '<div id="clear"></div>';
?>
</ul>
<?php
//fetch all the data from database.
echo '<div id="pagination_div">';
if(empty($_GET['cat'])) {
$rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
if($pid>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href='?page=".($pid-1)."' class='previous_button'>PREVIOUS</a>";
}
if($pid!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href='?page=".($pid+1)."' class='next_button'>NEXT</a>";
}
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$pid) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li class='pagination'><a href='?page=".$i."'>".$i."</a></li>"; }
}
echo '</div>';
} else {
$rows=mysqli_num_rows(mysqli_query($connect,"SELECT * FROM products WHERE type='$cat'"));
//calculate total page number for the given table in the database
$total=ceil($rows/$limit);
// cat is set - pagination for cat + page
if($pid>1)
{
//Go to previous page to show previous 10 items. If its in page 1 then it is inactive
echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid-1)." class='previous_button'>PREVIOUS</a>";
}
if($pid!=$total)
{
////Go to previous page to show next 10 items.
echo "<a href=".$_SERVER['REQUEST_URI']."?page=".($pid+1)." class='next_button'>NEXT</a>";
}
//show all the page link with page number. When click on these numbers go to particular page.
for($i=1;$i<=$total;$i++)
{
if($i==$pid) { echo "<li class='current'>".$i."</li>"; }
else { echo "<li class='pagination'>".$i."</li>"; }
}
echo '</div>';
};
?>
</ul>
</div>
<?php include("footer.php"); ?>
Well, you define the page parameter. But the category parameter is lost once clicked on a link because it's simply not there.
You've got this:
echo "<li class='pagination'>".$i."</li>";
You lack the parameter cat. Here's an example:
echo '<li class="pagination">'.$i.'</li>';

Categories