Below is my Code.
<?php
$folder_name=$_GET['folder_name'];
$dirname = "customized_kits/images/";
$images = glob($dirname."*");
$filecount = count( $images );
$i=0;
for($i=0;$i<200;$i++) { ?>
<div class="col-md-2"> <div class="photos">
<div id="images1">
<a href=# data-lightbox="roadtrip">
<?php
if($filecount>$i) {
$filename = substr($images[$i], strrpos($images[$i], '/') + 1);
echo '<img src="'.$images[$i].'" style="width:150px;height:150px" /><br/>';
?>
<i class="fa fa-remove"></i>
<?php } else {
echo '<a ><img style="width:150px;height:150px;background-color:#eee" /></a>';
} ?>
</div>
</div>
</div>
<?php } ?>
Here I created 200 boxes with foreach loop I want to show 20 divs for single page and with help of pagination I want to show other divs.I searched in many websites I didnt get exact answer please help me to get out of this issue.
Okay so from your code you can have 200 values of $i and you want to paginate them in segments of 20. Let us start off by adding a script above your code to get the range that needs to be displayed. Page number comes as a GET parameter in number. ex example.com/feeds?number=2.
<?php
$pageno = $_GET['number']; // Page number from frontend
// Now this value will give you 1,2,3 etc
$start = $pageno*20;
// $start contains the initial offset or the value of `$i` from to start
for($i=$start;$i<$start+20;$i++) {
// Will only show 20 elements on the page
}
?>
Remember this is a very basic example. Although i would recommend using range in sql query instead so you don't have to load the complete dataset in each query.
More explanation
Suppose you have M results and you want to show N results on each page, the results on page x would start from x*N to x*(N+1) pure math here. In your code you are iterating $i over the whole loop rather just iterate over that specific range to get results. The value of x is the example is the page number that we get from number in GET verb.
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 months ago.
I have a multidimensional array that is used to pull images and data into the page. The page will also have a sort by system which will display only the array items that have a certain tag (currently tagging yes or no with 0 and 1) I am using an if statement to decide whether the page displays the default (all of the photos) or the sorted array. The default works fine on its own, but im trying to get the sorted to display. When i include the html/php for displaying the images, instead of "echo $mImage['img_caption'];" (Which is a stand in, im just testing if i can even get the statement to display the items. Instead of the full caption string, it displays the first character of every value of every key.) i get errors akin to "unexpected "{" or "<" and the page wont load. Ive basically had to teach myself php over the course of three days, so im sure im missing something easy. Ill include an example of the array, the php for the sort, and the section that works to display the entire array.
Here is my arrays
$arrImages[] =
[
'img_sm'=>'exampleple.jpg',
'img_lg'=>'example2.thumb.jpg',
'img_caption'=>'multifamily',
'img_description'=>'example',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=>1,
];
$arrImages[] =
[
'img_sm'=>'example.jpg',
'img_lg'=>'example.thumb.jpg',
'img_caption'=>'notmultifamily',
'img_description'=>'example786',
'img_path' => 'img/getest',
'img_type'=>'getest',
'MultiFamily'=> 0,
];
And here is the if statement
<?php
$sorted = 1;
//If the viewer is sorting, check what they are sorting by and then sort by that before pushing the page to change display.
if ($sorted = 1){//start if sorted statement
$MultiFamilySorted = 1; //replace this with a function that checks all checkboxes to see if they are sorted by that or not. This is simulating that the viewer is sorting to see only multifamily homes.
foreach($arrImages as $sortedImages)://Start sort loop
if ($sortedImages['MultiFamily'] = 1){//start display sorted
foreach($sortedImages as $mImage): //start display loop
echo $mImage['img_caption'];//i am just using this to test if it will display
endforeach;
}//end display sorted
endforeach;
}//end if sorted statement
?>
This is the section for my default display
<div class="ImageDisplayTestBox">
<?php
foreach($arrImages as $mImage): //loop through the image array
?>
<div class="masonry-item no-default-style col-sm-3">
<a href="<?php echo $mImage['img_path'] . '/' . $mImage['img_lg']; ?>">
<span class="thumb-info thumb-info-centered-info thumb-info-no-borders">
<span class="thumb-info-wrapper">
<img src="<?php echo $mImage['img_path'] . '/' . $mImage['img_sm']; ?>" class="img-fluid" alt="">
<span class="thumb-info-title">
<span class="thumb-info-inner"><?php echo $mImage['img_caption']; ?></span>
<span class="thumb-info-type"><?php echo $mImage['img_type']; ?></span>
</span>
<span class="thumb-info-action">
<span class="thumb-info-action-icon"><i class="fas fa-plus"></i></span>
</span>
</span>
</span>
</a>
</div>
<?php endforeach; //end loop ?>
</div>
Thanks!
I did not check you code for the syntax error you mentioned, but there is a simpler way of doing this:
<?php
if ($sorted == 1){
$arrImages = array_filter($arrImages, function($image){
return $image['MultiFamily'] == 0;
});
}
?>
You can use this instead of your sorting code.
This executes the sorting function for each array member. If the function returns true it will pass it to the the sorted array. If it is false it will skip it.
The documentation of array_filter is here: https://www.php.net/manual/de/function.array-filter.php
I'll try to be clear. My problem is that i'm using php to get data from mysql. The table has more than one record. To show table records i'm using a while loop that as a condition has mysqli_fetch_array(). The records from the table must be shown in an echo(because i want to show them as html on the page), but every div generated from the while loop has a link to send me to another page, and if the client clicks on that link it will get the current divs information(after the table in database has more than one record the informations will be different) to be shown at the next page. Hope you understood it. Thanks in advance!
while($rows1 = mysqli_fetch_array($query1,MYSQLI_ASSOC))
{
echo "<div class=\"row\">
<div class=\"col-md-6\">
<div class=\"thumb\">
<figure>
<img src=\"images/extra-images/room-grid1.jpg\" alt=\"\"/>
<figcaption>
<a rel=\"prettyPhoto[gallery2]\" href=\"images/extra-images/room-grid1.jpg\">
<i class=\"fa fa-search\"></i>
</a>
</figcaption>
</figure>
</div>
</div>
<div class=\"col-md-6\">
<div class=\"text\">
<h4>".$rows1['Dh_lloji']."</h4>
<p>".$rows1['Dh_Pershkrimi']."</p>
<ul class=\"room-grid-meta\">
<li>Max: ".$rows1['Dh_Kapaciteti']."</li>
<li>Size: ".$rows1['Dh_madhesia']."</li>
<li>Floor: ".$rows1['Dh_Kati']."</li>
</ul>
<div class=\"retail room-grid-retail\">
<span>
<sup>$</sup>
".$rows1['Dh_cmimi']."
<sub>night</sub>
</span>";
if(isset($_SESSION['Emri_Mbiemri']) != '')
{
echo "<a class=\"btn-3\" href=\"payment.php\">Book now</a>";
}else{
$error = "You must be logged in to book!";
}
echo "<br>".$error."</div>
<div id=\"price_room\">".$rows1['Dh_cmimi']."</div>
</div>
</div>
</div>";
$count++;
}
Looks like you need at least your custom counter, that will be increment inside cycle, and set div's id based on this counter plus use it in generated link
I made a solution for my problem and i wanted to share it with you guys. I gave a $count to the links defining every link the while loop generates...
<a class=\"btn-3\" href=\"payment.php?id=$count\">Book now</a>
After that i used a multidimensional array to store the current divs variable(in my case rooms price, the count, and the id) of the room from database...
$room_prices[$count][0] = $rows1['Dh_cmimi'];
$room_prices[$count][1] = $count;
$room_prices[$count][2] = $rows1['ID_Dhoma'];
After the client click a specific link he will get the id so i can make a search at my multidimensional array for the price and the id of that room..
for($i = 0; $i < $count; $i++)
{
if($_GET['id'] == $room_prices[$i][1]){
$sess_room_price = $room_prices[$i][0];
$sess_room_id = $room_prices[$i][2];
}
}
After i get these i store them at the global session variables to use them at the other page...
$_SESSION['Rooms_Price'] = $sess_room_price;
$_SESSION['Rooms_ID'] = $sess_room_id;
So this did the work for me... Hope this helps others.
Making mobile site with Concrete5 and using page list block with custom template. I'm trying to count sub pages using PHP.
<?php foreach ($pages as $page):
// Prepare data for each page being listed...
$title = $th->entities($page->getCollectionName());
$url = $nh->getLinkToCollection($page);
$target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
$target = empty($target) ? '_self' : $target;
$description = $page->getCollectionDescription();
$description = $controller->truncateSummaries ? $th->shorten($description, $controller->truncateChars) : $description;
$description = $th->entities($description);
$mies = 0;
?>
<li class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c" data-theme="c"><div aria-hidden="true" class="ui-btn-inner ui-li"><div class="ui-btn-text"><a target="<?php echo $target ?>" class="ui-link-inherit" href="<?php echo $url ?>">
<h2 class="ui-li-heading"><?php echo $title ?></h2>
<p class="ui-li-desc"><?php echo $description ?></p>
</a>
</div><span class="ui-icon ui-icon-arrow-r ui-icon-shadow"></span><span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($mies) ?></span></div></li>
<?php endforeach; ?>
So, probably need to use Count function(or length?), I don't know. If I am editing wrong place please advice if you have any experience in Concrete5 cms.
If you want to show the corresponding page number in the span element in your code:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo $mies; ?></span>
If you want to show the remaining sub-pages, then in the html code snippet above just replace $mies with count($pages) - $mies like:
<span class="ul-li-count ui-btn-corner-all ul-count-second"><?php echo count($pages) -$mies; ?></span>
You would first have to initialise $mies before you start the forloop so it should be something of the form:
<?php
$mies = 0;
foreach ($pages as $page):
//Rest of your code and increment $mies with every iteration
$mies ++; //This ensures that you are counting the corresponding pages
?>
If you want to get the count of total number of sub-pages, you just have to echo out $mies outside the for block may be like:
<?php
endforeach;
echo $mies; //Shows you the total number of pages processed inside the for loop.
//or Just find the length of pages array
echo count($pages);
?>
As far as getting the length of array is concerned you could use count or sizeof. I stumbled upon a SO question about using count or sizeof method for finding the length of an array.
This should get you started in the right direction.
You need the parent ID;
$parentId = Page::getCollectionParentId();
Note that Page::getCollectionParentId() gets the current page's parent ID,so you may want to try;
$parentId = $page->getCollectionParentID();
Then create a new PageList to filter with and filter by the parentId;
Loader::model('page_list');
$pl = new PageList();
$pl->filter(false, '(p1.cParentID IN ' . $parentId . ')');
// Get the total
var_dump($pl->getTotal());
This is untested but the theory makes sense.
This is likely a bit simpler.
$pl->getTotal()
$pl is the PageList object that is set in the controller.
Also, these days you can just use the h() method instead of writing out $th->entities()
Edit: I should clarify that you don't need to do a $pl = new PageList() because $pl is already set to the PageList object in the controller and passed to the view.
hi guys i am trying to do this like i have 5 frames (fancy border) and i have items list. when items load every item load different frame. when 5 frame done then 6th frame repeat frames list. below my script
<?php
$allgfts=mysql_query("select id,image_url from {$statement} order by id limit {$startpoint}, {$limit}");
while($gfts=mysql_fetch_array($allgfts))
{
$id=$gfts['id'];
$image=$gfts['image_url'];
?>
<div id="pic-1">
<div class="thumbnail-item">
<?php echo '<img src="images/'.$image.'" alt="" width="161" height="161" class="thumbnail g-size" />'; ?>
<span><?php echo 'Readmore';?></span>
<?php echo '<a class="gtbtn" href="g_buy.php?id='.$id.'">Get This</a>';?>
</div>
</div>
<?php
}
?>
I think you're asking how to echo a list of images, with that list wrapping to a new line every fifth item.
Given an array of results from a table (consider using PDO, by the way), I would do the following:
//$arr being the array
$x=1; //start counter
$list = '<ul>'; //using a list, because that's what it is
for($i=0;$i<count($arr);$i++) {
$list.='<li class="thumbnail-item">';
$thumb ='<a href="g_detail.php?id='.$arr[$i][id].'">';
$thumb.='<img src="images/'.$arr[$i][image_url].'" alt="" class="thumbnail g-size" /></a>';
$thumb.='<span><a href="g_detail.php?id='.$arr[$i][id].'>Readmore</a></span>';
$thumb.='<a class="gtbtn" href="g_buy.php?id='.$arr[$i][id].'">Get This</a>';
$list.=$thumb;
$list.='</li>';
if($x%5 == 0)||($x==count($arr)) {
$list.='</ul>';
if($x<count($arr)) {
$list.='<ul>';
}
}
$x++;
}
echo $list;
This is untested, but broadly speaking should work.
Use the remainder operator("%"). I don't know what your table structure looks like, but I am going to assume your product ID loads in sequential order, starting with 1.
In your WHILE loop, use the following:
$remainder = $id % 5;
if($remainder == 1){
//load my DIV with frame 1
}
else($remainder == 2){
//load my DIV with frame 2
}
......
So what I'm trying to do is select all the distinct months from my database and then print them in a list. That, I can accomplish. The problem lies in the fact that I need my list to be two column. The way that I achieve this with CSS is by using 2 different div's "left" and "right" which are floated next to each other. This poses a problem with PHP because it needs to echo a div close and a new div open after it echoes the sixth month. Then it needs to start again from where it left off and finish. I can't just list all of the months in the HTML, either because I don't want it to list a month if I don't have any records in the DB for that month, yet. Any ideas? I hope I was clear enough!
Thanks!
-williamg
Something like this should work (the basic idea being to just keep a count of the months an increment it as you loop through them):
<div class="left">
<?php
$x = 1;
foreach($months as $month) {
# switch to the right div on the 7th month
if ($x == 7) {
echo '</div><div class="right">';
}
echo "<div class=\"row\">{$month}</div>";
# increment x for each row
$x++;
}
</div>
<?php
$numberOfMonths = count($months);
$halfwayPoint = ceil($numberOfMonths / 2);
echo "<div class=\"left\">";
for($i=0; $i<$halfwayPoint; $i++){
echo $months[$i] . "<br />";
}
echo "</div><div class=\"right\">";
for($i=$halfwayPoint; $i<$numberOfMonths; $i++){
echo $months[$i] . "<br />";
}
echo "</div>";
?>
Rant: on
When displaying tabular data, use table instead of floating div. It will make sense when viewing the page with css disabled. If you use floated div, then you data will displayed all way down. Not all table usage is bad. People often hate table so much, so using floated div. Table only bad when used for page layout.
Rant: off
When I need to have certain content displayed with some open, close, and in-between extra character, I will make use of implode. This is the example:
$data = array('column 1', 'column 2');
$output = '<div>'.implode('</div><div>', $data).'</div>';
//result: <div>column 1</div><div>column 2</div>
You can extends this to almost anything. Array and implode is the power that php have for many years. You will never needed any if to check if it last element, then insert the closing character, or check if it first element, then insert opening character, or print the additional character between elements.
Hope this help.
Update:
My bad for misread the main problems asked. Sorry for the rant ;)
Here is my code to make a data displayed in 2 column:
//for example, I use array. This should be a result from database
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
//should be 12 month, but this case there are only 9 of it
for ( $i = 0; $i <= 5; $i++)
{
//here I do a half loop, since there a fixed number of data and the item for first column
$output = '<div class="left">'.$data[$i].'</div>';
if ( isset($data[$i+6] )
{
$output = '<div class="right">'.$data[$i+6].'</div>';
}
echo $output."\n";
}
//the result should be
//<div class="left">1</div><div class="right">7</div>
//<div class="left">2</div><div class="right">8</div>
//<div class="left">3</div><div class="right">9</div>
//<div class="left">4</div>
//<div class="left">5</div>
//<div class="left">6</div>
Other solution is using CSS to format the output, so you just put the div top to down, then the css make the parent container only fit the 6 item vertically, and put the rest to the right of existing content. I don't know much about it, since it usually provided by fellow css designer or my client.
Example assumes you have an array of objects.
<div style="width:150px; float:left;">
<ul>
<?php
$c = count($categories);
$s = ($c / 3); // change 3 to the number of columns you want to have.
$i=1;
foreach($categories as $category)
{
echo '<li>' . $category->CategoryLabel . '</a></li>';
if($i != 0 && $i % $s == 0)
{
?>
</ul>
</div>
<div style="width:150px; float:left;">
<ul>
<?php
}
$i++;
}
?>
</ul>
</div>