EDIT : Does not work on external hosting AND on localhost… So I must be doing something wrong.
I am working with Code Igniter and I have a view. My view page shows no error, but doesn't show the data I requested either. Everythings fine with my config and database I guess... Any idea ? Am I doing anything wrong here? I am using latest version of mamp on mac 4.4.1 and latest version of code igniter 3.1.7.
Thanks guys in advance for your help ;)
View
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<body>
<div>
<p> <?php
echo 'Trying to display:' . $data . ' yes?';
?> </p>
</div>
</body>
</html>
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_secret($id)
{
$this->db->where('id', $id);
$this->db->select('secret');
$query = $this->db->get('tch_api');
if ($query->num_rows==1) {
foreach ($query->result() as $row) {
// tried with this line too : $secret = $row->secret; and without the following one same result
$data[] = $row->secret;
}
return $data;
}
}
}
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('welcome_model');
$secret = $this->welcome_model->get_secret(1);
$data = array(
'data' => $secret
);
$this->load->view('welcome_message', $data);
}
}
Did you try with isset
? Please change the view like:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<body>
<div>
<p> <?php
if (isset($data))
{ echo 'Trying to display:' . $data . ' yes?';} else { echo 'No data found' ;} ?> </p>
</div>
</body>
</html>
UPDATE
Assuming from your above code that you want to get the data of Id 1 from table and show it in your view.
Please do the following changes and try again.
Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_secret($id)
{
$this->db->where('id', $id);
$this->db->select('secret');
$query = $this->db->get('tch_api');
return $query->result();
}
}
?>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('welcome_model','wl');
}
public function index()
{
$data['secret'] = $this->wl->get_secret(1);
$this->load->view('welcome_message', $data);
}
}
?>
View
<!DOCTYPE html>
<html lang="en">
<head>
<title>Data Fetching</title>
</head>
<body>
<div>
<p><?php
//testing 1
if(isset($data)){var_dump($data);}
//testing 2
if (isset($data))
{ echo 'Trying to display:' . $data . ' yes?';} else { echo 'No data found' ;} ?> </p>
</div>
</body>
</html>
Please try this and post your result here..
Please mark it as Answer if the issue has been Fixed.
You are doing a few strange things. Firstly in your get_secret
function you only expect one row, hence one value for secret judging by your logic; you should thus use row()
not result()
. Further, I expect your issue is that (1) your function doesn't return anything when the item with $id
doesn't exist in database and (2) you are trying to echo an array (which will result in an array to string conversion). Assuming you just want to get the value of secret
where $id = 1
you should do the following:
function get_secret($id)
{
$this->db->where('id', $id);
$this->db->select('secret');
$query = $this->db->get('tch_api');
if ($query->num_rows==1) {
return $query->row()->secret;
} else {
return '';
}
}
The rest of your code should work. If you still don't see any results, verify that a row with $id = 1
exists as that can be the only other issue.
controller:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->load->model('welcome_model');
$data['data'] = $this->welcome_model->get_secret(1);
$this->load->view('welcome_message', $data);
}
}
model:
function get_secret($id)
{
$this->db->where('id', $id);
$this->db->select('secret');
$query = $this->db->get('tch_api');
if ($query->num_rows==1)
{
return $query->row->secret;
}
else
{
return "";
}
}
and simply display in view
<?php echo $data;?>
$data in view will be an array And echo can not print an array, try using print_r
view
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<div>
<p>
<?php
echo 'Trying to display:';
print_r($data);
?>
</p>
</div>
</body>
</html>