如何从db检索数据并在codeigniter中显示它

时间:2016-02-23 19:09:56

标签: php codeigniter

我将此代码显示如下,但是当它运行时,它总是会出错 消息:未定义的变量:结果为什么它会给出错误我无法弄清楚,有人可以给我一个解决方案吗?

myads_model.php

<?php
    class Myads_model extends CI_Model
    {

        public function __construct()
    {

        $this->load->database();

    }

        public function getmyads( $SESSION['$email'] )
        {
            $this->db->select('Vehicleid,Image1,Brand,Model,Modelyear,
                               VehicleCondition,Mileage,BodyType,Transmission,
                               Fueltype,EngineCapacity,Price,Negotiable,
                               Description,Phone,Email');
            //$this->db->from('vehicle');
            $this->db->where('Email',$SESSION['$email']);
            $query=$this->db->get('vehicle');
            return $result=$query->result();
        }

    }

    ?>

myads_ctrl.php

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

/**
*
*/
class Myads_ctrl extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();


    }

    public function index()
    {
        $data=array();
        $this->load->model('myads_model');

        $this->data['result']=$this->myads_model->getmyads($_SESSION['email']);
        $this->load->view('pages/templates/header');
        $this->load->view('pages/myads_view', $this->data);
        $this->load->view('pages/templates/footer');
    }

}
?>

myads_view.php

我的广告

<?php $image=$post->Image1;
      $vehicleid=$post->Vehicleid;
echo '<img src="http://localhost/ci/images/' . $image . '" style="width:200px; height:150px;"/>';?>



<?php echo $post->Brand . " " . $post->Model . " " . $post->Modelyear;?>


<b>BRAND: <?php echo $post->Brand;?></b>
<b>MODEL : </b><?php echo $post->Model;?>
<b>MODEL YEAR : </b><?php echo $post->Modelyear;?>
<b>CONDITION : </b>
<?php echo $post->VehicleCondition;?>


<b>MILEAGE : </b><?php echo $post->Mileage;?>KM
<b>TRANSMISSION: </b><?php echo $post->Transmission;?>
<b>FUEL TYPE: </b><?php echo $post->Fueltype;?>


<b>Rs. </b><?php echo $post->Price;?>
<a href="<?php echo 'adpreview_ctrl/getad_preview/'.$vehicleid?>"><input type="button" value="View" class="btn-primary"></a>

1 个答案:

答案 0 :(得分:0)

在您的模型中,请不要将参数命名为$SESSION['$email'],而是$email。并将模型中的select语句更改为:

    public function getmyads( $email )
    {
      $this->db->select('your fields here');
      $this->db->from('vehicle');
      $this->db->where('Email', $email);

      $query = $this->db->get();
      $result = $query->result();
      return $result->num_rows() > 0 ? $result->result_array() : false;
    }

在您的控制器中:

$this->data['result']=$this->myads_model->getmyads($_SESSION['email']);   
$this->load->view('pages/templates/header');
$this->load->view('pages/myads_view', $this->data);
$this->load->view('pages/templates/footer');

在您看来,您应该能够访问以下结果:

var_dump($result);