PHP output is empty - php

this is my code php5.6
$lang = array(
"HTML" => "60%",
"CSS" => "80%",
"JS" => "70%",
"PHP" => "50%",
);
echo "<ul>";
for ($lang=0; $lang < count($lang) ; $lang++) {
echo "<li>" . $lang[$lang] . "</li>";
}
echo "</ul>";
When i run my page it does't show any thing

Use foreach on arrays.
$langs = array(
'HTML'=> '60%',
'CSS' => '80%',
'JS' => '70%',
'PHP' => '50%',
);
echo '<ul>';
foreach ($langs as $lang=>$per) {
echo "<li>$lang $per</li>";
}
echo '</ul>';

Related

how to get values using foreach

I am trying to create menu dynamically according to the user input. I have an array like this, I want to get each value by their indices or I want to use the values to create menu by their order:
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
);
Try the following code:
foreach ($menu['main_menu'] as $item => $menu_item) {
if (is_array($menu_item)) {
foreach ($menu_item as $menu_name => $menu_attr) {
echo "menu name: " . $menu_name;
echo '<br/>';
foreach ($menu_attr as $attr => $val) {
echo $attr . "->" . $val;
echo '<br/>';
}
}
}
else{
echo $item.": ".$menu_item;
echo "<br />";
}
}
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
'menu_item4'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
'menu_item5'=>array('development2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item6'=>array('php2' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development2' )),
);
$submenus = [];
$mainMenu = [];
$menuStart;
$menuEnd;
//prapring data
foreach($menu['main_menu'] as $item){
if(is_array($item)){
foreach($item as $link){
if(isset($link['sub_menu']) & $link['sub_menu'] != '0'){
$submenus[] = [
'name' => key($item),
'sub_menu' => $link["sub_menu"],
'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
'end' => "</li>"
];
} else {
$mainMenu[] = [
'name' => key($item),
'body' => "<li class='{$link["Class name"]}'><a href='{$link["URL"]}' class='{$link["Class name"]}' id='{$link["menu_id"]}'>" . key($item) . "</a>",
'end' => "</li>"
];
}
}
} else {
$menuStart = "<ul class='{$item}'>";
}
}
/// menu generatig
$menuEnd = '</ul>';
echo $menuStart;
foreach($mainMenu as $menu){
echo $menu['body'];
foreach($submenus as $submenu){
if($submenu["sub_menu"] == $menu['name']){
echo "<ul>";
echo $submenu['body'];
echo "</ul>";
}
}
echo $menu['end'];
}
echo $menuEnd;
Following code may help you.
$menu['main_menu']= array(
'menu_name' =>'main_menu',
'menu_item1'=>array('home' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'menu_id'=>'new_menu' )),
'menu_item2'=>array('development' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'0' )),
'menu_item3'=>array('php' => array('URL' => 'Http://www.google.com','Class name'=>"item_class",'sub_menu'=>'development' )),
);
foreach($menu['main_menu'] as $menu_name=>$menu_value){
echo "<br><br>menu_name ". $menu_name;
if(is_array ( $menu_value )){
foreach($menu_value as $k=>$v){
if(is_array ( $v )){
foreach($v as $key=>$value)
echo "<br>key=".$key." value=".$value;
}
}
}
}
truy this
foreach($menu['main_menu'] as $menu_name=>$menu_value)
{
if(is_array($menu_value))
{
foreach($menu_value as $value)
{
if(is_array ( $value ))
{
echo "<a href='{$value["URL"]}' class='{$value["Classname"]}'>" . key($menu_value) . "</a><br />"; //
}
}
}
}

Multi level menu from array in PHP

