Creating Next/Previous pagination links - php

I am working on a portfolio site and have all projects in array, I have created a project template page that uses a GET variable to set the current project and load appropriate content into page. I am trying to add links at the bottom that link to the next and previous project but cannot figure out the correct way to write the link
Here is what the array looks like:
$projects = array();
$projects["Beeston-Residence"] = array(
"name" => "Beeston House",
"heroClass" => "hero-beeston",
"mainImg" => BASE_URL ."images/beeston_main.jpg",
"previewImg" => BASE_URL ."images/beeston_preview.jpg",
"year" => "2014",
"yearClass" => "twenty14",
"location" => "Brisbane, Australia",
"photographer" => "Rachel Cruz",
"detailOne" => BASE_URL ."images/beeston_detail1.jpg",
"detailTwo" => BASE_URL ."images/beeston_detail2.jpg",
"detailThree" => BASE_URL ."images/beeston_detail3.jpg",
"detailFour" => BASE_URL ."images/beeston_detail4.jpg",
"description" => "lorem ipsum and shit",
"detailOneSum" => "lorem ipsum detail 1",
"detailTwoSum" => "lorem ipsum detail 2",
"detailThreeSum" => "lorem ipsum detail 3",
"detailFourSum" => "Lorem ipsum detail 4",
"materials" => array("matOne","matTwo","matThree"),
"materialImg" => array(BASE_URL ."images/beeston_mat1.jpg",BASE_URL ."images/beeston_mat2.jpg",BASE_URL ."images/beeston_mat3.jpg"),
"testamonialImg" => BASE_URL ."images/client.jpg",
"testamonial" => "Cabin Is Great!"
);
$projects["Book-Tower-Residence"] = array(
"name" => "Book Tower House",
"heroClass" => "hero-bookTower",
"mainImg" => BASE_URL ."images/bookTower_main.jpg",
"previewImg" => BASE_URL ."images/bookTower_preview.jpg",
"year" => "2012",
"yearClass" => "twenty12",
"location" => "London, United Kingdom",
"photographer" => "Rachel Cruz",
"detailOne" => BASE_URL ."images/bookTower_detail1.jpg",
"detailTwo" => BASE_URL ."images/bookTower_detail2.jpg",
"detailThree" => BASE_URL ."images/bookTower_detail3.jpg",
"detailFour" => BASE_URL ."images/bookTower_detail4.jpg",
"description" => "lorem ipsum and shit",
"detailOneSum" => "lorem ipsum",
"detailTwoSum" => "lorem ipsum",
"detailThreeSum" => "lorem ipsum",
"detailFourSum" => "Lorem ipsum",
"materials" => array("matOne","matTwo","matThree"),
"materialImg" => array(BASE_URL ."images/bookTower_mat1.jpg",BASE_URL ."images/bookTower_mat2.jpg",BASE_URL ."images/bookTower_mat3.jpg"),
"testamonialImg" => BASE_URL ."images/client.jpg",
"testamonial" => "Cabin Is Great!"
);
$projects["1917-Bungalow"] = array(
"name" => "1917 Bungalow",
"heroClass" => "hero-bungalow",
"mainImg" => BASE_URL ."images/bungalow_main.jpg",
"previewImg" => BASE_URL ."images/bungalow_preview.jpg",
"year" => "2013",
"yearClass" => "twenty13",
"location" => "Autstin, Texas",
"photographer" => "Rachel Cruz",
"detailOne" => BASE_URL ."images/bungalow_detail1.jpg",
"detailTwo" => BASE_URL ."images/bungalow_detail2.jpg",
"detailThree" => BASE_URL ."images/bungalow_detail3.jpg",
"detailFour" => BASE_URL ."images/bungalow_detail4.jpg",
"description" => "lorem ipsum and shit",
"detailOneSum" => "lorem ipsum",
"detailTwoSum" => "lorem ipsum",
"detailThreeSum" => "lorem ipsum",
"detailFourSum" => "Lorem ipsum",
"materials" => array("matOne","matTwo","matThree"),
"materialImg" => array(BASE_URL ."images/bungalow_mat1.jpg",BASE_URL ."images/bungalow_mat2.jpg",BASE_URL ."images/bungalow_mat3.jpg"),
"testamonialImg" => BASE_URL ."images/client.jpg",
"testamonial" => "Cabin Is Great!"
);
etc... The array has 15 projects in total
The project template The projects get filtered into is set up like this:
<?php
require_once("../includes/config.php");
require_once(ROOT_PATH . "includes/projectInfo.php");
$project_id = $_GET["id"];
$project = $projects[$project_id];
include (ROOT_PATH . "includes/header-project-page.php");
?>
<section class="l-col-group project-intro">
<h2 class="project-title"><?php echo $project["name"]; ?> </h2>
<aside class="project-info">
<ul class="project-brief">
<li class="pb-heading">STATUS </li>
<li class="pb-info">Complete <?php echo $project["year"];?></li>
<li class="pb-heading">LOCATION</li>
<li class="pb-info"><?php echo $project["location"];?></li>
<li class="pb-heading">PHOTOGRAPHER</li>
<li class="pb-info"><?php echo $project["photographer"];?></li>
</ul>
</aside>
<p class="project-summary">
<?php echo $project["description"];?>
</p>
</section>
<section>
<figure class="l-col-group">
<figcaption class= "left">
<h3>VISION</h3>
<p><?php echo $project["detailOneSum"];?></p>
</figcaption>
<img class="project-detail-img right" src="<?php echo $project["detailOne"];?>" alt="">
</figure>
<figure class="l-col-group">
<figcaption class="right">
<h3>DESIGN</h3>
<p><?php echo $project["detailTwoSum"];?></p>
</figcaption>
<img class="project-detail-img left" src="<?php echo $project["detailTwo"];?>" alt="">
</figure>
<figure class="l-col-group left">
<figcaption class="left">
<h3>DEVELOPMENT</h3>
<p><?php echo $project["detailThreeSum"];?></p>
</figcaption>
<img class="project-detail-img right" src="<?php echo $project["detailThree"];?>" alt="">
</figure>
<figure class="l-col-group">
<figcaption class="right">
<h3>TEAM</h3>
<p><?php echo $project["detailFourSum"];?></p>
</figcaption>
<img class="project-detail-img left" src="<?php echo $project["detailFour"];?>" alt="">
</figure>
</section>
<section class="testamonial">
<img class="testamonial-image" src="<?php echo BASE_URL; ?>images/testamonial.png" alt="testamonial">
<h3 class="testamonial-client">DAVID WALSH</h3>
<p class="testamonial-quote">Cabin surpassed went above and beyond my wildest dreams! Customer service at its best.</p>
</section>
include (ROOT_PATH . "includes/contact.php");
include (ROOT_PATH . "includes/footer.php");
?>
I have had luck retrieving the current projects position in the array by using the following:
<?php $key = array_search($project, $projects);
$offset = array_search($key, array_keys($projects));
$next = $offset + 1;
$prev = $offset - 1;
?>
But am having trouble on figuring out how to use this information to create a link to the next project in the array
I was trying something like this but do not know what to put as the link
<?php if ($prev > 0): ?>
<a style ="color:black" href="">Previous</a>
<?php endif; ?>
<?php
Thank you in advance for the help.
CHEERS.

