简体   繁体   English

创建下一个/上一个分页链接

[英]Creating Next/Previous pagination links

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. 我正在一个投资组合站点上工作,并且所有项目都处于阵列中,我创建了一个项目模板页面,该页面使用GET变量设置当前项目并将适当的内容加载到页面中。 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 等等...阵列共有15个项目

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: 传递索引而不传递id可能更好,更简单:

// 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 = "&larr;&nbsp;Previous";
    $html .= "<a href='?i=$i_prev'>$label</a>";
}

if ($i < ($l - 1)) {
    $i_next = $i + 1;
    $label = "Next&nbsp;&rarr;";
    $html .= "&nbsp;<a href='?i=$i_next'>$label</a>";
}
echo $html;

Or if you want to pass ids: 或者,如果您想传递ID:

$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 = "&larr;&nbsp;Previous";
    $html .= "<a href='?id=$id_prev'>$label</a>";
}

if ($index < ($l - 1)) {
    $id_next = $lst_ids[$index + 1];
    $label = "Next&nbsp;&rarr;";
    $html .= "&nbsp;<a href='?id=$id_next'>$label</a>";
}
echo $html;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM