【问题标题】:How to increase/decrease the number in $_GET variable link?如何增加/减少 $_GET 变量链接中的数字?
【发布时间】:2020-09-24 03:12:20
【问题描述】:

我有一个 PHP 页面,它利用 $_GET 变量“链接”在页面上显示特定图像。我的 PHP 页面 URL 看起来像“website.com/page.php?link=1”

PHP 代码:

$link = $_GET['link'];
if($link == 1) { $linkURL = "http://website.com/image1.jpg";}
if($link == 2) { $linkURL = "http://website.com/image2.jpg";}
if($link == 3) { $linkURL = "http://website.com/image3.jpg";}

HTML

<img src="<?=$linkURL;?>"></img>

我想在这个页面上添加“下一个”和“上一个”按钮,点击后会转到下一个/上一个 $link URL。

<a class="prevBtn" href="">Previous</a>
<a class="nextBtn" href="">Next/a>

为了实现这一点,我应该把什么作为 href?基本上,我希望它是“$link -1”代表上一个,“$link +1”代表下一个。

提前感谢您的帮助!

【问题讨论】:

  • 您实际上在问题中写下了您的答案。遵循你的逻辑!
  • 所以是 href="=$linkURL+1;?> ?分号是在 +1 之前还是之后?
  • 正如您在评论示例中所拥有的那样。
  • 执行 Previous 不会产生正确的链接。它打印出“website.com/-1”而不是“website.com/page.php?link=-1”

标签: php html


【解决方案1】:

你试试可以通过url作为参数传递

<a class="prevBtn" href="<?php echo $linkURL?>?link=-1">Previous</a>
<a class="nextBtn" href="<?php echo $linkURL?>?link=+1">Next/a>

【讨论】:

  • 这很接近,但不能完全达到正确的效果。在您的示例中,它回显了 $linkURL,它是指向图像的链接。所以显示的 URL 是“website.com/image1.jpg?link=-1”,这不是我们想要的。假设当前 URL 是“website.com/page.php?link=2”我希望上一个按钮是“website.com/page.php?link=1”知道如何解决?
  • 你想要类似轮播的效果吗?
  • 即使我们在for循环中设置它最终也会增加锚标签的数量。
  • 不需要轮播效果。我只是想要它到下一个或上一个链接。
  • 如果当前 URL 是“website.com/page.php?link=2”,我希望下一个按钮链接到“website.com/page.php?link=3”并且上一个按钮链接到“website.com/page.php?link=1”
【解决方案2】:

假设您的图像始终是 image1.jpg、image2.jpg、image3.jpg 等,您可以像这样实现您想要的:

注意:我只使用 PHP 只是为了更容易遵循逻辑

<?php

//Check $_GET variable exists otherwise set it to a default value = 1

if(isset($_GET['link'])) { 
    $link = $_GET['link'];
} else {
     $link = 1;
}

if($link == 1) { $linkURL = "http://website.com/image1.jpg";}
if($link == 2) { $linkURL = "http://website.com/image2.jpg";}
if($link == 3) { $linkURL = "http://website.com/image3.jpg";}

// Set variables for the previous and next links
$prev = $link-1;
$next = $link+1;

//Display your image    
echo "<img src='".$linkURL."'></img>";

//Only show the previous button if it's NOT the first image
if($prev!=0){
     echo "<a class='prevBtn' href='http://website.com/page.php?link=".$prev."'>Previous</a>";
}

//Only show the next button if it's NOT the last image
if($next<=3){
    echo "<a class='nextBtn' href='http://website.com/page.php?link=".$next."'>Next</a>";
}
?>

【讨论】:

  • 图片不一定都是image1.jpg、image2.jpg等,图片名称差异很大。
  • 所以我需要保留$link = $_GET['link']; if($link == 1) { $linkURL = "http://website.com/image1.jpg";} if($link == 2) { $linkURL = "http://website.com/image2.jpg";} if($link == 3) { $linkURL = "http://website.com/image3.jpg";}
  • 对不同的命名图像进行了小修改
  • 它看起来不错,但它给了我“500 Internal Server Error”。
  • 它说“PHP 解析错误:语法错误,第 45 行 /file-path/page.php 中的意外 '{'
【解决方案3】:

另一种方法是使用数组。这样做的好处是您只需将新图像添加到数组中,它仍然可以工作。您不必担心编辑下一个按钮周围的其他 if 语句。

<?php

//Check $_GET variable exists otherwise set it to a default value = 1

$images = [
"image1.jpg",
"image2.jpg",
"image3.jpg",
];

if(isset($_GET['link']) { 
    $link = $_GET['link'];
} else {
     $link = 0;
}

if(!$images[$link]) {
    $link=0;
}

// Set variables for the previous and next links
$prev = $link-1;
$next = $link+1;

//Display your image    
echo "<img src='http://website.com/".$images[$link]."'></img>";

//Only show the previous button if it's NOT the first image
if($prev<0){
     echo "<a class='prevBtn' href='http://website.com/page.php?link=".$prev."'>Previous</a>";
}

//Only show the next button if it's NOT the last image
if($next!=count($images)){
    echo "<a class='nextBtn' href='http://website.com/page.php?link=".$next."'>Next</a>";
}
?>

【讨论】:

    猜你喜欢
    • 2016-01-17
    • 2014-02-15
    • 1970-01-01
    • 2019-10-17
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多