It's probably better and simpler to pass indexes and not ids:
// get project id
$lst_ids = array_keys($project);
$i = !empty($_GET["i"]) ? $_GET["i"] : 0;
$project_id = $lst_ids[$i];
// show previous and next links, as applicable
$l = count($projects); // should give 15
$html = '';
if ($i) {
$i_prev = $i - 1;
$label = "← Previous";
$html .= "<a href='?i=$i_prev'>$label</a>";
}
if ($i < ($l - 1)) {
$i_next = $i + 1;
$label = "Next →";
$html .= " <a href='?i=$i_next'>$label</a>";
}
echo $html;
Or if you want to pass ids:
$l = count($projects); // should give 15
$lst_ids = array_keys($project);
$project_id = !empty($_GET["id"]) ? $_GET["id"] : NULL;
$index = $project_id ? array_search($project_id, $lst_ids) : 0;
$html = '';
if ($index) {
$id_prev = $lst_ids[$index - 1];
$label = "← Previous";
$html .= "<a href='?id=$id_prev'>$label</a>";
}
if ($index < ($l - 1)) {
$id_next = $lst_ids[$index + 1];
$label = "Next →";
$html .= " <a href='?id=$id_next'>$label</a>";
}
echo $html;

Related

resume foreachloop after incrementing php bootstrapp drop down menu

