2017-08-10 2 views
2

だから私は何らかのデータベースを複製するためにdb.phpというphpファイルを持っています。PHPファイルの情報を含むページの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 --> 
:私はページヘッダタイトル、サブタイトル、画像およびいくつかのポリゴンポイントを変更する必要がある部分要素を持つ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セクションの要素でわかるように、私はすべての情報を手書きで書きました。しかし、私の目標は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リクエストが行われます。
その要求は(あなたの例に基づいて)このように、&で区切って、あなたが必要なすべてのパラメータを、あなたのURLの末尾に?を追加することによって、パラメータの形式で変数をとることができます。

nameofthepage.php?id=1&another=2

PHPで

は、あなたはこのように、スーパーグローバル$_GETを使用してこれらのパラメータをキャッチすることができます:今、あなたは理解しておくべきことで

$id = $_GET['id'];

@Spectarionの答えは、あなたの問題を解決します。
これはPHPでHTTPを操作するための基本であるため、答えは正しいとは思わないと思いますが、それはあなた次第です。

関連する問題