在没有已知密钥的情况下在php中迭代多维数组

时间:2017-02-28 15:56:55

标签: php multidimensional-array

我正在编写一个Web应用程序,我相信其中一个部分需要一个 多维数组。该数组包含数据库中的应用程序列表。 我希望能够通过个人名称或显示应用程序列表 一个唯一的ID。我有这部分工作。然后我想点击一个人 应用程序,只需提取该特定行信息即可填写表格。 目前,当我这样做时,它会调出数据库中的所有行或 仅第一行。有没有人有什么建议? 我的解释不是很好,所以我要包含部分代码。对不起 它太久了。我试图尽可能地减少它。即使它包括在内 在代码中,我没有包含config.php,因为它只是我的数据库连接。

userList.php:

<?php
include("config.php");

?>
<!DOCTYPE html>
<html>
    <body>
        <h1>Test</h1>
    <p><b><u>Users</b></u></p>
  </body>
</html>
<?php
require_once("/class/users.php");
$rowt = array(array());
$rowt = users::fillForm($rowt);
foreach($rowt as $test) {
    if(is_array($test))
    {
    echo "<a href='userDisplay.php'>".$test['name']."</a><br/>";
    }
}
?>

userDisplay.php:

<!DOCTYPE html>
 <html>
   <body>
        <h1>Tester</h1>
      <?php
      include("config.php");
      //declare array
      $rowt = array(array());
      //pass array into class function
      //since functions can't return more than one variable, you have to pass the
      //array and set it equal to the original variable while calling the pdo function
      $rowt = users::fillForm($rowt);
        foreach($rowt as $test=> $rowt){
      ?>
      <h2>Application for <?php echo $rowt['name']?></h2>
       <table>
         <tr><th><b>Name</b></th>
             <th><b>Phone Number</b></th>
             <th><b>Best Time to Call<b></th>
         </tr>
         <tr></tr>
         <tr><td><output type='text' maxlength="30" required name='name'><?php echo $rowt['name']?></output></td>
             <td><output type="text" maxlenth="30" required  name="p_num"><?php echo $rowt['phone_number']?></output></td>
             <td><output type='text' maxlength="30" required name='bc_time'><?php echo $rowt['best_call_time']?></output></td></tr>
         <tr></tr>
         <tr>
             <th><b>Visa Status<b></th>
             <th><b>IT Experience<b></th>
             <th><b>Relevant Experience<b></th>
         </tr>
         <tr></tr>
         <tr><td><output type='text' maxlength="30" required name='v_status'><?php echo $rowt['visa_status']?></output></td>
             <td><output type='text' maxlength="30" required name='it_exp'><?php echo $rowt['it_exp']?></output></td>
             <td><output type='text' maxlength="30" required name='rel_exp'><?php echo $rowt['relevant_exp']?></output></td>
         </tr>
         <tr></tr>
         <tr>
             <th colspan="3"><b>Description<b></th>
         </tr>
         <tr></tr>
         <tr>
             <td colspan="3"><output name="description" rows="4" cols="100"></output><?php echo $rowt['description']?>></td>
         </tr>

    </table>
  </body>
</html>
<?php
}
echo "<a href='userList.php'>Back</a>";
?>

users.php用户类的函数:

public function insertForm() {
    $correct = false;
    try {
          $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
          $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          $sql = "INSERT INTO user(name, phone_number, best_call_time, description,
          visa_status, it_exp, relevant_exp) VALUES(:name, :p_num, :bc_time, :description,
          :v_status, :it_exp, :rel_exp)";

          $stmt = $con->prepare($sql);
          $stmt->bindValue("name", $this->name, PDO::PARAM_STR);
          $stmt->bindValue("p_num", $this->p_num, PDO::PARAM_STR);
          $stmt->bindValue("bc_time", $this->bc_time, PDO::PARAM_STR);
          $stmt->bindValue("v_status", $this->v_status, PDO::PARAM_STR);
          $stmt->bindValue("it_exp", $this->it_exp, PDO::PARAM_STR);
          $stmt->bindValue("rel_exp", $this->rel_exp, PDO::PARAM_STR);
          $stmt->bindValue("description", $this->description, PDO::PARAM_STR);
          $stmt->execute();
          return "Entry Successful <br/> <a href='userForm.php'>Home</a>";
        }catch(PDOException $e) {
          return $e->getMessage();
        }
    }
  public static function fillForm($rowt) {
      $successt = false;
      try{
        $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $sql1 = "SELECT * FROM user";
        $stmt1 = $conn->prepare($sql1);
        $stmt1->execute();
        $rowt = $stmt1->fetchAll(PDO::FETCH_NUM&PDO::FETCH_ASSOC);
        return $rowt;
      }catch (PDOException $et) {
        echo $et->getMessage();
        return $successt;
      }
    }

