If...Else statement display error logic in CakePHP - php

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 ) {
....
}

Related

How to get foreach loop with item array into a input form

I have to following foreach loop
$data = array(
"success" => false,
"results" => array()
);
foreach ($cart->get_contents() as $item) {
$data['items'][] = array(
'description' => urlencode($item['name']),
'amount' => urlencode($item['price']),
'quantity' => urlencode($item['qty'])
);
}
$data['success'] = true;
Now I would like to for the following for each item
<input type="hidden" name="' . $field . '" value="' . htmlentities($value) . '">
where the $field would be the item description, and the htmlentities($value) would be the corresponding value from the loop.
Any help welcome as I am not sure where to start, sorry if this is very obvios
I think (if I understand you) that you can achieve this like:
$data = [
'success' => false,
'items' => [] // Probably itemsm not results??
];
foreach ($cart->get_contents() as $item) {
$data['items'][] = array(
'description' => urlencode($item['name']),
'amount' => urlencode($item['price']),
'quantity' => urlencode($item['qty'])
);
}
$data['success'] = true;
foreach ($data['items'] as $item) {
echo "<input type='hidden' name='" . $item['description'] . "'' value='" . htmlentities($item['amount']) . "'>";
}
I also updated the code a little.

PHP output is empty

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>';

selected attribute between <option tags in a foreach loop

I'm looking for some guidance here where I've struggled to get working.
I have an associative array with the following options:
$options_format [] = [
'name' => 'Audio',
'type' => 'Audio',
];
$options_format [] = [
'name' => 'EBook',
'type' => 'EBook',
];
$options_format [] = [
'name' => 'Hardcover',
'type' => 'Hardcover',
];
$options_format [] = [
'name' => 'Paperback',
'type' => 'Paperback',
];
The following for each loop then walks through the array to output the html:
<?php
$select = 'selected';
foreach ($options_format as $key => $value) {
echo '<option value="' . $value['name'] . '"' . (isset($format) && $format == $value['name']) { "$select" };
echo ">" . $value['name'];
echo '</option>' . "\n";
}
?>
The $format variable hold the value for which ever is selected and it will then check if the $format variable is set. But what I'm trying to do is if the form entry has a validation error, it will retain the value of what the user had selected previously.
Any thoughts?
$select = 'selected';
foreach ($options_format as $key => $value) {
echo '<option value="' . $value['name'] . '"' . ((isset($format) && $format == $value['name']) ? "$select" : "") ;
echo ">" . $value['name'];
echo '</option>' . "\n";
}
The way you write your ternary operator:
((isset($format) && $format == $value['name']) ? "$select" : "")
Assuming that $format holds the value of your $_POST['select_field_name'].
Try this you can change $format with your required value.
<?php
$options_format [] = [
'name' => 'Audio',
'type' => 'Audio',
];
$options_format [] = [
'name' => 'EBook',
'type' => 'EBook',
];
$options_format [] = [
'name' => 'Hardcover',
'type' => 'Hardcover',
];
$options_format [] = [
'name' => 'Paperback',
'type' => 'Paperback',
];
$format = "Hardcover";
$select = 'selected';
echo "<select>";
foreach ($options_format as $key => $value) {
echo "<option value='".$value['name'] ."' ";
if($value['name']==$format)
echo $select. ">";
else
echo ">";
echo $value['name'] .'</option>' . "\n";
}
echo "</select>";
?>

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);

Categories