2017-02-28 71 views
1

我正在编写一个web应用程序,我相信其中一个部分需要一个 多维数组。该数组包含数据库中的应用程序列表。 我希望能够通过个人名称或 唯一ID显示应用程序列表。我有这部分工作。然后,我想点击一个个人应用程序,只需提取特定行信息以填写表单。 目前,当我这样做时,它会调出数据库中的所有行,或者只显示第一行的 。有没有人有什么建议? 我不是很好的解释,所以我包括我的代码的一部分。对不起 这么久了。我试图尽可能地减少它。尽管它包含代码中的 ,但我没有包含config.php,因为它只是我的数据库连接。在没有已知键的情况下在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:从users.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>"; 
?> 

功能:

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; 
     } 
    } 
+0

模板?我在HTML和PHP之前就已经看到了这个建议,但是我从来没有听说过如何解决这个问题。对于这个特定的部分,我通过名称而不是user_id来调用个人用户。该特定表中没有user_id。对于具有类似功能的Web应用中的其他页面,我使用的是创建的日期而不是名称。 – jbfig

+0

你应该考虑研究一个模板系统,如Twig或http://github.com/figdice/figdice – Gabriel

回答

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或(不太明智但有时需要)的模板引擎。

+0

我没有使用这个解决方案,但它帮助我找到我的解决方案,所以谢谢。 – jbfig

相关问题