error foreach in View Codeigniter : Illegal string offset - codeigniter

Please help, i use foreach to return data from database, but it only return the last data (array always update with last data). So i modified the code but i got error : Illegal string offset in foreach.
here is my controller
foreach($dataDesa as $desa)
{
$namaDesa = json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1'));
$datakonten[$namaDesa] = array(
'proyek' =>json_decode($this->curl->simple_get($desa->alamat_api.'/proyek_pertanian')),
'nama_desa' =>json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1')),
'lelang' =>json_decode($this->curl->simple_get($desa->alamat_api.'/pelelangan'))
);
}
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$nama_Desa = array();
foreach($datakonten['getAllDesa'] as $row)
{
$nama_Desa[] = $row->nama_desa;
}
$datakonten['nama_desa']=$nama_Desa;
$data['content'] = $this->load->view('public/konten/v_home',$datakonten,TRUE);
$this->load->view('public/halaman/v_home',$data);
and here is my view
$i=0;
foreach($nama_desa[$i]['proyek'] as $rows)
{
$nama = $rows->nama_proyek;
i++;
}
i've tested $nama_desa[0] and $nama_desa[1] and they have value returned (the value is "Kranon" and "Wulung") and i use the value like $Kranon['proyek'] and theres no error and returned the value that i want, but when i combined it with $nama_desa[$i]['proyek'] i got this error.
`
Please help, thanks in advance

You only push $row->nama_desa to $nama_desa. and try to get ['proyek'] from $nama_desa?

As you explained above, you getting value with this $nama_desa[1] but you trying to access this key $nama_desa[$i]['proyek'] which means its looking for $nama_desa[1]['proyek'] which is not exist.
Just do
print_r($name_desa);die;
You will find there is no key present with the name of 'proyek' on $name_desa[1] (key '1' i have only given for example. It can be any number of key)
I guess you will get your value by accessing this $nama_desa['proyek'] OR You will get idea with print result.

I have just found my solution.
I always getting an error whenever i want to pass variable that refers another value into foreach, so I modified my controller so I can pass the value into foreach in View directly. And I moved kind of "get-data-from-API" from Controller into View.
My Controller :
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$data['content'] = $this->load>view('public/konten/v_home',$datakonten,TRUE);
My View :
foreach($getAllDesa as $rows)
{
$proyek = json_decode($this->curl->simple_get($rows->alamat_api_lelang));
foreach($proyek as $baris)
{
$namaProyek = $baris->nama_proyek;
?>
Nama Proyek : <?php echo $namaProyek;?></br>
<?php
}
}

Related

gettin request/query parameters with less code

Is this:
$paginate = $request->get('paginate');
Equivalent to this, for getting a query param if it is present or assign to the associated variable "null" it it is not present:
if ($request->has('paginate')) {
$paginate = $request->get('paginate');
} else {
$paginate=null;
}
According to get() method documentation:
This method belongs to Symfony HttpFoundation and is not usually needed when using Laravel.
Alternatively you can use filled and $request->paginate
So it checks if the request has the "item"and it has value.
$paginate = null;
if ($request->filled('paginate')){
$paginate = $request->paginate;
}

Data fetch and listing issue faced in my Laravel project

I have developing the ticket system. First table data fetch and display the view page but reply result is showing the last records. How to solve this issue. Please support me.
Controller
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
if($ticketdetails->count()){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
$reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
if($reply_check!="")
{
$reply = Reply::where('reply_ticket_id', '=', $ticketID)->first();
}
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
View Page
if($ticketdetails->count())
#foreach($ticketdetails as $ticketdetails)
<p>{{$ticketdetails->ticket_details}}</p>
$replyid = $reply->reply_ticket_id;
$ticketdetailsid = $ticketdetails->ticket_id;
#php
if($replyid==$ticketdetailsid)
{
#endphp
<p>{{$reply->reply_ticket_comments}}</p>
#php
}
#endphp
#endforeach
#endif
Expecting View page- For example
Ticket Case : Printer not working
Reply:Restart Printer - this is first reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Display For view page
Ticket Case : Printer not working
Reply:okay, we will check now -- this is second reply
Ticket Case : After restart same issue.
Reply:okay, we will check now -- this is second reply
Note:Ticket case data display is correct but reply data only showing the last record.
Initialize reply as array and with each iteration push new value to that array. It will look like following
$ticketdetails = TicketDetails::where('ticket_id', '=', $ticketID)
->orderBy('ticket_id', 'asc')->get();
$reply = [];
if(count($ticketdetails) > 0){
foreach ($ticketdetails as $ticketItem) {
$ticketID = $ticketItem->ticket_id;
// $reply_check = Reply::where('reply_ticket_id', '=', $ticketID)->count();
//if($reply_check!=""){
//Code change below
$reply[$ticketID] = Reply::where('reply_ticket_id', '=', $ticketID)->first() ?? [];
// }
} //Close foreach
} // Close if loop
return view('view-ticket',compact('data','ticketdetails','reply'));
reply will consist of all the records with matching ticketID found in ticketdetails
And in view page you can do following
#if(count($ticketdetails) > 0)
#foreach($ticketdetails as $ticketItem) //Change in name of variable
<p>{{$ticketItem->ticket_details}}</p>
//You don't need to check for id as new reply array consist of all records mapped with ticket ID
#if(count($reply) >0)
#if(count($reply[$ticketItem->ticket_id]) >0)
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
#endif
#endif
#endforeach
#endif
Thanks for the support.
I have stored the reply count in array variable $reply_display[ ] =$reply_check; Then view page check the array values
#php
$replyck = $reply_display[$i];
if($replyck==1){ #endphp
<p>{{$reply[$ticketItem->ticket_id]['reply_ticket_comments']}}</p>
#php }
$i = $i+1;
#endphp
:)- my error issue solved.

How to send array data as a parameter for get value in Laravel controller?

This is My code which is used in function -
foreach($allcourses as $allcourse) {
$tl_class[] = $allcourse['title_of_class'];
// $trn_email[] = $allcourse['trainer_email'];
}
$rating = new RatingController;
Log::info(print_r($tl_class, true). ' : RatingController tl_class data');
I want to send array data a parameter as below-
$rating = new RatingController;
$rating_data = $rating->getAllPublicRatting($tl_class);
but Showing error. So, How to pass this array value as a parameter?
$tl_class[];
foreach ($allcourses as $allcourse) {
tl_class->push($allcourse['title_of_class']);
// $trn_email[] = $allcourse['trainer_email'];
}
you need to push data to your array cause your each loop when return always your array define empty.

how to get required label from string of objects

In my code-igniter application, I have an string that contains json objects. each object has value and label, and I want to get the label from an object where the value matches. Please tell how can I do that?
String:
$my_string = '{value:"123",label:"example"},{value:"321",label:"required label"}';
Hope this will help you :
Use json_decode and foreach for that like this :
Working demo : https://eval.in/1008536
/*your json string should be like this */
$my_string='[{"value":"123","label":"example"},{"value":"321","label":"required label"}]';
$arr = json_decode($my_string,true);
if ( ! empty($arr))
{
foreach ($arr as $item) {
if ($item['value'] == '321')
{
$label = $item['label'];
}
}
}
echo $label;
/* Output : required label*/
For more : http://php.net/manual/en/function.json-decode.php

Access to filter var attribute?

In Mage_Catalog_Block_Layer_View there is a variable called $_filters, that loops through and displays properties in the $_filter var. Whenever I try to var dump this variable, my server throws an error. This behavior is completely mysterious. Does anyone know if I can get back to the attribute code? I'd like to do something like this:
$_filter->getAttribute()->getAttributeCode();
Have you tried $_filter->getAttributeModel()->getAttributeCode()?
public function getFilters()
{
$filters = array();
if ($categoryFilter = $this->_getCategoryFilter()) {
$filters[] = $categoryFilter;
}
$filterableAttributes = $this->_getFilterableAttributes();
foreach ($filterableAttributes as $attribute) {
$filters[] = $this->getChild($attribute->getAttributeCode().'_filter');
}
return $filters;
}
you can see where the variable setting to this array takes place and how the attribute code is used in this class. Note that this is an array containing view objects not a object itself and dumping out all this just eats up your server memory. And of course you can't call methods on this array.
So there is no actual way to get this code out of this array and you probably have more success in template or subobject level or if you need this in the same class you can get the filters out of $this->_getFilterableAttributes() method or you can try and iterate over each filters array member and their sub-members
$filters['0']->getItems()

Resources