2017-08-10 43 views
2

所以,我有一个名为db.php的PHP文件来复制某种数据库。这基本上是与一些多维关联数组文件:如何使用PHP文件中的信息动态更改页面的HTML内容?

<?php 
$arrShowcases = array(
    "1"=>array("title"=>"Landing Page", "descr"=>"Welcome!", "img"=>"actions-landing.jpg", "polygon"=>array(
     0=> array(
       "points"=>"1338,4,1338,50,1272,50,1272,4", 
       "link"=>"69", 
       "modalbody"=>"This button lets you Signup on the webapp", 
       "modaltitle"=>"Signup Button" 
      ), 
     1=> array(
       "points"=>"1246,12,1249,44,1206,44,1204,14", 
       "link"=>"2", 
       "modalbody"=>"This button redirects you for the login form", 
       "modaltitle"=>"Login Button" 
      ) 
    )), 
    "2"=>array("title"=>"Login page", "descr"=>"Make your login", "img"=>"actions-login.jpg", "polygon"=>array(
     0=> array(
      "points"=>"1338,4,1338,50,1272,50,1272,4", 
      "link"=>"69", 
      "modalbody"=>"This button lets you Signup on the webapp", 
      "modaltitle"=>"Signup Button" 
      ), 
     1=> array(
      "points"=>"1246,12,1249,44,1206,44,1204,14", 
      "link"=>"69", 
      "modalbody"=>"This button redirects you for your dashboard", 
      "modaltitle"=>"Login Button" 
      ) 
    )) 
); 
?> 

然后,我有一个HTML页面,我需要改变页面标题标题,字幕,图片和一些多边形点的部分元素:

<section> 
    <div class="container"> 
     <div class="row"> 
     <div class="col-md-3"> 
      <?php include cfgPath."/includes/menu.html"; ?> 
     </div> 
     <div class="col-md-9"> 
      <h1 class="page-header"><i class="glyphicon glyphicon-picture"></i> Landing page</h1> 
      <h3>// Lorem ipsum...</h3> 
      <div class="col-md-12"> 
      <div style="position:relative;"> 
       <img id="pageScreenshot" src="<?php echo cfgRoot; ?>/assets/images/actions-landing.jpg" class="img-responsive" alt="Landing Page"/> 
       <svg id="svgImageMap" width="1366" height="768"> 
       <polygon points="1338,4,1338,50,1272,50,1272,4" data-originalpoints="1338,4 1338,50 1272,50 1272,4" data-id="0"/> 
       <polygon points="1246,12,1249,44,1206,44,1204,14" data-originalpoints="1246,12 1249,44 1206,44 1204,14" data-id="1"/> 
       <polygon points="378,43,446,42,445,11,377,9" data-originalpoints="378,43 446,42 445,11 377,9" data-id="2"/> 
       <polygon points="196,10,367,10,366,42,195,43" data-originalpoints="196,10 367,10 366,42 195,43" data-id="3"/> 
       <polygon points="121,16,120,35,164,39,164,17" data-originalpoints="121,16 120,35 164,39 164,17" data-id="4"/> 
       <polygon points="14,15,13,40,95,43,95,12" data-originalpoints="14,15 13,40 95,43 95,12" data-id="5"> 
       </svg> 
      </div> 
      <!-- data original points: X1,Y1 X2,Y2 X3,Y3 X4,Y4 --> 
      </br> 
      <a class="btn btn-success btn-sm btn-block" href="<?php echo cfgRoot; ?>/app/showcase/showcaseView.php">Go Back</a> 
      </div> <!-- colmd12 --> 
     </div> <!-- colmd9 --> 
     </div> <!-- row --> 
    </div> <!-- container --> 
    </section> <!-- section --> 

正如你可以在section元素的上面的html中看到的那样,我手写了所有的信息。但是,我的目标是用db.php文件提供的东西来更改这些信息。

我想要像这样nameofthepage.php?id = 1(并且数组的位置1中提供的所有信息都转到html)或这个:nameofthepage.php?id = 2(并提供所有信息在数组的位置2转到html)。任何建议或提示,以获得此行为?