I'm trying to make a menu from an array in PHP.
I would like to make it more readable, so I used an array.
My array is the following:
$menu = array(
'calendar' => array(
'text' => 'Calendar',
'rights' => 'user'
),
'customers' => array(
'text' => 'Customers',
'rights' => 'user',
'sub' => array(
'create-new' => array(
'text' => 'Create new customer',
'rights' => 'user'
),
'show-customers' => array(
'text' => 'Show all customers',
'rights' => 'user'
)
)
)
);
And the PHP to parse the array:
buildMenu($menu);
function buildMenu($menu_array, $is_sub=FALSE) {
$attr = (!$is_sub) ? ' id="menu"' : ' class="submenu"';
$menu = "<ul".$attr.">";
foreach($menu_array as $id => $properties) {
foreach($properties as $key => $val) {
if(is_array($val)) {
$sub = buildMenu($val, TRUE);
}
else {
$sub = NULL;
$$key = $val;
}
}
if(!isset($url)) {
$url = $id;
}
$menu .= "<li>".$text."".$sub."</li>";
unset($url, $text, $sub);
}
return $menu . "</ul>";
}
Do I miss something ?
It don't echo me anything.
Thanks for your help.
Just return will not give output you need to either use below code, inside function.
echo $menu . "</ul>";
instead of
return $menu . "</ul>";
or use below code if you return your html output.
echo $output = buildMenu($menu);
instead of
buildMenu($menu);

correct way to display 3 dimensional array with key and value php

I have a 3 dimensional array defined like:
$seccc = array(
array(
"Href" => base_url().'capture/',
"Icono" => base_url().'assets/images/icon_home.png',
"Texto" => 'Captura',
"Submenu" => array(1,2,3)
),
array(
"Href" => base_url().'seg/',
"Icono" => base_url().'assets/images/icon_tra.png',
"Texto" => 'Seg',
"Submenu" => array('ALFA','OMEGAL','DELTA')
),
array(
"Href" => base_url().'usr/',
"Icono" => base_url().'assets/images/icon_users.png',
"Texto" => 'Users',
"Submenu" => ''
),
array(
"Href" => base_url().'clients/',
"Icono" => base_url().'assets/images/icono_gro.png',
"Texto" => 'Clients',
"Submenu" => ''
),
array(
"Href" => base_url().'suc/',
"Icono" => base_url().'assets/images/icupo.png',
"Texto" => 'Suc',
"Submenu" => ''
)
);
I am doing foreach loop like
foreach ($seccc as $part)
{
foreach ($part as $item)
{
echo '<li>'.$item["Href"];
if(is_array($item["Submenu"]))
{
foreach($item["Submenu"] as $subkey)
{
echo '<ul>';
echo $subkey;
echo '</ul>';
}
}
}
echo '</li>';
}
}
However I can not access to "Href", "Icono", "Texto" or "Submenu" items, How to access their values
seems $item["Href"] does not work
foreach ($seccc as $part)
{
// use $part instead of $item, here you can get $part['Icono'], $part['Texto'] etc
echo '<li>'.$part["Href"];
if(is_array($part["Submenu"]))
{
// loop over $part['Submenu'] if it's an array
foreach($part["Submenu"] as $key => $subkey)
{
echo '<ul>';
echo $subkey;
echo '</ul>';
}
}
echo '</li>';
}
You have one loop to many
foreach ($seccc as $item)
{
echo '<li>'.$item["Href"];
if(is_array($item["Submenu"]))
{
foreach($item["Submenu"] as $subkey)
{
echo '<ul>';
echo $subkey;
echo '</ul>';
}
}
}

3 dimension Array foreach

