2016-11-18 81 views
0

我確實需要使這個表工作,但我找不到我的代碼中缺少什麼或者爲什麼這個變量未定義。表格工作正常。它顯示,它上面也有一個搜索框。表格的html代碼很好,它顯示錶格。 PHP錯誤出現在調用id,名稱等的表的列表中。請我需要幫助..遇到一個PHP錯誤嚴重性:通知消息:未定義索引:id

我的控制器:(tabledisplay.php)

function index() 
    { 
     $data = array(); 
     $this->load->model('table_model'); 
     $data['result'] = $this->table_model->display_table(); 
     $this->load->view('table_view', $data);//error here 

    } 

我的觀點:(table_view.php)

<table class="table table-hover"> 
       <tr> 
        <th>ID</th> 
        <th>First Name</th> 
        <th>Middle Name</th> 
        <th>Last Name</th> 
        <th>Position</th> 
       </tr> 

       <?php 
       $row = array(); 
       foreach ($row as $result); ?>//the values cannot be displayed 
       <tr> 
        <td><strong><?php echo $row['id']; ?></strong></td>//error starts here 
        <td><?php echo $row['fname']; ?></td> 
        <td><?php echo $row['mname']; ?></td> 
        <td><?php echo $row['lname']; ?></td> 
        <td width="15%"><?php echo $row['position']; ?></td> 
        <td width="100px"><button type="submit" class="btn btn-success">Sign in</button></td> 
       </tr> 
       <?php ?> 

我的模型:(table_model.php)

function display_table() 
    { 

    function filterTable($query) 
    { 
     $connect = mysqli_connect("localhost","root","","registerusers"); 

     $filter_Result = mysqli_query($connect, $query); 

     return $filter_Result; 

    } 

     if(isset($_POST['search'])) 
     { 
       $valueToSearch = $_POST['valueToSearch']; 
      // search in all table columns 
      // using concat mysql function 
      $query = "SELECT * FROM `user` WHERE CONCAT(`id`, `fname`, `lname`, `position`) LIKE '%".$valueToSearch."%'"; 
      $search_result = filterTable($query); 
     } 
     else{ 
      $query = "SELECT * FROM `user` LIMIT 10"; 
      $search_result = filterTable($query); 
     } 
    return $search_result; 
    } 

回答

2

你有一個$data['result']在控制器那裏爲每個視圖

$result先在的foreach

<?php if ($result) {?> 
<?php foreach ($result as $row) { ?> 
<tr> 
<td><strong><?php echo $row['id']; ?></strong></td> 
<td><?php echo $row['fname']; ?></td> 
<td><?php echo $row['mname']; ?></td> 
<td><?php echo $row['lname']; ?></td> 
<td width="15%"><?php echo $row['position']; ?></td> 
<td width="100px"><button type="submit" class="btn btn-success">Sign in</button></td> 
</tr> 
<?php } ?> 
<?php } else { ?> 
<p>Sorry No Results</p> 
<?php } ?> 

或嘗試如

<?php if ($result) {?> 
<?php foreach ($result as $row) { ?> 
<tr> 
<td><strong><?php echo $row->id; ?></strong></td> 
<td><?php echo $row->fname; ?></td> 
<td><?php echo $row->mname; ?></td> 
<td><?php echo $row->lname; ?></td> 
<td width="15%"><?php echo $row->position; ?></td> 
<td width="100px"><button type="submit" class="btn btn-success">Sign in</button></td> 
</tr> 
<?php } ?> 
<?php } else { ?> 
<p>Sorry No Results</p> 
<?php } ?> 
+0

是正確的,@ wolfgang1983 –

+0

試過兩個^,但結果仍然是不確定的變量..謝謝。 我已編輯我的查詢,現在它工作正常,除了它獲得的帖子的限制,另一個概率-_- – Distro