如何在codeigniter中显示数据库中选定的下拉选项值?

时间:2016-11-02 09:28:10

标签: php codeigniter

当我点击相册时,它会在数据库显示下面显示与该相册相关的所有数据。前端如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery Ajax方法

完成它

在您的视图中

<select id="select_album">
  <option value="album1">album1</option>
  <option value="album2">album2</option>
  <option value="album3">album3</option>
  <option value="album4">album4</option>
</select>
<div id="result">
<table>
//your default listing here
</table>
</div>

在jQuery脚本中是

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#select_album").change(function(){
    var selected=$("#select_album").val();
    dat='album_name='+selected;
    var url='<?php echo site_url();?>/Controller/method/';
    $.ajax({
       type: "POST",
       url: url,
       data: dat,
       success: function(data)
       { 
          if(data=='fail') {
              alert("there is no photos in this album");
           }
          else {
           $('#result').html(data);
          }            
       }
     });
});
});
</script>

在您的控制器中

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Controller extends CI_Controller {
public function __construct() {
   parent::__construct();
}
public function index() {
    //some view or logics
}
public function method() {
   $album_name=$this->input->post('album_name');
   if($album_name==NULL) {
     echo "fail";
     //prevent direct access to this method
   }
   else {
      $data=$this->YourModel->getPhotosfromAlbum($album_name);
      if($data==TRUE) {
          echo "<table>";
          echo "<tr><th>id</th><th>image name</th><th>image</th></tr>";
          foreach($data as $dat) {
               echo '<tr><td>'.$dat['id'].'</td>'.$dat['img_name'].'<td><img src="'.base_url.'/images/albums/'.$dat['img_src'].'" height="100" widh="100"/></td></tr>';
            }
          echo "</table>";
       }
      else {
         echo "fail";
       }
   }
}

在你的模特中

<?php
class YourModel extends CI_Model {
public function __construct() {
    parent :: __construct();
}
public function getPhotosfromAlbum($album_name) {
   $query=$this->db->get_where('photos',array('album_name'=>$album_name));
   $result= $query->result_array();
   if($result==TRUE) {
     return true;
     }
   return false;
  }
}