【问题标题】:How to make echo results in table hyperlinks如何在表格超链接中制作回显结果
【发布时间】:2014-03-20 18:34:41
【问题描述】:

我已从 DB 中检索数据并插入到 html 表中,但是我想让表中的每个值都成为指向另一个页面的超链接。下面我尝试制作瞳孔 ID 并链接到 profile.php,但所有瞳孔 ID 值现在都消失了!

(if (!isset($_POST['search'])) {
                    $pupils = mysql_query("SELECT * FROM pupil") or die("Cant find         Pupils");
                    $count = mysql_num_rows($pupils);
                    if ($count == 0) {
                        $totalpupil = "There are currently no Pupils in the system.";
                    } else {
                        while ($row = mysql_fetch_array($pupils)) {
                            ?>
                            <tr>
                                <td><?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?></td>
                                <td><?php echo $row['pupil_name'] ?></td>
                                <td><?php echo $row['class_id'] ?></td>                        
                            </tr>
                            <?php
                        }
                    }
                })

整理表应将每个超链接显示为指向另一个页面的超链接。有什么帮助吗?

【问题讨论】:

  • 你错过了 ... 的结束

标签: php sql hyperlink html-table echo


【解决方案1】:

由于您的 HTML 无效,您缺少结束 &gt; 并且您没有为超链接定义文本

<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?>   //Wrong

正确的是

<?php echo '<a href="profile.php?id='.$row['pupil_id'].'">'.$row['pupil_id'].'</a>'; ?>

【讨论】:

  • 为有用的答案干杯。设置 ['search'] 时,我将如何重新填充表格。例如,当用户提交搜索时,我需要用与其搜索相关的学生填充表格...
  • 您可以为您的第一个 if 编写一个 else 块,然后在您的查询中添加一个 WHERE 子句,其余部分保持不变
【解决方案2】:

尝试替换这个:

<?php echo '<a href="profile.php?id=' .$row['pupil_id'] . '"</a>' ?>

用这个:

<?php echo "<a href='profile.php?id=".$row['pupil_id']."'>link</a>"; ?>

另外,你根本没有&lt;table&gt; 标签。

