pass multi-dimensional array from controller to view codeignighter - codeigniter

how do i pass a multi diminutional array to a view?
controller code
public function index(){
data['navs'] = array(
'name' => array('one', 'two', 'three'),
'link' => array('one', 'two', 'three'));
$this->load->view('adminView', $data);}
view code
<?php if ($navs) {
foreach ($navs as $nav) {
echo('<li>' . $nav->name . '</li>');
}
}?>

First of all you need to build the array, the right way. It should be something like:
$data['navs'] = array( array( 'name' => 'one',
'link' => 'linkone'),
array( 'name' => 'two',
'link' => 'linktwo')
);
$this->load->view('adminView', $data);
Then in your view:
foreach($navs as $n){
echo "<li><a href='{$n['link']}'>{$n['name']}</a></li>";
}

Once in the view, refer to your data as array elelements, not object properties (you are passing the array of arrays, not array of objects). Based on your controller, your view code should look like that:
foreach ($navs as $nav) {
echo('<li>' . $nav['name'] . '</li>');
}
However, that won't output the right result because your $nav['link'] and $nav['name'] are two arrays. You'd need to call any of their elements or change controller accordingly.

how to fetch dynamic array value by using controller of codeigniter in php
Array
(
[id] => 2
[name] => Wellness & Spa
[isSelected] => true
[subModules] => Array
(
[0] => Array
(
[id] => 1
[name] => Spa1
[isSelected] => true
[0] => object:81
)
[1] => Array
(
[id] => 2
[name] => Spa2
[isSelected] => true
[0] => object:82
)
[2] => Array
(
[id] => 3
[name] => Wellness
[isSelected] => true
[0] => object:83
)
)
)

Related

How to create a single array using two iterating loop and than update_batch