到目前为止,我试图做回声是这样的:

<?php echo $arrShowcases[positions-that-i-want]["information-that-i-want"]; ?> 

改变硬编码的HTML但是,这并不让我的动态行为,我需要。

+0

你有什么试过?它看起来像你要求别人为你做。所有你需要做的就是用'$ _GET ['id']'得到'?id'参数,然后引用数组的适当部分,例如'$ arrShowcases [1] ['title']'是第1页的标题等... – Andy

+0

AJAX的典型案例。 – iquellis

+1

@iquellis不是真的,因为它可以在服务器上生成。不需要异步调用。 – Spectarion

回答

1

我希望你能明白这一点。

<?php 
    //----------------------------- 
    // If id is not sent then stop with page execution. You can redirect or something else. 
    if(!isset($_GET["id"])) { 
     die("ID not received."); 
    } 
    // Get that received id because at this point we are sure that id is received because page execution did not stopped before. 
    $id = $_GET["id"]; 
    //----------------------------- 
    // Import your "database". 
    require_once "db.php"; 
    //----------------------------- 
    // If there is no page with that id, stop page execution. 
    if(!isset($arrShowcases[$id])) { 
     die("page does not exists."); 
    } 
    // If there is a page then store it in a variable. 
    $page = $arrShowcases[$id]; 
    //----------------------------- 
?> 
<section> 
    <div class="container"> 
     <div class="row"> 
     <div class="col-md-3"> 
      <?php include cfgPath."/includes/menu.html"; ?> 
     </div> 
     <div class="col-md-9"> 
      <h1 class="page-header"> 
       <i class="glyphicon glyphicon-picture"></i> 
       <?= $page['title']; ?> // Echo page title. 
      </h1> 
      <h3><?= $page['descr']; ?></h3> // Echo page description. 
      <div class="col-md-12"> 
      <div style="position:relative;"> 
       <img id="pageScreenshot" src="<?php echo cfgRoot; ?>/assets/images/<?= $page['img']; ?>" class="img-responsive" alt="<?= $page['title']; ?>"/> // Echo image filename and page title. 
       <svg id="svgImageMap" width="1366" height="768"> 
       // For each polygon, echo it's points and key as an index (0, 1, 2, 3...) 
       <?php foreach ($page["polygon"] as $key => $value) { ?> 
        <polygon points="<?= $value['points']; ?>" data-originalpoints="<?= $value['points']; ?>" data-id="<?= $key; ?>"/> 
       <?php } ?> 
       </svg> 
      </div> 
      <!-- data original points: X1,Y1 X2,Y2 X3,Y3 X4,Y4 --> 
      </br> 
      <a class="btn btn-success btn-sm btn-block" href="<?php echo cfgRoot; ?>/app/showcase/showcaseView.php">Go Back</a> 
      </div> <!-- colmd12 --> 
     </div> <!-- colmd9 --> 
     </div> <!-- row --> 
    </div> <!-- container --> 
    </section> <!-- section --> 
+0

你在开玩笑吗?对于任何具有常识的人来说都有很好的文档记录。 – Spectarion

+0

我觉得这里没有什么要说明的。这是自我解释。如果他能够自己创建这个多维数组,我相信他会弄明白的。如果你没有提供更好的答案,请停止评论。 – Spectarion

+0

我的评论已经改善了你的回答,但是@Andy给出了一个更好的评论,不需要为点评。 SO是一个交流意见的社区,学会以积极的方式处理这个问题。 – Narayon

1

当您在浏览器中输入URL时,您正在发出GET HTTP请求。该请求可以采取变量参数的形式,加入?到您的网址的结尾,然后你需要的所有参数,通过&分离,像这样(根据你的例子)

nameofthepage.php?id=1&another=2

在PHP中,你可以使用超全局$_GET,像这样赶上这些参数:

$id = $_GET['id'];

现在你应该明白@Spectarion个答案,解决了你的问题。
我不认为任何答案应该被标记为正确的,因为这些是使用PHP使用HTTP的基础知识,但这取决于您。

相关问题