【讨论】:

    【解决方案3】:

    您不要在链接标签之间放置任何文字,&lt;a href=""&gt;text here&lt;/a&gt;

    也许这会对你有所帮助:

     <td><?php echo '<a href="profile.php?id=' .$row['pupil_id']  . '">'.$row['pupil_name'].'</a>' ?></td>
    

    【讨论】:

      【解决方案4】:

      http://uk3.php.net/mysql_query

      请注意,您从中学习的任何资源都可能已经过时了。 mysql_query 现已弃用。

      http://uk3.php.net/manual/en/ref.pdo-mysql.php 是替代品。

      这是我不久前写的使用 PDO 的入门指南(这更安全)。

      在 php 脚本需要访问您的数据库时包含此文件。一个示例文件名是“database.php”,但这是您的电话。将命名空间从“yourproject”设置为您的项目被调用的任何内容。更正数据库凭据以适合您的数据库

      希望这会为您省去很多麻烦!

      我在底部为您提供了一些示例用法。我记得当我刚开始时,有时很难得到明确的建议。

      //***** in a database class file*****/
      namespace yourproject;
      class Database {
      
          private $db_con = '';
      
          /*** Function to login to the database ***/
          public function db_login()
              {
                  // Try to connect
                  try{
                          // YOUR LOGIN DETAILS:
                          $db_hostname = 'localhost';
                                     $db_database = 'yourdatabasename';
                                      $db_username = 'yourdatabaseusername';
                                      $db_password = 'yourdatabasepassword';
      
                          // Connect to the server and select database
                          $this->db_con = new \PDO("mysql:host=$db_hostname;dbname=$db_database",
                                                      "$db_username",
                                                      "$db_password",
                                                      array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
      
                          // Prevent emulation of prepared statements for security
                          $this->db_con->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
                          $this->db_con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
      
                          return true;
                      }
                  // If it fails, send user to maintenance page
                  catch(PDOException $e)
                      {
                          header("location:http://yourwebsiteurl.com/maintenance.php");
                          exit();
                      }
              }
      
          /*** Function for database control ***/
          public function db_control($query , $parameters, $returnID = false)
              {
                  if(!is_array($query) && is_array($parameters))
                      {
                          try{
                                  //prepare the statement
                                  $statement = $this->db_con->prepare($query);
      
                                  //execute the statement
                                  $statement->execute($parameters);
      
                                  //check whether this is a select, if it is then we need to retrieve the selected data
                                  if(strpos($query, 'SELECT') !== false)
                                      {
                                          //fetch the results
                                          $result = array();
                                          while( $row = $statement->fetch(\PDO::FETCH_ASSOC) )
                                              {
                                                  $result[] = $row;
                                              }
      
                                          //count the results
                                          $count = count($result);
      
                                          //return the array
                                          return array( 'results' => $result, 'result_count' => $count );
                                      }
                                  //else return the number of affected rows
                                  else{
                                          //count the affected rows and place into a returnable array
                                          $affected_rows = $statement->rowCount();
                                          $returnArray = array('result_count' => $affected_rows);
      
                                          //check to see if we are to return a newly inserted autoincrement ID from an INSERT
                                          if($returnID)
                                              {
                                                  //find the newly created ID and add this data to the return array
                                                  $insertID = $this->db_con->lastInsertId();
                                                  $returnArray['ID'] = $insertID;
                                              }
      
                                          return $returnArray;
                                      }
                              }
                          catch(PDOException $e)
                              {
                                  return false;
                              }
                      }
                  else{
                          return false;
                      }
              }
      }
      
      // Start the database class and connect to the database then create a globally accessible function for ease of reference
      $db = new \yourproject\Database();
      $db->db_login();
      function _db( $sql , $params , $returnID = false ){
          return $GLOBALS['db']->db_control( $sql , $params , $returnID );
      }
      

      当您包含此文件时,您现在拥有一个新功能:_db()。由于该函数是全局函数,因此可以从任何类或 std 文件中调用它。当调用如下所示的变量时,将产生一个如下所示的数组:

      array(
         'result_count' => 3,
         'results' => array(
           array(/*row 1*/),
           array(/*row 2*/),
           array(/*row 3*/),
           .. etc etc
         )
      )
      

      现在在你的 php 脚本中包含你的数据库文件:

      //call in the database file
      require_once 'database.php';
      
      //your query as in the op
      $sql = 'SELECT * FROM pupil';
      //your params for the query
      $params = array();
      //running the query and getting the results returned into a variable called $query
      $query = _db($sql,$params);
      
      //if no results
      if( $query['result_count'] == 0 )
      {
          echo 'sorry no pupils in the system';
      }
      else
      {
          //looping through each result and printing into a html table row
          for( $i = 0 ; $i < $query['result_count'] ; ++$i )
          {
              echo '<tr><td><a href="profile.php?id=' . $query['results'][$i]['pupil_id'] . '"</a></td>';
              echo '<td>'. $query['results'][$i]['pupil_name'] . '</td>';
              echo '<td>'. $query['results'][$i]['class_id'] . '</td></tr>';
          }
      }
      

      您的原始查询,但传递了一些参数

      //Passing parameters to the query
      //your query
      $sql = 'SELECT * FROM pupil WHERE pupil_id = :pupil_id AND class_id = :class_id';
      //your params for the query
      $params = array(
                  ':pupil_id' => 12,
                  ':class_id' => 17,
              );
      //running the query and getting the results returned into a variable called $query
      $query = _db($sql,$params);
      
      //deal with the results as normal...
      

      如果将第三个参数设置为 true,则可以返回刚刚输入的行的自动 id,例如:

      //where $sql is a query that will INSERT a row
      $query = _db($sql,$params, true);
      

      【讨论】:

      • 感谢所有帮助人员 - 显然这个示例适用于未设置搜索的情况。我现在需要在搜索后使用相同的结果重新填充同一个表(设置搜索时) - 为此我到目前为止没有成功。这是一件容易的事吗?
      猜你喜欢
      • 1970-01-01
      • 2020-09-05
      • 2017-07-07
      • 2013-07-16
      • 2018-09-30
      • 1970-01-01
      • 2021-07-20
      • 2020-03-16
      • 1970-01-01
      相关资源
      最近更新 更多