How do I take id on every iteration from check_seeds array and add on each itteration into seeded[] array.
In more simple words, I want to take an item from the first iteration and add into the first iteration, take an item from the second iteration and add into the second iteration and so on...
Actually, on update_batch we need third parameter (primary key, index) to update array values in database rows where id from database rows matches with the id in update_batch.
$check_seeds = $this->tournament_model->get_seeds($tournament_id);
$seeds = $this->input->post('seed');
foreach ($seeds as $key => $value){
if(!empty($key) && !empty($value)){
$seeded[] = array(
'id' => (Add id here),
'tournament_id' => $tournament_id,
'stage_id' => $stage_id,
'seed_id' => $value,
'team_name' => $key,
);
$this->db->update_batch('tournament_seed', $seeded, 'id');
redirect('organizer/tournaments);
}
}
print_r($check_seeds)
Array
(
[0] => Array
(
[id] => 3
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[1] => Array
(
[id] => 4
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
[2] => Array
(
[id] => 5
[tournament_id] => 713161746
[stage_id] => 3
[seed_id] => 3
[team_name] => -V-ATTAX
)
)
in your model function get_seeds() you can query the current max value of id as an alias and return it together with the query result:
function get_seeds($tournament_id) {
$this->db->select_max('id', 'max_id');
$this->db->where('tournament_id', $tournament_id);
$result = $this->db->get('tournament_seed');
return $result->result();
}
then in your controller's for_each() you increment that value:
$i=0;
foreach ($seeds as $key => $value){
$i++;
$seeded[] = array(
'id' => $value->max_id + $i,
//....
);
}
Codeigniter docs: Selecting Data, scroll down to select_max(), as there is no internal bookmark

displaying particular data from entire json using laravel

I have an issue in printing the data from the below json in laravel. Find below the json data and help me with this.
DarthSoup\Cart\Item Object ( [rowId] => 76cc22e6f533b8ef3d08e6eca0f110b4 [id] => 61XJrpB7CgiYSRCX6hR78wCEABhjLI7jNLzPyDrkYA3LyIVisz [name] => linux [quantity] => 1 [price] => 109 [options] => DarthSoup\Cart\CartItemOptions Object ( [items:protected] => Array ( [package] => Super Lite ) ) [subItems] => DarthSoup\Cart\SubItemCollection Object ( [items:protected] => Array ( ) ) [created_at:protected] => Carbon\Carbon Object ( [date] => 2018-06-22 05:41:18.674548 [timezone_type] => 3 [timezone] => UTC ) [updated_at:protected] => Carbon\Carbon Object ( [date] => 2018-06-22 05:41:18.674574 [timezone_type] => 3 [timezone] => UTC ) [associatedModel:protected] => [taxRate:protected] => 19 ) 1
From the above json how to display the "price=>109" and "package=>super Lite"...
I'm getting an error as "Cannot use object of type DarthSoup\Cart\Item as array"
Find below the controller Code:
public function addCart(Request $request)
{
$name = $request->input();
$hosting=$name['hosting'];
$package=$name['package'];
$price=$name['price'];
$response=Cart::add(['id'=>str_random(50) ,'name' => $hosting, 'quantity' => 1, 'price' => $price, 'options' => ['package' => $package]]);
//dd($response);
return Cart::content();
return view('main.cart',Cart::content());
}
Blade file is given below:
#foreach($response->options as $option){
{{$option['name']}}
}
#endforeach
In my output I need to display the value of price,name and package...please help me to solve this error...
First this is not json, it is php object
if your variable name is $item then you can try this
$item->rowId;
$item->id;
$item->name;
$item->price;
foreach($item->options as $option){
echo $option->package;
}

Put/Get UploadField submitted image from custom Form -> Silverstripe 3.1

I have a small problem programming with the use of UploadField . I have created pages to make a light CMS on the FrontEnd. But I don't know how retrieve this image to the page «Update».
There is the code from the page «Create» :
$uploadField = new UploadField( 'ImageEvenement', 'Image' );
There is the code I tried to get working for the page «Update»
$evenID = Session::get('evenementID');
$evenement = Versioned::get_by_stage('PageCalendrierEvenement', 'Stage')->byID($evenID);
..
$SavedImage = File::get()->byID($evenement->ImageEvenementID)
$uploadField = new UploadField( 'ImageEvenement', 'Image', $SavedImage );
How can I retrieve the submitted image to $SavedImage ? My idea to get the ID from File don't work.
Another method :
$SavedImage = $evenement->ImageEvenement();
If I dump data from $SavedImage I'm viewing :
Image Object
(
[destroyed] =>
[model:protected] => DataModel Object
(
[customDataLists:protected] => Array
(
)
)
[record:protected] => Array
(
[ClassName] => Image
[Created] => 2015-07-15 14:41:24
[LastEdited] => 2015-07-16 15:03:25
[Name] => images.jpg
[Title] => images
[Filename] => assets/Membres/9/calendrier/images.jpg
[ShowInSearch] => 1
[ParentID] => 15
[OwnerID] => 9
[ID] => 22
[RecordClassName] => Image
)
[changed:DataObject:private] => Array
(
)
[original:protected] => Array
(
[ClassName] => Image
[Created] => 2015-07-15 14:41:24
[LastEdited] => 2015-07-16 15:03:25
[Name] => images.jpg
[Title] => images
[Filename] => assets/Membres/9/calendrier/images.jpg
[ShowInSearch] => 1
[ParentID] => 15
[OwnerID] => 9
[ID] => 22
[RecordClassName] => Image
)
[brokenOnDelete:protected] =>
[brokenOnWrite:protected] =>
[components:protected] =>
[unsavedRelations:protected] =>
[sourceQueryParams:protected] =>
[failover:protected] =>
[customisedObject:protected] =>
[objCache:ViewableData:private] => Array
(
)
[class] => Image
[extension_instances:protected] => Array
(
[BetterButtonDataObject] => BetterButtonDataObject Object
(
[owner:protected] =>
[ownerBaseClass:protected] => DataObject
[ownerRefs:Extension:private] => 0
[class] => BetterButtonDataObject
)
[SiteTreeFileExtension] => SiteTreeFileExtension Object
(
[owner:protected] =>
[ownerBaseClass:protected] => File
[ownerRefs:Extension:private] => 0
[class] => SiteTreeFileExtension
)
[Hierarchy] => Hierarchy Object
(
[markedNodes:protected] =>
[markingFilter:protected] =>
[_cache_numChildren:protected] =>
[owner:protected] =>
[ownerBaseClass:protected] => File
[ownerRefs:Extension:private] => 0
[class] => Hierarchy
)
)
[beforeExtendCallbacks:protected] => Array
(
)
[afterExtendCallbacks:protected] => Array
(
)
)
Any idea?
class PageCalendrierEvenement extends Page {
private static $db = array(
"Titre" => "Varchar(50)",
"DateDepart" => "Date",
"DateFin" => "Date",
);
private static $has_one = array(
'Creator' => 'Member',
'ImageEvenement' => 'Image',
);
..
}
Thank you!
has one relations need the "ID" suffix in the name of the relation (as it's saved to db...), e.g.
$uploadField = new UploadField( 'ImageEvenementID', 'Image', $SavedImage );
then it should save automatically.
OR, what i do for a single relation:
$imageField = UploadField::create('ImageEvenement', 'Image');
$imageField->setAllowedFileCategories('image');
$imageField->setAllowedMaxFileNumber(1);
hope that helps.
My code works fine but are not clean and not using SilverStripe function classes. The reason for non working saving images to dataobject, is because that I have not use :
$form->saveInto($evenement)
I would like to thank you Wmk for give me the get method to fill form with values on another post :
$form->loadDataForm($evenement)
Finaly, all works fine now!
Finaly after long time brain searching, I have found the trick! The command to use with UploadField is setValue($value) with fileIDs include with it. My final code is :
$evenement = Versioned::get_by_stage('PageCalendrierEvenement', 'Stage')->byID($evenID);
...
$uploadField = new UploadField( 'ImageEvenement', 'Image' );
$data['ImageID'] = $evenement->ImageEvenement()->ID;
$fileIDs[]=$data['ImageID'];
$uploadField->setValue(array('Files' => $fileIDs));
Thats it!

CodeIgniter, variable declaration

I have a problem with undefined variable. In view/somefolder/somefile.php they echo some variabla (<?php echo $news;?> ) . I tried everything to find where this variable is defined (Find in Path at PHPStorm), but without result. When I put print_r($GLOBALS) to test, I got
[_ci_cached_vars:protected] => Array
(
[unread] => 0
[unans] => 0
[ques_unread] => 0
[is_loged] =>
[level] =>
[avatar] => media/img/avatar.png
[username] =>
[root_cat] => 0
[logErr] =>
[radio_list] => Array
(
)
[entitet] => 1
[current_modul] => infograda
[tree] => <ul></ul>
[category] => stdClass Object
(
[id] => 153
[title] => HRONIKA
[description] =>
[parent_id] => 0
[order] => 52
[module] => infograda
[expanded] => 0
[content_type_id] => 1
)
[module] => infograda
[active] =>
[additionHtml] =>
[news] => Array
(
[0] => stdClass Object
(
[id] => 1574
[content_id] => 1574
[module] => infograda
[order] =>
[title] => Title
[text] => <div class="uvod_holder">
in Codeigniter, a view is loaded from a controller. For example:
Controller:
// this file resides in application\controllers\mycontroller.php
class Mycontroller extends CI_Controller{
function mypage() {
$data = array('news' => 'some news');
$this->load->view('page', $data);// will load views\page.php and pass the $data array
}
}
View
<!--this file resides in application\views\page.php -->
<html>
<head></head>
<body><?= $news; ?></body>
</html>
To make this work, your URL will say http://localhost/index.php/mycontroller/mypage (if you're working locally)
Your variable is being set in the controller that loads that view.

sorting and filtering not working in custom admin module

I am trying to implement an admin module in magento which has a grid in the first page and grids in the tabs while editing the grid entities.
The main grid works fine, but the grids in the tabs are not working fine.
The problem I found while I debugged the code is that, I am loading the collection in the grid with field filtering, ie I am filtering the collection with filter that is the user id. I did this because I need only data of a single user from the table. This made the entire problem, the data in the grid is coming correctly, but the filtering,sorting and searching feature inside grid is not working and returning a 404 not found error page. I tried removing the field filter I added while getting the collection, then it works fine but all the data in the table is coming which is the opposite to my requirement.
Is there any possible solution to this. Here is the way I am trying to do:
protected function _prepareCollection() {
$collection = Mage::getModel('merchant/subscriptions')->getCollection()->addFieldToFilter('user_id', Mage::registry('merchant_data')->getId());
$this->setCollection($collection); //Set the collection
return parent::_prepareCollection();
}
Thanks in advance.
ok My problem is solved there is a mistake in my code. In the grid file the function below was wrong.
public function getGridUrl() {
return $this->getUrl('*/*/transactiongrid', array('user_id',Mage::registry('merchant_data')->getId(), '_current' => true));
}
The correct method was
public function getGridUrl() {
return $this->getUrl('*/*/transactiongrid', array('user_id'=> Mage::registry('merchant_data')->getId(), '_current' => true));
}
Filter action is dependent on your below method:
public function getGridUrl() {
return $this->getUrl('*/*/grid', array('user_id' => Mage::registry('merchant_data')->getId(),'_current'=>true));
}
now this is how you will prepare collection:
protected function _prepareCollection()
{
$regData = Mage::registry('merchant_data');
if(isset($regData))
$regData = $regData->getId();
else
$regData = $this->getRequest()->getParam('user_id');
$collection = Mage::getModel('merchant/subscriptions')->getCollection()->addFieldToFilter('user_id',$regData);
...
When I dumped $regData I got this:
Cubet_Merchant_Model_Merchant Object
(
[_eventPrefix:protected] => core_abstract
[_eventObject:protected] => object
[_resourceName:protected] => merchant/merchant
[_resource:protected] =>
[_resourceCollectionName:protected] => merchant/merchant_collection
[_cacheTag:protected] =>
[_dataSaveAllowed:protected] => 1
[_isObjectNew:protected] =>
[_data:protected] => Array
(
[user_id] => 3
[firstname] => Robin
[lastname] => Cubet
[email] => robin#cubettech.com
[username] => robincubet
[password] => 51a7f45eb11fc49b5967a0039193c3ad:HSX8JkSO5lr3uaRHrzd86i7gb0RATeDb
[created] => 2013-12-12 08:34:28
[modified] => 2013-12-16 09:03:56
[logdate] =>
[lognum] => 0
[reload_acl_flag] => 1
[is_active] => 1
[extra] => N;
[rp_token] =>
[rp_token_created_at] =>
)
[_hasDataChanges:protected] =>
[_origData:protected] => Array
(
[user_id] => 3
[firstname] => Robin
[lastname] => Cubet
[email] => robin#cubettech.com
[username] => robincubet
[password] => 51a7f45eb11fc49b5967a0039193c3ad:HSX8JkSO5lr3uaRHrzd86i7gb0RATeDb
[created] => 2013-12-12 08:34:28
[modified] => 2013-12-16 09:03:56
[logdate] =>
[lognum] => 0
[reload_acl_flag] => 1
[is_active] => 1
[extra] => N;
[rp_token] =>
[rp_token_created_at] =>
)
[_idFieldName:protected] => user_id
[_isDeleted:protected] =>
[_oldFieldsMap:protected] => Array
(
)
[_syncFieldsMap:protected] => Array
(
)
)

Resources