1 个答案:

答案 0 :(得分:0)

这里有很多内容,但是如果我得到你的问题的要点,你希望能够在点击用户列表中的某一行时返回一个用户。为此,您需要更新SQL查询以拉取特定用户。有点像:

// Formatting into a class to cut down on repetition.
<?php 
class User {
  private $dbConnect;

  // functionally these two are similar but I separated users and user
  // for clarity of purpose.
  public function getUsers()
  {
    // Enumerating your select columns is clearer, and more efficient.
    $sql = "SELECT name, phone_number, best_call_time, description,
      visa_status, it_exp, relevant_exp 
      FROM user";

    $result = $this->makeQuery($sql);

    return ($result) ? $result : array();
  }

  public function getUser($name)
  {
    // Enumerating your select columns is clearer, and more efficient.
    $sql = "SELECT name, phone_number, best_call_time, description,
      visa_status, it_exp, relevant_exp 
      FROM user
      WHERE name = :name";

    $param = $this->prepareUserInfo(array('name' => $name));

    $result = $this->makeQuery($sql, $param);

    return ($result) ? $result : array();
  }

  public function createUser($userInfo)
  {
    $sql = "INSERT INTO user(name, phone_number, best_call_time, description,
    visa_status, it_exp, relevant_exp) VALUES(:name, :p_num, :bc_time, :description,
    :v_status, :it_exp, :rel_exp)";
    $params = $this->prepareUserInfo($userInfo);

    try {
      $this->connect();

      $stmt = $this->dbConnect->prepare($sql);
      $stmt = $this->bindParams($stmt, $data);
      $stmt->execute();
      return "Entry Successful <br/> <a href='userForm.php'>Home</a>";
    } catch(PDOException $e) {
      return $e->getMessage();
    }

  }

  private function prepareUserInfo($userInfo)
  {
    $infoArray = array();

    foreach ($userInfo as $key => $value) {
      // Going with your original code I'm hardcoding param type here, but 
      // you could easily write a check for data type and set param dynamically.
      $infoArray[] = array(
        'key' => $key,
        'value' => $value,
        'type' => PDO::PARAM_STR,
      );
    }
    return $infoArray;
  }

  private function makeQuery($sql, $data = array())
  {
    try{
      $this->connect();
      $stmt = $this->dbConnect->prepare($sql);

      if (!empty($data)) {
        $stmt = $this->bindParams($stmt, $data);
      }

      $stmt->execute();
      $result = $stmt->fetchAll(PDO::FETCH_NUM&PDO::FETCH_ASSOC);
      return (!empty($result)) ? $result : false;
    } catch (PDOException $e) {
      echo $e->getMessage();
      return false;
    }
  }

  private function bindParams($stmt, $data)
  {
    foreach ($data as $item) {
      $stmt->bindValue("name", $this->name, PDO::PARAM_STR);
      $stmt->bindValue($item['key'], $item['value'], $item['type']);
    }
    return $stmt;
  }

  private function connect()
  {
    $dbConnect = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);
    $dbConnect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $this->dbConnect = $dbConnect;
  }
}

?>

从那里你的点击处理程序需要触发User->getUser('some name');请求。您可以通过将PDO连接分离到它自己的类并从那里处理查询构建和执行来进一步获取此抽象。

支持上述关于不将您的演示文稿与数据层混合的评论。查看一个模板引擎,如Twig或(不太可取,但有时是必要的)通过构建一个视图加载器来将自己的模板文件加载到输出缓冲区,添加动态变量并返回渲染的字符串。