Question in short: i want to output array: 0, 1 , 2 then break, echo something , output 3,4,5 and keep this in a loop.
Hello everyone i am working on a dynamic php website with bootstrap 4 and php to practice the language. Unfortunately i am now stuck since i dont know how to create something that kindoff looks like an algorith. Allright enough talking and lets get down to the code:
nav.php file
<ul class="navbar-nav">
<li class='nav-item dropdown'>
<a class='nav-link dropdown-toggle' data-toggle='dropdown' datatarget='dropdown_target' href='#'>
<span class='caret'></span>Dropdown
</a>
<div class="dropdown-divider"></div>
<div class="dropdown-menu" aria-labelledby="dropdown_target">
<!-- <a class="dropdown-item">Dropdown</a> -->
<?php
$i=0;
foreach ($dropItems as $item ) {
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
$i++;
if($i==1) break;
echo "<a class='dropdown-item'>Dropdown</a>";
}
?>
</div>
</li>
</li>
<?php
foreach ($navItems as $item ) {
echo "<li class='nav-item'> <a class='nav-link' href=\"$item[slug]\">$item[title]</a> </li>";
}
?>
</ul>
arrays.php
<?php
//Navigatie menu items
$navItems = array(
array(
"slug" => "index.php",
"title" => "home"
),
array(
"slug" => "contact.php",
"title" => "Contact"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
$dropItems = array(
array(
"slug" => "#",
"title" => "Lps"
),
array(
"slug" => "#",
"title" => "Sps"
),
array(
"slug" => "market.php",
"title" => "Marketplace"
),
);
?>
You can use array_chunk to split the array in the chunks of three and foreach nested with echo "something";.
Can't see in your code where this echo of three is so I just made an example of how to do it.
$arr = range(1,12); //example array
$chunks = array_chunk($arr, 3);
Foreach($chunks as $chunk){
Foreach($chunk as $val){
Echo $val ." ";
}
Echo "\nsomething\n";
}
https://3v4l.org/0VlN0
Thanks to Andreas i got it working using his method
$chunks = array_chunk($dropItems, 2);
Foreach($chunks as $chunk){
Foreach($chunk as $item){
echo "<a class='dropdown-item' href=\"$item[slug]\">$item[title] </a>";
}
Echo "<div class='dropdown-divider'></div>";
}

PHP: Help in Retrieving and Echoing Multidimensional Array ID

I'm looking to display a list of available weapons of a game, based on items listed in an array. I'm able to display information suck as 'Attribute Values' and 'Price' perfectly via a foreach loop, however being a PHP newbie, I'm having a lot of trouble working out how to echo each item's ID, such as 'Short Sword', 'Middle Sword', 'Long Sword', etc. I thought I was getting close using key($sword)... but no dice. Here's what I'm working with:
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $sword) { ?>
<li>
<img src="<?php echo $sword["Item Sprite"]; ?>">
<span><?php echo $sword; ?></span>
<div>ATT +<?php echo $sword["Attribute Value"]; ?></div>
<div><?php echo $sword["Price"]; ?>G</div>
</li>
<?php } ?>
If anyone could lend a hand and help demonstrate how best to echo an item's ID or a per item basis throughout my loop, then that would be very awesome indeed. Thank you for your time.
try with this code
use key attr in foreach loop
and echo $key value instead of array like $sword
<?php $item_swords = Array();
$item_swords["Short Sword"] = Array (
"Attribute Value" => 5,
"Price" => 100,
);
$item_swords["Middle Sword"] = Array (
"Attribute Value" => 8,
"Price" => 250,
);
$item_swords["Long Sword"] = Array (
"Attribute Value" => 12,
"Price" => 750,
); ?>
<?php foreach ($item_swords as $key=>$sword) { ?>
<li>
<img src="
<?php //echo $sword["Item Sprite"]; ?>">
<span><?php echo $key; ?></span>
<div>
ATT +
<?php echo $sword["Attribute Value"]; ?></div>
<div>
<?php echo $sword["Price"]; ?>G
</div>
</li>
<?php } ?>

PHP Array Iterator to DIV

I want to loop through the unknown depth array with RecursiveIteratorIterator in SELF::FIRST mode along with RecursiveArrayIterator.
If the array value is an array, I will open a DIV so the "subarray" will be inside this DIV. Something like
$array = array(
'key0' => '0',
'key1' => array(
'value0' => '1',
'value1' => '2',
),
'key2' => '3',
'key3' => array(
'value2' => '4',
'value3' => array(
'value4' => '5'
),
'value4' => array(
'value5' => '6'
),
),
);
Then the HTML should be:
<div>
<div>
<p>key0 is 0</p>
</div>
<div>
<p>key1</p>
<div>
<p>value0 is 1</p>
<p>value1 is 2</p>
</div>
</div>
<div>
<p>key2 is 3</p>
</div>
<div>
<p>key3</p>
<div>
<p>value2 is 4</p>
<p>value3</p>
<div>
<p>value4 is 5</p>
</div>
<p>value4</p>
<div>
<p>value5 is 6</p>
</div>
</div>
</div>
</div>
But the problem is my code can only close 1 <div> tag each time. I have no idea how to remember how deep was there. So I can close to a for loop and echo </div>.
My current code:
<?php
echo '<div>';
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($iterator_array), RecursiveIteratorIterator::SELF_FIRST);
$is_start = true;
$last_element = '';
foreach($iterator as $key => $value) {
if(is_array($value) && $is_start) {
echo '<div><p>' . $key . '</p>';
$is_start = false;
$last_element = end($value);
} elseif(is_array($value) && !$is_start) {
echo '</div><div><p>' . $key . '</p>';
$last_element = end($value);
} elseif(!is_array($value)) {
echo '<div><p>' . $key . ' is ' . $value . '</p></div>';
if($last_element == $value) {
echo '</div>';
}
}
}
echo '</div>';
?>
Use this recursive function
May be it will help you
get_div($array);
function get_div($arr) {
foreach($arr as $k => $a){
echo '<div>';
if(is_array($a)) {
echo "<p>".$k."</p>";
get_div($a);
} else {
echo "<p>".$k." is ".$a."</p>";
}
echo '</div>';
}
}

