Echo/Print Query Result at Controller in Codeigniter - php

I am trying to display my Model query return result in my controller, but I don't know how to do it. could you please show me? Thanks in advance
Controller:
function updateJobsheetCDC()
{
$prnID = $this->input->post('prnID');
$this->load->model('Ipss_model');
$data['prnEmail'] = $this->Ipss_model->prnEmailList($prnID);
echo $data['prnEmail']['name'];
}
Model:
function prnEmailList($prnID)
{
$q = $this->db->query("SELECT pm.* , prm.*,e.* from principal_master pm ,principal prm,email e where pm.prnID=e.prnID and prm.prID= pm.prID and e.prnID='" .$prnID."'");
if($q->num_rows()>0) {
foreach($q ->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
I have tried
echo $data['prnEmail']['name']; but it do not work. It shows Severity: Notice
Message: Undefined index: name.

You have multi-dimentinal array returning from model. So you have to loop over them then print data
function updateJobsheetCDC()
{
$prnID = $this->input->post('prnID');
$this->load->model('Ipss_model');
$data = $this->Ipss_model->prnEmailList($prnID);
foreach ($data as $key => $value) {
echo $value->name;
}
}

Related

Navigate an array from controller in Codeigniter

Good I'm starting with php and codeigniter, I have the following problem I'm running an array from my model, which is the result of a query, how can I read one by one my records in the controller?
function getCustomers() {
$sql = "SELECT * FROM customers";
$query = $this->db->query($sql);
if ($query->num_rows() > 0) {
$i = 0;
foreach($query->result() as $row) {
$img[$i]['id'] = $row->id;
$img[$i]['name'] = $row->name;
$img[$i]['Location'] = $row->Location;
$img[$i]['telephone'] = $row->telephone;
$i++;
}
return $img;
}
}
You can return array to the controller by loading the model into the controller like this.
$this->load->model('yourmodel');
$array = $this->yourmodel->yourmodelsmethod();
First you have to return this array to your controller like
$this->load->model('yourmodel');
$array['test'] = $this->yourmodel->yourmodelsmethod();
foreach($test as $key=>$value)
{
echo $value;
}
By doing this you can easily print your data from model in controller

retrieve data from the database but the result of " array"

I tried to call up data from the model , but when running the results in view the word " array" . is there who can help me ? i am using codeigniter
Controller
$data=array('pengunjung' => $this->mcrud->jumlah_visitor(),
'isi' =>'user/monitoring');
$this->load->view('layout/wrapper', $data);
Model
function jumlah_visitor() {
$date = date("Ymd");
$this->db->where('date',$date);
$this->db->group_by(array('ip'));
$ambil= $this->db->get('tbcounter');
if ($ambil->num_rows() > 0) {
foreach ($ambil->result_array() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<div class="pull-left">Hari Ini : </div>
<div class="pull-right number"> <?php echo $pengunjung; ?></div>
Result
Hari ini : array
First you check the return value of $this->mcrud->jumlah_visitor() after that you print the value. If it's an array you have to use loop for that.
<?php echo $pengunjung; ?>
to
<?php print_r($pengunjung); ?>
$pengunjung variable is array not string variable
Yes of-course...
in your Model you are returning an array
foreach ($ambil->result_array() as $data) {
$hasil[] = $data;
}
return $hasil;
The same you are trying to catch in your controller $this->mcrud->jumlah_visitor(),
return your index-key just like $data->index_key
Try printing array as pengunjung is holding an array, not a simple variable.
echo "<pre>";
print_r($this->mcrud->jumlah_visitor);
echo "</pre>";
You will get complete array info.
You can't echo an array. you should use a loop to display result of $pengunjung.
In View just use
foreach( $pengunjung as $key => $value)
{
//echo $value;
}
Rewrite your model function like this
function jumlah_visitor() {
$date = date("Ymd");
$this->db->where('date',$date);
$this->db->group_by(array('ip'));
$ambil= $this->db->get('tbcounter');
$data = $ambil->result_array();
if (data->num_rows() > 0) {
foreach ($data as $data1) {
$hasil[] = $data1;
}
return $hasil;
}
}

display array values in view using codeigniter

Controller:
function checkedServices() {
$data = array();
foreach($this->input->post('check_service') as $ser) {
$data[] = array('service' => $ser);
}
$checked_services['services'] = array($data);
$this->load->view('checked_services', $checked_services);
}
view:
foreach($services as $ser)
{
echo $ser;
}
Output:
Severity: Notice
Message: Array to string conversion
Filename: views/checked_services.php
In Controller
function checkedServices()
{
$data = array();
foreach($this->input->post('check_service') as $ser) {
$data[]['service'] = $ser;
}
$checked_services['services'] = $data;
$this->load->view('checked_services', $checked_services);
}
In View
foreach($services as $ser)
{
echo $ser['service'];
}
$data is array
$checked_services['services'] = $data;
You are making a simple code complex.
Just remove array from this statement.
$checked_services['services'] = $data;
In your controller, you are looping and getting data in $data.
$data is already an array, why do you want array($data)?

How To Get NON-Duplicate Values using Zend DB model?

fetchresultsAction
public function fetchresultsAction() {
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
//$form_data = $this->getRequest()->getPost();
//$examcatID = $form_data['examcatID'];
$examID = 'Exam_ID_001';
$Exam_Results = new Admin_Model_Examresults();
$results = $Exam_Results->fetchExamResults($examID);
foreach ($results as $value) {
print_r($value['subjectID']);
echo '<br/>';
}
}
Model > Examresults
public function fetchExamResults($examID) {
$sel = $this->select();
$sel->distinct();
$sel->where('examID=?', $examID);
$result = $this->fetchAll($sel); // fetch the rows to the array called result
return $result;
}
This Code is not work.I need have to get NON-Duplicate values of subjectID from mysql database.How Can I do that.Please help me to solve this problem.
Note : This is Zend 1.12 version
Try using print_r($value->subjectID)

Foreach error variable cannot identified

i have this function
function c_del()
{
$session_data = $this->session->userdata('logged_in');
$uname = $session_data['username'];
$query = $this->user_m->viewDetail($uname);
foreach($query as $row)
{
$username=$row->username;
}
$id_calon_reg=$_GET['a'];
$query1 = $this->candidate_m->del_calon($id_calon_reg);
$query3 = $this->candidate_m->search_calon($id_calon_reg);
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
}
unlink($foto_calon);
$query2 = $this->candidate_m->viewAll();
$data=array(
"query"=>$query2,
"username"=>$username
);
$this->load->helper(array('form'));
$this->load->view('candidate_view',$data);
}
i want to unlink the path stored in $foto_calon, but i get this error
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: foto_calon
Filename: controllers/candidate.php
Line Number: 67
line 67 is where i call unlink function.
but I already define the variable $foto_calon in foreach.
the first foreach when i want to store the username into $username its success but got error in the second foreach.
i can't find out what's the problem. can anybody tell me?
Try to replace
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
}
with
if(is_array($query3)) {
foreach($query3 as $row)
{
$foto_calon=$row['foto_calon'];
}
}
And if you want to unlink each row of foto_calon in the result array then put that unlink in this foreach loop like
if(is_array($query3)) {
foreach($query3 as $row)
{
$foto_calon=$row['foto_calon'];
unlink($foto_calon);
}
}
Try to put the unlink into the foreach, like so :
$id_calon_reg=$_GET['a'];
$query1 = $this->candidate_m->del_calon($id_calon_reg);
$query3 = $this->candidate_m->search_calon($id_calon_reg);
foreach($query3 as $row)
{
$foto_calon=$row->foto_calon;
unlink($foto_calon);
}

Categories