【问题标题】:Creating Next/Previous pagination links创建下一个/上一个分页链接
【发布时间】:2014-09-26 13:27:41
【问题描述】:

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

提前感谢您的帮助。 干杯。

【问题讨论】:

  • 你真的需要使用项目名称作为数组键吗?你可以只使用数字,这样更容易实现你想要的。
  • 我使用项目名称作为数组键,因为这是我发现使用 Rewrites 最容易清理我的 url 的方式,例如项目的 url 将是 /projects/Beeston-Residence,我仍然在学习 php 的过程中,这样的决定是一次决定一个问题,而不考虑它会如何影响其他事情。没有数字作为关键,是不是更难实现?

标签: php arrays hyperlink


【解决方案1】:

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

【讨论】:

  • 感谢 Stefan,但不幸的是,Href 无法正常工作,因为以前的结果是 &lt;a href="?id=-1"&gt;←&amp;nbsp;Previous&lt;/a&gt;
  • @VinceBrown:好的,我做了一些修改 - 试试看?
  • Stefan 对不起,我没有回复你!感谢您的尝试,但不幸的是那些也没有工作。这一切都是为了一个学校项目,截止日期很快,所以现在我只是把它放在次要位置并继续前进。如果您有任何其他想法,我愿意接受建议。再次感谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2013-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2014-12-14
  • 1970-01-01
相关资源
最近更新 更多