Echo datas from an array with php

I have the following array in php:
$stats = array(
"Item 1" => 20,
"Item 2" => 30,
"Item 3" => 66,
"Item 4" => 1
);
I need to echo these values, so I try this:
<?
foreach ($stats as $stat => $data) {
echo '
<div class="col-sm-6">
<div class="widget">
<div class="widget-body p-t-lg">
<div class="clearfix m-b-md">
<h1 class="pull-left text-primary m-0 fw-500"><span class="counter" data-plugin="counterUp">'.$data.'</span></h1>
<div class="pull-right watermark"><i class="fa fa-2x fa-tv"></i></div>
</div>
<p class="m-b-0 text-muted">'.$stats[$stat].'</p>
</div>
</div>
</div>
';
}
?>
But I have only the numerical values echoed.
Do you have the solution ?
Thanks.
Since you are using the foreach construct, $stat holds the keys and $data holds the values. So when saying echo $stats[$stat] is equivalent to echoing the value that has the key $stat. If you want to echo the keys you should do this : echo $stat.
Here you are trying to print the index of the array,
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
echo $key;
Use this code to print the key of the array

How do I merge same array without show it duplicate

I want to merge array which have same key to be one. Example
$options = array(
array("group" => "header","title" => "Content 1"),
array("group" => "header","title" => "Content 2"),
array("group" => "menu","title" => "Content 3"),
array("group" => "content","title" => "Content 4"),
array("group" => "content","title" => "Content 5"),
array("group" => "content","title" => "Content 6"),
array("group" => "footer","title" => "Content 7")
);
foreach ($options as $value) {
if ($value['group']) {
echo "<div class='{$value['group']}'>";
echo $value['title'];
echo "</div>";
}
}
Current output is
<div class='header'>Content 1</div><div class='header'>Content 2</div><div class='menu'>Content 3</div><div class='content'>Content 4</div><div class='content'>Content 5</div><div class='content'>Content 6</div><div class='footer'>Content 7</div>
What I want here is to be
<div class='header'>
Content 1
Content 2
</div>
<div class='menu'>
Content 3
</div>
<div class='content'>
Content 4
Content 5
Content 6
</div>
<div class='footer'>
Content 7
</div>
Let me know
$grouped = array();
foreach($options as $option) {
list($group, $title) = array_values($option);
if (!isset($grouped[$group])) {
$grouped[$group] = array();
}
$grouped[$group][] = $title;
}
foreach ($grouped as $group => $titles) {
echo sprintf('<div class="%s">%s</div>', $group, implode('', $titles));
}
$groups = array ();
foreach ( $options as $value ) {
if ( !isset ( $groups[$value['group']] ) ) {
$groups[]['group'] = $value['group']
}
$groups[$value['group']]['title'][] = $value['title'];
}
foreach ( $groups as $group ) {
echo "<div class="{$group['group']}">";
echo implode ( "\n", $group['title'] );
echo "</div>";
}
This should work, but if it doesn't matter to you, you could also just change the structure of your hardcoded-array, then you wouldn't need my first foreach.

Categories