在PHP中没有提交表单时使用$ _GET

时间:2016-10-25 06:18:43

标签: php html

这是我的代码:

<?php 

require_once('config.php'); 

/*
 Displays the list of artist links on the left-side of page
*/
function outputArtists() {
   try {
         $pdo = new PDO(DBCONNSTRING,DBUSER,DBPASS);
         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         $sql = "select * from Artists order by LastName limit 0,15";
         $result = $pdo->query($sql);
         while ($row = $result->fetch()) {
            echo '<a href="lab11-exercise10.php?id=' . $row['ArtistID'] . '" class="list-group-item';
            if (isset($_GET['id']) && $_GET['id'] == $row['ArtistID']) echo ' active';
            echo '">';
            echo $row['LastName'] . '</a>';
         }
         $pdo = null;
   }
   catch (PDOException $e) {
      die( $e->getMessage() );
   }
}

/*
 Displays the list of paintings for the artist id specified in the id query string
*/
function outputPaintings() {
   try {
      if (isset($_GET['id']) && $_GET['id'] > 0) {   
         $pdo = new PDO(DBCONNSTRING,DBUSER,DBPASS);
         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

         $sql = 'select * from ArtWorks where ArtistId=' ;
         $id =  $_GET['id'];
         $statement = $pdo->prepare($sql);
         $statement->bindValue(':id', $id);
         $statement->execute();
         while ($row = $statement ->fetch()) {
            outputSinglePainting($row);         
         }
         $pdo = null;
      }
   }
   catch (PDOException $e) {
      die( $e->getMessage() );
   }
}

/*
 Displays a single painting
*/
function outputSinglePainting($row) {
   echo '<div class="media">';
   echo '<a class="pull-left" href="#">';
   echo '<img class="media-object" src="images/art/works/square-medium/' . $row['ImageFileName'] .'.jpg">';
   echo '</a>';
   echo '<div class="media-body">';
   echo '<h4 class="media-heading">';
   echo htmlentities($row['Title'], ENT_IGNORE | ENT_HTML5, "ISO-8859-1"); 
   echo '</h4>';
   echo $row['Description']; 
   echo '</div>';
   echo '</div>';
}

?>






<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Chapter 11</title>

    <!-- Bootstrap core CSS  -->    
    <link href="bootstrap3_defaultTheme/dist/css/bootstrap.css" rel="stylesheet"> 

  </head>
<body>

<div class="well"><h1>User Input (pdo)</h1></div>

<div class="container">
   <div class="row">

      <div class="col-md-3">
         <div class="list-group">
            <?php outputArtists(); ?>      
         </div>
      </div>
      <div class="col-md-9">
         <?php outputPaintings(); ?>  
      </div>

   </div>
</div>

</body>
</html>

我不太确定$ _GET ['id']是什么意思。根据我的理解,我们在提交类似提交名称=“XXX”的表单时使用$ _GET,然后我们使用$ _GET ['XXX']。但是这段代码中没有'id',所以为什么它使用$ _GET ['id']&gt; 0或$ _GET ['id'] == $ row ['ArtistID']?在这种情况下,$ _GET ['id']的值是什么意思?

1 个答案:

答案 0 :(得分:0)

好的,让我们这样说吧,你在页面$a中有一个变量pageA.php 现在,您希望在GET$a变量pageB.php的内容用于任何用途。

因此,要将变量$a传递给pageB.php,请使用以下内容传递:pageB.php?value=$apageB.php?id=$apageB.php?whatever=$a

现在,你的问题必须是“这是做什么的?” 好吧,这个'值'或'id'或'what',只是简单地传递到$_GET[]数组

所以,如果你想在pageB.php中使用它,你所要做的只是$get_a = $_GET['value']

希望说清楚!