php - 将 "$message"放入 codeigniter 的表内

标签 php html codeigniter

我有一个问题,我想将 $message 放入表中,相当于“未找到搜索”。

这是我在 table 外面的照片 $message = Search not found

查看:

 <div class="z table-responsive" >
            <table class=" table table-hover"  >


                 <thead >
                             <tr >

                                   <th>ID Number</th>
                                   <th>First name</th>
                                   <th>Middle name</th>
                                   <th>Last name</th>
                                   <th>Sex</th>

                           </tr>
                      </thead>

   <?php if ( isset($message) ){
         echo  $message;

   } else{  foreach($results as $row){
        ?>

    <tbody>
          <tr>


                  <td><?php echo $row-> Idnumber ?></td>
                  <td class="text-capitalize "><?php echo $row -> Firstname ?></td>
                   <td class="text-capitalize"><?php echo $row->Middlename ?></td>
                   <td class="text-capitalize"><?php echo $row-> Lastname ?></td>
                   <td class="text-capitalize"><?php echo $row-> Sex?></td>
                            <td>
                                <a href="<?php echo site_url('viewstudentinalpha/viewspecific/'.$row->Id) ?>" class="btn btn-info " style="font-size: 18px;" type="submit" name="submit" role="button">Option</a>

                             </td>
         </tr>
      </tbody>




    <?php }} ?>




            </tbody>

            </table>
         </div> 

最佳答案

您使用了错误的结束标签。

This will print Search not found inside the table with colspan 5 and center aligned

Note: Most of time we check where foreach value is empty ($results). But in your case you are checking some thing else ($message).

更改

<tr>  <!-- Changed -->
    <td colspan="5" align="center"><?php echo  $message; ?> </td>
</tr>

if ( isset($message) ){ 更改为 if (!empty($message)){

最终代码

<div class="z table-responsive" >
    <table class=" table table-hover"  >
        <thead >
            <tr >
                <th>ID Number</th>
                <th>First name</th>
                <th>Middle name</th>
                <th>Last name</th>
                <th>Sex</th>
            </tr>
        </thead>
        <tbody>
            <?php 
            if (!empty($message)) # improved
            {
                ?>
                <tr> # Changed
                    <td colspan="5" align="center"><?php echo  $message; ?> </td>
                </tr>
                <?php           
            } 
            else
            {  
                foreach($results as $row)
                {
                    ?>              
                        <tr>
                            <td><?php echo $row-> Idnumber ?></td>
                            <td class="text-capitalize "><?php echo $row -> Firstname ?></td>
                            <td class="text-capitalize"><?php echo $row->Middlename ?></td>
                            <td class="text-capitalize"><?php echo $row-> Lastname ?></td>
                            <td class="text-capitalize"><?php echo $row-> Sex?></td>
                            <td>
                                <a href="<?php echo site_url('viewstudentinalpha/viewspecific/'.$row->Id) ?>" class="btn btn-info " style="font-size: 18px;" type="submit" name="submit" role="button">
                                    Option
                                </a>
                            </td>
                        </tr>
                    <?php 
                }
            } 
            ?>
        </tbody>

    </table>
</div> 

编辑01

在 Controller 中

function search_keyword() 
{ 
    $session_data = $this->session->userdata('logged_in'); 
    $data['Username'] = $session_data['Username']; 
    $keyword = $this->input->post('keyword'); 
    $data['results'] = $this->model_adminlogin->search($keyword); 
    $this->load->view('result_view',$data); 
}

在 View 中

像这样改变你的 if 。

复制我在上面添加的所有代码以供您查看。并且只更改这几行。

<?php 
if (!empty($results)) # Change
{
    ?>
    <tr> # Change
        <td colspan="5" align="center"> Search not found </td> # Change
    </tr>
    <?php           
} 
else
{ 

关于php - 将 "$message"放入 codeigniter 的表内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35130994/

相关文章:

javascript - PHP-jquery-ajax动态依赖选择-难度

javascript - CSS:将元素从静态转换为固定/绝对时如何计算丢失的空间

mysql - codeigniter Mysql加入查询结果

php - 不确定为什么下拉框没有填充数据或选择

php - JSON.parse 在 Chrome 中不起作用

php - 第一个数据 curl WSDL SSL 失败

php - php图表显示数据的问题

javascript - 坚持导航栏延迟

HTMLParser 只转换文件的第一行

javascript - 我的 Ajax 代码无法在 codeigniter 中发布数据库中的数据