如何从 php 包含文件 switch/case 语句传回 html

How can I pass html back from a php include files switch/case statement

我正在使用 href 标签来更改我网站内容区域中的页面。我目前正在通过一个函数调用内容,效果很好。但是,每当我 运行 进入包含多个数字的内容时,我都在努力清除内容区域并显示大图像。我正在尝试使用 href 标记调用带有 switch 语句的 PHP 文件。代码如下。我怎样才能让它工作?

这是ranch_signs.php中的调用部分:

<div class="row">
  <div class="outlet-covers-item col-md-6">  
 <figure>
   <a href="?page=enlarge_image&?id=1"><img src="images/c-lazy-3.jpg" alt="Oval shape custom metal ranch sign"></a>
   <figcaption>
     <h4>Oval Shape Sign </h4>
     black finish<br> 
     custom metal sign <br>
     <a href="?page=enlarge_image&?id=2">Enlarge Images</a>
   </figcaption>
    </figure><br><br>
  </div>
  <div class="outlet-covers-item col-md-6">  
 <figure>
   <a href="?page=enlarge_image&?id=cl2"><img src="images/forman-mizpah.jpg" alt="Overhead custom metal ranch sign"></a>
   <figcaption>
  <h4>Overhead Ranch Sign </h4>
  custom metal sign <br>
  <a href="?page=enlarge_image&?id=cl2">Enlarge Images</a>
   </figcaption>
 </figure><br><br>
  </div>
</div>

这是enlarge_image.php:

<?php switch ($id) { 
  case 1: ?>
    <figure>
   <img src="images/outlet-1.jpg" alt="Proud Stallion custom metal outlet cover">
   <figcaption>
     <h4>Proud Stallion</h4>
     black painted Outlet Cover<br>
     <a href="?page=outlet_covers">Shrink Images</a>
   </figcaption>
    </figure><br><br>
  <?php break; ?>
  
  <?php case 2: ?>
    <figure>
   <img src="images/outlet-2.jpg" alt="Bull Moose Copperized finish custom metal outlet cover">
      <figcaption>
     <h4>Bull Moose</h4>
     Copperized finish<br>
     <a href="?page=ranch_signs_enlarged">Shrink Images</a>
      </figcaption>
    </figure><br><br>
  <?php break; ?>
<?php } ?>

有没有办法让这段代码起作用?

您的链接包含一个额外的问号“?”作为 URL 的一部分;我认为这是不正确的,这意味着您的链接指向了错误的位置。

例如:

<a href="?page=enlarge_image&?id=cl2">Enlarge Images</a>

我假设应该是:

<a href="?page=enlarge_image&id=cl2">Enlarge Images</a>

根据您的编辑,您似乎假设 id 的 GET 参数只是直接传递到 PHP 代码中,并且可以通过 $id.

您需要通过输入流读入这个变量。

enlarge_image.php 文件应如下所示:

    <?php 

$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_INT); // This line reads in 'id' sent through the URL into the variable $id

switch ($id) { 
      case 1: ?>
        <figure>
          <img src="images/outlet-1.jpg" alt="Proud Stallion custom metal outlet cover">
          <figcaption>
            <h4>Proud Stallion</h4>
            black painted Outlet Cover<br>
            <a href="?page=outlet_covers">Shrink Images</a>
          </figcaption>
        </figure><br><br>
      <?php break; ?>

      <?php case 2: ?>
        <figure>
          <img src="images/outlet-2.jpg" alt="Bull Moose Copperized finish custom metal outlet cover">
          <figcaption>
            <h4>Bull Moose</h4>
            Copperized finish<br>
            <a href="?page=ranch_signs_enlarged">Shrink Images</a>
          </figcaption>
        </figure><br><br>
      <?php break; ?>
    <?php } ?>