Array ( [0] => rubix )
Array ( [0] => limo )
Array ( [0] => icecream )
I need to convert this array into following type. How can I do that? For example like this:
Array ( [0] => rubix [1] => schoolbag [3] => limo [4] => frezzer [5] => paper )
array_merge() is an option if you want to combine arrays.
$array1 = array("id1" => "rubix ");
$array2 = array("id2" => "limo ", "id3" => "icecream ", "id4" => "frezzer ");
$array3 = array_merge($array1, $array2/*, $arrayN, $arrayN*/);
Related
I want to build a custom menu output so I am calling this class:
$pages = \Ip\Menu\Helper::getMenuItems('menu1');
It returns this:
Array
(
[0] => Ip\Menu\Item Object
(
[title:protected] => Home
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/home
[target:protected] =>
[selected:protected] =>
[current:protected] => 1
[children:protected] => Array
(
[0] => Ip\Menu\Item Object
(
[title:protected] => Lorem ipsum
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/lorem-ipsumy
[target:protected] =>
[selected:protected] =>
[current:protected] =>
[children:protected] =>
[depth:protected] => 2
[disabled:protected] => 0
[blank:protected] => 0
)
[1] => Ip\Menu\Item Object
(
[title:protected] => about
[pageTitle:protected] =>
[url:protected] => http://porto.gigaweb.me/about
[target:protected] =>
[selected:protected] =>
[current:protected] =>
[children:protected] =>
[depth:protected] => 2
[disabled:protected] => 0
[blank:protected] => 0
)
)
[depth:protected] => 1
[disabled:protected] => 0
[blank:protected] => 0
)
)
I have tried to turn it into an array but it returns value like this [*title].
I want to loop through it and output the values but cant seem to do it.
It is an array of objects. Not an array of arrays. So you have to do something like this:
foreach ($pages as $menuItem) {
$page->getTitle();
}
You can find other methods of $page object here http://www.impresspages.org/menu-item
Please keep in mind, that you are getting MenuItem objects, not Page objects.
I'm trying to store widget id's from a specific sidebar in an array. But I can't seem to get the first part done.
I have the following array:
$results = get_option('sidebars_widgets');
print_r($results); /* Gives me the following array */
Array
(
[orphaned_widgets_1] => Array
(
[0] => search-2
[1] => recent-posts-2
[2] => recent-comments-2
[3] => archives-2
[4] => categories-2
[5] => meta-2
)
[sidebar-footer] => Array
(
)
[wp_inactive_widgets] => Array
(
)
[sidebar-main] => Array
(
)
[Shortcodes] => Array
(
[0] => pages-2
[1] => widget-id
[2] => another-id
[3] => yet-another
)
[array_version] => 3
)
Then, I want all of the Shortcodes widget id's in array. Like doing:
$shortcode_widget_id = array();
foreach { code here to put the 4 id's in the $shortcode_widget_id array }
But how do I get the list of only the Shortcode id's, not the other ones?
$shortcode_array = $presentarray['Shortcodes'];
$shorcode_keys = array_keys( $shortcode_array);
print_r($shorcode_keys );
is this you are after ?
In my cakephp i need to retrieve datas from database.
how can i display the table result in neat format.
Controller:
function MarketingTaskmanagement(){
$data['list'] = $this->TravancoAdmin->get_task_all();
$this->set($data);
$this->render('Marketing/taskmanagement');
}
Model:
function get_task_all(){
$users = $this->query('select * from tbl_tasks');
if($users){
return $users;
}else{
return 1;
}
}
View:
But it displays values as so many arrays:
Array ( [0] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 1 [task_title_mm] => ghjg [task_description_mm] => gjg [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [1] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 2 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [2] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 3 [task_title_mm] => hf [task_description_mm] => hfgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/27/2012 ) ) [3] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 4 [task_title_mm] => hfh [task_description_mm] => fgh [task_from_mm] => 09/03/2012 [task_to_mm] => 09/20/2012 ) ) [4] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 5 [task_title_mm] => h [task_description_mm] => h [task_from_mm] => 09/04/2012 [task_to_mm] => 09/28/2012 ) ) [5] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 6 [task_title_mm] => hjk [task_description_mm] => hk [task_from_mm] => 09/05/2012 [task_to_mm] => 09/22/2012 ) ) [6] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 7 [task_title_mm] => v [task_description_mm] => v [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [7] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 8 [task_title_mm] => d [task_description_mm] => d [task_from_mm] => 09/03/2012 [task_to_mm] => 09/28/2012 ) ) [8] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 9 [task_title_mm] => f [task_description_mm] => d [task_from_mm] => 09/04/2012 [task_to_mm] => 09/27/2012 ) ) [9] => Array ( [tbl_tasks] => Array ( [task_ids_mm] => 10 [task_title_mm] => b [task_description_mm] => b [task_from_mm] => 09/05/2012 [task_to_mm] => 09/27/2012 ) ) )
In codeigniter there have something like this:
$users = $this->query('select * from tbl_tasks')->result();
Then it displays the correct array without any complication .
So how can i do this in cakephp?
And i need to display the all task_title_mm values from the table result in view page.
How can i do this?
Use the "find" method.
$this->ModelName->find('all');
or
$this->ModelName->find('list');
If you need less information from query, put:
$this->ModelName->recursive = -1;
before find method.
Since you expanded your question I'll elaborate a bit on the anwser.
It is a good practice to not use $this->query() where a native cake search would work.
if you don't have a model file for your tbl_tasks create one:
<?php
class Task extends AppModel {
var $name = 'Task';
var $displayField = 'task_title_mm';
var $useTable = 'tbl_tasks';
}
Load it inside your get_task_all() function. You can load a model dinamically inside another.
function get_task_all(){
//load the Task model on the fly if it isnt associated with this one
//loadModel won't work because we're inside a model.
//App:import might work, dunno.
$this->Task = ClassRegistry::init('Task');
$users = $this->Task->find('list');
return $users ? $users : 1;
}
find('list') works if you declare 'task_title_mm' as displayField otherwise you can use $this->Task->find('all', array('fields' => array('task_title_mm')));, but the result will be prefixed with ['Task'];
function get_task_all(){
$users = $this->query('select * from tbl_tasks') ....
Hey hey hey! Stop right there! That just gave me a heart attack.
Zero. Read the cookbook! No seriously, Stop! before you go any further I would suggest going through the cookbook atleast once. Do things the right way from the beginning is the way to go.
Use proper methods that cake has provided you to get the job done. SQL queries can get the job done just fine, but trust me using cake's inbuilt methods will benefit you long term and make your life much easier.
As per your query on how to display the data in table format, use cake bake: http://book.cakephp.org/1.3/view/1522/Code-Generation-with-Bake . The cake bake utility will generate the basic scaffold for your application controllers, models, views included. The default view templates of cake are really useful and has almost all the functionality built in, pagination etc.
Want a paginated result? No problem use paginate!
function index() {
$this->ModelAlias->recursive = -1;
$this->set('list', $this->paginate());
}
And voila! you have your paginated data inside $list readily available in your view. And if you have your views baked everything is nicely displayed in a table as you needed.
Don't want to fetch everything? Insert conditions into paginate like this:
function index() {
$this->ModelAlias->recursive = -1;
$this->paginate = array(
'conditions' => array('ModelAlias.tasks' => 'incomplete')
);
$this->set('list', $this->paginate());
}
Not only this you can include virtually any otherkey like 'fields', 'order', 'limit' that cake supports.
Once you start using cake's functions things start to get really awesome. For fetching related data you can use cake 'containable' behavior. This is just one thing of hundreds that cake does really well. I can't outline everything here, I insist please have a go at the cookbook http://book.cakephp.org
If I do a print_r($_SESSION) in my page.ctp I get:
Array
(
[Config] => Array
(
[userAgent] => b3346028c15f82ac5d4b25c4f50d8718
[time] => 1281034201
[timeout] => 100
)
[manualLogout] => 1
[Message] => Array
(
)
[Auth] => Array
(
[redirect] => /events/add/controller:events
)
[facebookSynced] => 1
)
The var facebookSynced I set in my controller with $this->Session-write() - and there it is in the session as expected. But when I do a pr($this->Session) or a pr($session) from page.ctp I get:
SessionHelper Object
(
[helpers] => Array
(
)
[__active] => 1
[valid] =>
[error] =>
[_userAgent] => b3346028c15f82ac5d4b25c4f50d8718
[path] => /
[lastError] =>
[security] => medium
[time] => 1281016202
[sessionTime] => 1281034202
[watchKeys] => Array
(
)
[id] =>
[host] =>
[timeout] =>
[base] => /Eclipse/Calc_1.3.2/trunk
[webroot] => /Eclipse/Calc_1.3.2/trunk/
[here] => /Eclipse/Calc_1.3.2/trunk/users/login
[params] => Array
(
[controller] => users
[action] => login
[named] => Array
(
)
[pass] => Array
(
)
[plugin] =>
[form] => Array
(
)
[url] => Array
(
[url] => users/login
)
[models] => Array
(
[0] => User
)
)
[action] => login
[data] =>
[theme] =>
[plugin] =>
)
How do I access my session with the facebookSynced var in it, and what is the difference between these two 'sessions'. Extra info: in core.php I have:
Configure::write('Session.save', 'php');
Configure::write('Session.cookie', 'CAKEPHP');
Configure::write('Session.save', 'custom_sesh');
Configure::write('Session.timeout', '180');
Configure::write('Session.checkAgent', true);
Configure::write('Session.start', true);
The contents of custom_sesh is just one line:
ini_set('session.cookie_lifetime', 0);
The first result from print_r($_SESSION) is a list of the values of the actual session variable.
The second result from pr($this->Session) shows the contents of CakePHP's Session helper object.
Both are entirely different things. If you wanted to access the value of your session variable facebookSynced you would probably want to do something like:
$foo = $_SESSION["facebookSynced"];
In your view you should be using the Session helper.
echo $session->read('Key.value');
Where you have written in something like, in your controller,
$this->Session->write('Key.value','example');
if you are using
$this->Session->write('facebookSynced',true);
in your controller,
just use
$foo = $this->Session->read('facebookSynced');
in the view.
as simple as that ! :)
Array
(
[0] => Array
(
[auth_id] => 1
[auth_section] => Client Data Base
[auth_parent_id] => 0
[auth_admin] => 1
[sub] => Array
(
[0] => Array
(
[auth_id] => 2
[auth_section] => Client Contact
[auth_parent_id] => 1
[auth_admin] => 1
)
)
)
[1] => Array
(
[auth_id] => 6
[auth_section] => All Back Grounds
[auth_parent_id] => 0
[auth_admin] => ,4
[sub] => Array
(
[0] => Array
(
[auth_id] => 7
[auth_section] => Edit Custom
[auth_parent_id] => 6
[auth_admin] => 1
)
)
)
[2] => Array
(
[auth_id] => 20
[auth_section] => Order Mail
[auth_parent_id] => 0
[auth_admin] => 1
[sub] =>
)
}
When I process the sub inner array
for($in=0 ; $in < count($auth); $in++){
$autsub = $auth[$in]["sub"];
for($g=0 ; $g<count($autsub); $g++){
echo $autsub[$g]["auth_id"];
}
}
it shows this error
Fatal error: Cannot use string offset as an array.........
how can I avoid that :(
The problem is that the last entry in the array (2) does not have a sub array, but you're trying to access it anyway. You'll need to check if the entry exists and if it's an array before looping over it. Here an example using foreach:
foreach ($array as $auth) {
if (!empty($auth['sub']) && is_array($auth['sub'])) {
foreach ($auth['sub'] as $sub) {
if (!empty($sub['auth_id'])) {
echo $sub['auth_id'];
}
}
}
}
You can test the offset type with the is_array() function. If you want a better answer, post the processing code.
Test if $auth[$in] and $autsub[$g] are arrays.