簡體   English   中英

創建下一個/上一個分頁鏈接

[英]Creating Next/Previous pagination links

我正在一個投資組合站點上工作,並且所有項目都處於陣列中,我創建了一個項目模板頁面,該頁面使用GET變量設置當前項目並將適當的內容加載到頁面中。 我正在嘗試在底部添加指向下一個和上一個項目的鏈接,但無法找出編寫鏈接的正確方法

這是數組的樣子:

$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!"
);

等等...陣列共有15個項目

項目模板過濾到的項目設置如下:

<?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");
?>

我很幸運通過使用以下命令來檢索數組中的當前項目位置:

 <?php $key = array_search($project, $projects); 
    $offset = array_search($key, array_keys($projects));
    $next = $offset + 1;
    $prev = $offset - 1;
    ?>

但是在弄清楚如何使用此信息創建到數組中下一個項目的鏈接時遇到了麻煩

我正在嘗試類似的事情,但不知道該把什么作為鏈接

    <?php if ($prev > 0): ?>
        <a style ="color:black" href="">Previous</a>
    <?php endif; ?>

    <?php

預先感謝您的幫助。 干杯。

傳遞索引而不傳遞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;

或者,如果您想傳遞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