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

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;
}
}

Related

Echo/Print Query Result at Controller in Codeigniter

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;
}
}

php empty result for language table

I am trying to add multiple language options with the database. I created a table as follows.
And my PHP function is:
public function Languages(){
$query=mysqli_query($this->db,"
SELECT * FROM languages") or die(mysqli_error($this->db));
while($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$data[]=$row;
}
if(!empty($data)) {
// Store the result into array
return $data;
}
}
So also I used the following code for showing result but I am getting the empty result:
<?php
$language = $ReSult->Languages();
$userLanguage = 'english';
echo $language['your_family'][$userLangauge];
?>
I know if I use $language['1'][$userLanguage]; then I get a result but I don't want to use id's here. Is there any way to do that to show results without using ids?
You could store the results in an associative array:
while ($row = mysqli_fetch_array($query,MYSQLI_ASSOC)) {
$data[$row['lang_key']] = $row;
}
This way, you can access the data directly by its key:
echo $language['your_family'][$userLangauge];
checkout this
function getIndex($language , $key)
{
foreach ($language as $row)
{
if (strcmp($row["lang_key"] , $key)==0) {
return $row["id"]
}
}
return null;
}
...
echo $language[getIndex($language , "your_family")][$userLanguage];
You can try like this way,
while ($row = mysqli_fetch_assoc($query)) {
$data[$row['tagKey']] = $row;
}

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 use database object in view

I'm having trouble passing data to my view when using $query->row() I can only seem to get the value when using it in the controller.
Here's the model:
public function get_post($post_id) {
$this->db->select('id, date, title');
$this->db->from('posts');
$this->db->where('id', $post_id);
$this->db->limit(1);
$query = $this->db->get();
$row = $query->row();
$row->title = html_purify($row->title);
$row->date_posted_iso = date('c', $row->date);
return $row;
}
The controller:
public function post($post_id) {
$row = $this->post_model->get_post($post_id);
$this->data['post'] = $row;
$this->data['meta_title'] = $row->title;
$this->template->build('post/show_post', $this->data);
}
The view:
<?php
foreach ($post as $row):
echo $row->date;
echo $row->title;
endforeach;
?>
The line $this->data['meta_title'] = $row->title; in the controller works correctly, but I don't want to explicitly define all of those variables in the controller.
I've tried with the foreach and without and get Object of class stdClass could not be converted to string
How can I correctly echo the value in my view using $query->row()?
As you are returning a row, you don't need a foreach loop, try changing your view code to:
<?php
echo $post->date;
echo $post->title;
?>
The variable should be $post and not $row as you define post element in the data array in your controller.
Try to do like this.............
public function post($post_id)
{
$row = $this->post_model->get_post($post_id);
$model = array();
$model['post'] = $row;
$this->template->build('post/show_post', $model);
}
In view fie do like this
foreach($post as $data)
{
echo $data['title'];
}

How can I convert db query result to array

original I thought the $query is array object, since I can print these data to a list table like this.
my controller code
$data['dates'] = $this->calendar_model->get_cal_eventDate();
$this->load->library('table');
$this->load->view('my_test', $data);
my view code
echo $this->table->generate($dates);
but when I changed my code to try to print $tbDataArr via foreach. It didn't work. How can I convert the $query result (eventDate field values) to array object.
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
return $query;
}
$tbDataArr = $this->calendar_model->get_cal_eventDate();
foreach ($tbDataArr as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}
Are you using CodeIgniter? If you are you can do this
function get_cal_eventDate() {
$this->db->select('eventDate');
$query = $this->db->get('tb_event_calendar');
$result = $query->result_array();
return $result;
}
If not need more info about what your doing with your PHP.
Codeigniter, right?
Use the $query->result_array() method.
see more here:
http://codeigniter.com/user_guide/database/results.html
//Convert $query->result_array() to $query->result() after add new field in array
$result = $query->result_array();
for($i=0;$i<$query->num_rows();$i++){
//insert new field in array (test_field with value 1)
$result[$i]+=array('test_field'=>1);
$result[$i] = (object)$result[$i];
}
return $result; //equivalent to return $query->result() without new field;

Categories