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)?
Related
This is my addToCart method in which I am adding the courses to user_cart column in users table.
public function addToCart($id){
$course = Course::findOrfail($id);
$user =Auth::user();
$cart_array = array();
$cart = $user->user_cart;
if ($cart == '') {
array_push($cart_array, array('id' => $course->id));
// print_r($cart_array);
} else {
$founder = false;
$cart_array = json_decode($cart, true);
for ($i = 0; $i < count($cart_array); $i++) {
$cart_for_eacch_course = $cart_array[$i];
if ($cart_for_eacch_course['id'] == $course->id) {
$founder = true;
}
}
if (!$founder) {
array_push($cart_array, array('id' => $course->id));
}
}
$data['id'] = json_encode($cart_array);
$update = User::where('id',$user->id)->update(['user_cart'=> $cart_array]);
return redirect()->back();
}
And this is my showcart method in which I am taking the courses from users table user_cart column array.
public function showcart(){
$user = Auth::user();
$array = $user->user_cart;
print_r($array);
return view('frontend.my_cart', compact('academic','nonacademic','instructor','coc','my_cart'));
}
And when I am displaying it I am getting the output as follows:
[{"id":84},{"id":86}]
Now can you tell me that how to loop them so I can get the courses? I tried by using a foreach loop for $array but it shows me the error of invalid argument supplied for foreach()
Looks like you are building the array into a JSON or string field within your database on the User object. The resulting array appears to be stored as a JSON string, so decode and then loop:
$array = json_decode( $user->user_cart, true );
foreach($array as $key => $val){
dump $key;
dump $val;
}
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;
}
}
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;
}
}
So basically I have an array, and I have a function that will return an array value, but it is not returning anything.
$skills = array("Attack");
$i = 0;
$hs = file_get_contents("localhost/player=".$_GET["player"]);
foreach($skills as $value) {
$hs[$i] = explode(",",$hs[$i]);
$stats[$value]["rank"] = $hs[$i][0];
$stats[$value]["level"] = $hs[$i][1];
$stats[$value]["xp"] = $hs[$i][2];
$i++;
}
function getSkill($skill) {
//echo $skill;
return $stats[$skill]["level"];
}
I have tried getSkill("Attack"); and it doesn't work;
but if I do echo $stats["Attack"]["level"]; it works fine.
You don't pass the $stats array to your function so it can't know what is in it.
function getSkill($stats, $skill) {
//echo $skill;
return $stats[$skill]["level"];
}
and call it with getSkill($stats,"Attack");
I currently have an array that looks like this:
[[{"name":"Shirt","data":[1,1,5,5,1,10000]},{"name":"Skittles","data":[1,9,1,1]}]]
I'm using:
preg_replace('/"([^"]+)"\s*:\s*/', '$1:',json_encode($results));
to create an array that should look like this:
[{"name":"Shirt","data":[1,1,5,5,1,10000]},{"name":"Skittles","data":[1,9,1,1]}]
However I can't seem to get rid of the extra set of brackets.
My model:
function get_data()
{
$this->db->select('ItemName, QuantitySold');
$query = $this->db->get('transactions');
$results = array();
foreach ($query->result_array() as $row)
{
if(!isset($results[$row['ItemName']]))
$results[$row['ItemName']] = array('name' => $row['ItemName'], 'data' => array());
$results[$row['ItemName']]['data'][] = $row['QuantitySold'];
}
//Rekey arrays so they aren't associative
$results = array_values($results);
return $results;
}
My controller:
function test()
{
$this->load->model('data');
$series_data[] = $this->data->get_data();
$data['series_data'] = json_encode($series_data, JSON_NUMERIC_CHECK);
preg_replace('/"([^"]+)"\s*:\s*/', '$1:',json_encode($series_data));
$this->load->view('chart', $data);
}
thanks in advance.
Why use preg_replace.... this is simply json encoded data:
$string = '[[{"name":"Shirt","data":[1,1,5,5,1,10000]},{"name":"Skittles","data":[1,9,1,1]}]]';
var_dump(
json_encode(
json_decode($string)[0]
)
);