0

そのため、現在、.htaccess やモジュールを使用せずに動的 URL とそのパスを生成しようとしています。基本的には、Apache が提供するさまざまなローカルホストでステートレスに動作する URL パスを書き直して出力しようとしています。

次のようなクエリ文字列を介して URI を取得する index.php が必要です (明らかにコードではありません)。

-> index.php  (echos dynamically generated URL hyperlinks)      
-> index.php?q=/   (echos every URI in JSON)                       
-> index.php?q=/some_id   (outputs table some_id info w/ links to reviews)                            
-> index.php?q=/some_id/reviews    (outputs table column of reviews w/ link to review_ids)                    
-> index.php?q=/some_id/reviews/review_id   (output related column info)

誰かが私がこれを行う方法を説明してもらえますか? $_SERVER メソッドを使用して URL を記述し、テーブル ID の配列を反復処理しながら爆発する必要があると思います..?

どんな助けでも大歓迎です!


編集:
これが私が書こうとしていたコードです:/

  <?php
     $db = $user = $pw = 'logininfo';

     try {
        $dbconn = new PDO('mysql:host=localhost;db='.$db, $user, $pw;   
     }
     catch (Exception $e) {
        echo "Error: ";
        echo $e->getMessage();
     }
  ?>

  <!DOCTYPE HTML>
  <head>
      <title>Product Reviews</title>
  </head>
   <body>
     <h1>Product List:</h1>
        <h2>
           <ul>
              <?php    
                  try {
                     $sql = "SELECT somename, some_id FROM products";
                 $stmt = $dbconn->query($sql);
                 if($stmt !== false) {
                 foreach($stmt as $row) {  //output just name
                     echo "<li>";
                 echo htmlentities($row['somename'])."<br />";

                 if($stmt !== false) {
                    $url = "<a href=index.php?q=".
                                htmlentities($row['some_id'])."/>".
                                htmlentities($row['somename'])."'s Review</a>";

                    echo $url;         //output URL
                    echo "</li>"."<br />";
                 }
                 else {
                    echo "Unnecessary";
                 }
                 }
                 if($_GET['url']) {   //don't really know what to put here
                    header("Location: $url");   //can't use headers anyway
                 }  
                }
                $stmt = null;
                 }
                 catch (PDOEXCEPTION $e) {
                echo $e->getMessge();
                 }
         ?>
    </ul>
    </h2>
   </body>
  </html>
4

2 に答える 2

2

URL は次のように記述できます。

http://example.org/index.php/reviews/id/ [where id can be your review id]

in を使用$_SERVER['PATH_INFO']index.phpて の後にある URL の一部を取得しindex.php、テキストを分解して目的のデータを取得します。

<?php
    $query_string = explode('/', $_SERVER['PATH_INFO']);

    switch(count($query_string)) {
        case 2:
            $some_id = (int) $query_string[1];
            if ($some_id === 0) {
                //(echos every URI in JSON)

            }
            else {
                // (outputs table some_id info w/ links to reviews)

            }

            break;

        case 3:
            //(outputs table column of reviews w/ link to review_ids)
            $some_id = (int) $query_string[1];
            $table_name = $query_string[2];
            break;

        case 4:
            //(output related column info)
            $some_id = (int) $query_string[1];
            $table_name = $query_string[2];
            $review_id = (int) $query_string[3];
            break;

        default:
            // Error case
    }
?>
于 2012-05-04T18:37:19.797 に答える
1

サイズはこちらをお試しください

if (isset($_GET['q']) && !empty($_GET['q']))
{
    $params = explode("/",$_GET['q']);

    if (isset($params[3]) && !empty($params[3]))
        echo "(output {$params[2]} {$params[3]} info)";

    else if (isset($params[2]) && !empty($params[2]))
        echo "(outputs table column of {$params[2]} w/ link to review_ids)";

    else if (isset($params[1]) && !empty($params[1]))
        echo "(outputs table {$params[1]} info w/ links to reviews)";
    else
        echo "(echos every URI in JSON) ";


}
else
    echo "(echos dynamically generated URL hyperlinks)";
于 2012-05-04T18:48:10.867 に答える