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

时间:2014-08-04 05:53:21

标签: php arrays hyperlink

我正在投资组合网站并将所有项目都放在数组中,我创建了一个项目模板页面,该页面使用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

提前感谢您的帮助。 欢呼声。

1 个答案:

答案 0 :(得分:0)

传递索引而不是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;