Im having some problem displaying a multidimensional array...
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
Basically I want to display everything and also save it in a variable...
echo "<ul>";
foreach($copyscape as $name => $value)
{
echo "<li>" . $name . " : ". $value . "</li>";
}
echo "</ul>";
I tried inserting another set of foreach inside but it gives me an
Invalid argument supplied for foreach()
Try with this code :
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
function test_print($item, $key)
{
echo "<li>" . $key . " : ". $item . "</li>";
}
echo "<ul>";
array_walk_recursive($copyscape, 'test_print');
echo "</ul>";
use this
foreach ($copyscape as $key=>$value){
echo "<li>" . $key. " : ". $value. "</li>"; // this for main Array
if (is_array ($value))
{
foreach ($value as $childArrayKey => $childArrayValue ){
echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array
}
}
}
or
foreach ($copyscape as $key=>$value){
echo "<li>" . $key. " : ". $value. "</li>"; // this for main Array
foreach ($value['result']['number'] as $childArrayKey => $childArrayValue ){
echo "<li>" . $childArrayKey . " : ". $childArrayValue . "</li>"; // this for childe array
}
}
In case if you want to have lists inside of lists (nested levels)..
function outputLevel($arr)
{
echo '<ul>';
foreach($arr as $name => $value)
{
echo '<li>';
if (is_array($value))
outputLevel($value);
else
echo $name . ' : ' . $value;
echo '</li>'
}
echo '</ul>';
}
outputLevel($copyscape);
Try following code:
<?php
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
MultiDimRecuArray($copyscape);
function MultiDimRecuArray($copyscape) {
echo "<ul>";
foreach ($copyscape as $key=>$val) {
if(is_array($val)){
MultiDimRecuArray($val);
}else{
echo "<li>" . $key . " : ". $val . "</li>";
}
}
echo "</ul>";
}
Try something with a recursive function, like this:
function printArray($array)
{
echo "<ul>";
foreach($array as $name=>$value)
{
if(is_array($value))
{
printArray($value);
}
else
{
echo "<li>" . $name . " : ". $value . "</li>";
}
}
echo "</ul>";
}
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
printArray($copyscape);
Use this
<?php
$copyscape = array (
'query' => 'www.example.html',
'querywords' => 444,
'count' => 230,
'result' => array(
'number' => array(
'index' => 1,
'url' => 'http://www.archives.gov/exhibits/charters/declaration_transcript.html',
'title' => 'Declaration of Independence - Text Transcript',
'minwordsmatched' => 406,
'viewurl' => 'http://view.copyscape.com/compare/w4med9eso0/1'
)
)
);
PrintMultiDimArray($copyscape);
function PrintMultiDimArray($copyscape) {
echo "<ul>";
foreach ($copyscape as $key=>$val) {
if(is_array($val)){
echo "<li>";
PrintMultiDimArray($val);
echo "</li>";
}else{
echo "<li>" . $key . " : ". $val . "</li>";
}
}
echo "</ul>";
}
?>
try this..
echo '<ul>';
displayList($copyscape);
echo '</ul>';
function displayList($arr)
{
foreach($arr as $name => $value) {
if (is_array($value)) {
displayList($value);
}
else {
echo '<li>';
echo $name . ' : ' . $value;
echo '</li>';
}
}
}
Use this:
function print_stuff($arr){
foreach($arr as $key => $val){
if(is_array($val)){
echo "$key: <ul>";
print_stuff($val);
echo "</ul>";
} else {
echo "<li>$key : $val</li>\n";
}
}
}
echo "<ul>";
print_stuff($copyscape);
echo "</ul>";
The code prints a nested list including the key name of the nested lists:
query : www.example.html
querywords : 444
count : 230
result:
number:
index : 1
url : http://www.archives.gov/exhibits/charters/declaration_transcript.html
title : Declaration of Independence - Text Transcript
minwordsmatched : 406
viewurl : http://view.copyscape.com/compare/w4med9eso0/1

If...Else statement display error logic in CakePHP

I have the following statement to display all my user's passions, it displays all those only with passions, and those without passions, it doesnt show the 'empty' text. Please assist. $cell is a table which I use to append all my data.
foreach ($user['Passion'] as $passion) {
if (empty($passion['PassionsUser']['passion_tag'])) {
$cell .= 'empty';
}
if ($passion['PassionsUser']['type'] == 'personal') {
$cell .= '[' . $html->link(ucwords($passion['PassionsUser']['passion_tag']), array('controller' => 'passions', 'action' => 'view', $passion['PassionsUser']['passion_tag']), array('escape' => false, 'class' => 'normalTip', 'title' => ucwords($passion['PassionsUser']['type'] . ' passion'))) . ']';
} elseif ($passion['PassionsUser']['type'] == 'professional') {
$cell .= '[' . $html->link(ucwords($passion['PassionsUser']['passion_tag']), array('controller' => 'passions', 'action' => 'view', $passion['PassionsUser']['passion_tag']), array('escape' => false, 'class' => 'normalTip', 'title' => ucwords($passion['PassionsUser']['type'] . ' passion'))) . ']';
}
}
$cells[] = $cell;
Guessing by your previous question, the problem is that if the user doesn't have any passions, $user[ 'Passion' ] will be empty so the loop doesn't run at all. Change the code to:
if( empty( $user[ 'Passion' ] ) {
$cell = 'empty';
}
else foreach( $user[ 'Passion' ] as $passion ) {
....
}

Categories