How to add static li inside dynamic list using php? - php

I have 5 menu in the website which is coming dynamically :
menu1 menu2 menu3 menu4 menu5
Now I want to add menu6 before menu5 using php.
My code:
foreach ($collection as $category) {
$i++;
$menuCategory = $this->getCategoryAsArray($category, $currentCategory);
$class = '';
$class .= 'nav'. $i;
if($i == 1) {
$class .= ' first';
} elseif ($i == $count) {
$class .= ' last';
}
if($menuCategory['is_active']) {
$class .= ' active';
}
//if($this->hasChildProduct($category)) {
//$class .= ' parent';
//}
if($this->hasChildSubCategory($category)) {
$class .= ' parent';
}
$class .= ' level-top';
$html .= '<li class="level0 '. $class .'">';
$html .= '<a href="'. $menuCategory['url'] .'">';
$html .= '<span>'. $menuCategory['name'] .'</span>';
$html .= '</a>';
//if($this->hasChildProduct($category)) {
//$html .= $this->getChildProductMenuHtml($category, $i);
//}
if($this->hasChildSubCategory($category)) {
$html .= $this->getChildSubcategoryMenuHtml($category, $i);
}
$html .= '</li>';
}
Menu6 is the static link which code is:
<li class="vertical-submenu" id="static-menu"><a href="<?php echo $block->getUrl('menu6')?>"><?php echo __('menu6')?></li>

I am not getting the code because i am not aware of values coming in the array in foreach loop.
But I can explain the situation to you via Algorithm.
$i = 0;
foreach(expression){
if(value == "menu5"){
write("Menu 4")
}
Write("Menu ".$i)
$i++;
}
Hope your problem is resolved with this. If you need anything else or want to elaborate more about you problem text me at shahrukhusmaani#gmail.com

Related

dynamic tab bootstrap creation doesn't work well

I created a dynamic bootstrap tab with a specific tab to insert in the administration inside description field.
The problem when I want to display the information inside the catalog, it display in the first tab all the text
If I click on the tab I have no problem, the information is correct after
<tabCatalog> and </tabCatalog> are the element allow to create the dynamic tab on the catalog include include inside the text.
$desc = $this->getProductsDescription();
$product_tab_title = '<div id="categoriesTabs" style="overflow: auto;">';
$product_tab_title .='<ul class="nav nav-tabs flex-column flex-sm-row" role="tablist" id="myTab">';
if (strpos($desc, '<tabCatalog>') !== FALSE) {
$cut = explode('<tabCatalog>', trim($desc));
$c = 0;
foreach ($cut as $k => $part) {
if (trim($part) != '') {
if (strpos($part, '</tabCatalog>') !== FALSE) {
$t = substr($part, 0, strpos($part, '</tabCatalog>'));
if ($k = 0) {
$class = 'nav-link active';
} else {
$class = 'nav-link';
}
$product_tab_title .= '<li class="nav-item">' . $t . '</li>';
}
}
$c++;
}
}
$product_tab_title .= '</ul>';
$product_tab_title .= '</div>';
$product_tab_description = '<div>';
$product_tab_description .= '<div class="tab-content">';
if (strpos($desc, '<tabCatalog>') !== FALSE) {
$cut = explode('<tabCatalog>', trim($desc));
$n = 0;
foreach ($cut as $n => $part) {
if (trim($part) != '') {
if (strpos($part, '</tabCatalog>') !== FALSE) {
$r = substr($part, strpos($part, '</tabCatalog>') + 13);
$product_tab_description .= '<div class="tab-pane active" id="tab' . $n . '">' . $r . '</div>';
}
}
$n++;
}
}
// content tab
$products_description_content = '<!-- Start products_description_tab -->' . "\n";
$products_description_content .= '<div class="col-md-' . $content_width .'">';
$products_description_content .= '<div class="separator"></div>';
$products_description_content .= '<div class="contentText">';
$products_description_content .= $product_tab_title;
$products_description_content .= $product_tab_description;
$products_description_content .= '<div>';
$products_description_content .= '<div>';
$products_description_content .= '<div>';
$products_description_content .= '<div>';
$products_description_content .= '<!-- end products_description_tab -->' . "\n";
echo $products_description_content
Both tabs have the active class...
foreach ($cut as $n => $part) {
if (trim($part) != '') {
if (strpos($part, '</tabCatalog>') !== FALSE) {
$r = substr($part, strpos($part, '</tabCatalog>') + 13);
$product_tab_description .= '<div class="tab-pane active" id="tab' . $n . '">' . $r . '</div>';
}
}
$n++;
}
I suggest you to remove active from the class list in that loop and add that tiny jQuery script on document ready:
$(document).ready(function(){
$(".tab-pane").first().addClass("active");
});
Or .eq(1) (zero-based) instead of .first() if you prefer having the second tab active on page load.

Adding background color to element within php if-statement

I have the following code in which I want to add an element to my if statement, but I am unsure how to approach this. What I am looking to do is add a background-color to the else part of it. Within this:
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
So if $status is not 0, I want to set background color for the class goal-box-right.
Is there anyway I can manipulate the CSS through php like this?
foreach ($rows as $row) {
$status = $row['status'];
if ($status == 0) {
$status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
$goal_date = $row['date'];
$fixed_goal_date = fixDate($goal_date);
$html = "";
$html .= '<div class="goal-box" id="comment-'.$row['id'].'">';
$html .= '<div class="goal-box-left">'; //added
$html .= '<div class="goal-post-status">'.$status_img. '</div>';
$html .= '</div>'; //added
$html .= '<div class="goal-box-right">'; //added
$html .= '<div class="goal-post-title">'.$row['title']. '</div>';
$html .= '<div class="goal-post-date">'.$fixed_goal_date. '</div>';
$html .= '<div class="goal-post-description">'.$row['description']. '</div>';
$html .= '</div>'; //added
$html .= '</div>';
Separate the logic - add a CSS class based on the status then set the colours in the stylesheet.
$class = $status != 0 ? 'status-nonzero' : '';
$html .= '<div class="goal-box-right ' . $class . '">';
CSS:
.goal-box-right.status-nonzero { background-color: #foo }
Advantage is keeping the style logic in the CSS where it belongs and away from the code that's generating the HTML.
So what's the problem:
$style = '';
if ($status == 0) {
$style = ' style="your-style:here"';
$status_img = '<img src="../icons/collection/x-sign.png" alt="Goal Not Complete">';
}
else {
$status_img = '<img src="../icons/collection/checkmark.png" alt="Goal Complete">';
}
// ...
$html .= '<div class="goal-box-right"' . $style . '>'; //added

How can I echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from the database. If we just use foreach, we will get the result one item per a loop, but actually I want it echo two items per one times as ub the below function.
This is my PHP function using foreach to fetch data from database.
I've used row item selector to wrap product selector, so I want to echo a block of product two times, and then it should echo the row item.
Example: row item = 1 then product = 2
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
foreach ($data as $k => $row) {
$out .= '<div class="row item">';
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
$out .= '</div>';
}
}
return $out;
}
This function will echo one item for a loop.
You can not print two times in one iteration of a loop. You can use conditional HTML output to do this job.
Consider this:
function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
$counter = 1;
$length = count($data);
if ($data) {
foreach ($data as $k => $row) {
if ($counter % 2 != 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if ($counter % 2 == 0 || $length == $counter) {
$out .= '</div>';
}
$counter++;
}
}
return $out;
}
You can use the modulus operator to make the check. If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero):
public function last_upated_products() {
$data = $this->newest_products_from_db('products');
$out = '';
if ($data) {
$i = 0;
foreach ($data as $k => $row) {
if( ($i % 2) === 0) {
$out .= '<div class="row item">';
}
$out .= '<div class="product">';
$out .= '<div class="image">';
$out .= '<img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive">';
$out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
$out .= '</div>';
$out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4>' . $row['prod_name'] . '</h4>';
$out .= '<p>' . $row['prod_descrip'] . '</p>';
$out .= '</div>';
$out .= '</div>';
if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
$out .= '</div>';
}
$i++;
}
}
return $out;
}
Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number.

More efficient way to build dynamic menu

I am creating a dynamic menu whos items appear depending on a set 'mode' (that is passed via ajax). Off this it creates the menu, disabling and hiding icons that are not associated with that mode.
The problem is, in my implementation there are a lot of if conditions. Can anyone show me a cleaner way of doing what I am trying to achieve?
My code is:
public function gridMenu()
{
$mode = Validate::sanitize($_POST['mode']);
$modes = array(
'products' => array('edit', 'delete', 'archive')
);
$output = '<div id="hexContainer">';
for($i = 1; $i < 7; $i++) {
$img = '';
$output .= '<div class="hex hex' . $i;
if($i == 1)
{
if(in_array('edit', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Edit" src="' . ROOT . 'images/edit.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 2)
{
if(in_array('zzz', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
if($i == 3)
{
if(in_array('delete', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Delete" src="' . ROOT . 'images/delete.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 4)
{
if(in_array('xxx', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
if($i == 5)
{
if(in_array('archive', $modes[$mode]))
{
$output .= ' hex-selectable';
$img = '<img data-option="Archive" src="' . ROOT . 'images/archive.png">';
} else {
$output .= ' hex-disabled';
}
}
if($i == 6)
{
if(in_array('zzz', $modes[$mode]))
{
$output .= ' hex-selectable';
} else {
$output .= ' hex-disabled';
}
}
$output .= '">';
$output .= $img;
$output .= '</div>';
}
$output .= '<div class="hex hex-mid"></div>';
$output .= '</div>';
echo $output;
}
Use the switch function:
switch ($i){
case 1:
//code for $i==1
break;
case 2:
// code for $i==2
break;
//...
}
This hasn't been tested, but you could try something along the lines of this.
Basically from your code I'm assuming that you're trying to insert an image only on the odd rounds. So with that I created a $_modes and $_mode variables which will hold the values for each round.
With these values, we will automatically create the HTML classes using the rules you have set in your code, and on each round if it's odd, and in the array then we also add the image.
public function gridMenu() {
$mode = Validate::sanitize($_POST['mode']);
$modes = array(
'products' => array('edit', 'delete', 'archive')
);
$_modes = array(
false,
array('Edit', 'edit'),
array('ZZZ', 'zzz'),
array('Delete', 'delete'),
array('XXX', 'xxx'),
array('Archive', 'archive'),
array('ZZZ', 'zzz'),
);
$output = '<div id="hexContainer">';
for($i = 1; $i < 7; $i++) {
$_mode = $_modes[$i];
$img = '';
$classnames = array('hex', 'hex' . $i);
if ( ($i % 2) === 0 && in_array($_mode[1], $modes[$mode])) {
$img = '<img data-option="' . $_mode[0] . '" src="' . ROOT . 'images/' . $_mode[1] . '.png">';
}
$classnames[] = (in_array($_mode[1], $modes[$mode]) ? 'hex-selectable' : 'hex-disabled');
$output .= '<div class="' . implode(' ', $classnames) . '">';
$output .= $img;
$output .= '</div>';
}
$output .= '<div class="hex hex-mid"></div>';
$output .= '</div>';
echo $output;
}

If in Foreach Loop

I followed a small tutorial and created a chat for a website. I added some features to it, and now individual chats can be saved but I'm not sure how to place an if statement inside the foreach loop. I would like to do something like this: if($chat_message->smgs_id != NULL) { something here } instead of <a class="save" href="#" id="'.$chat_message->id.'">Save</a>
$chat_messages_html = '<ul style="margin:10px;">';
foreach ($chat_messages->result() as $chat_message)
{
$chat_messages_html .= '<li>' . $chat_message->smsg_id . '<a class="save" href="#" id="'.$chat_message->id.'">Save</a> | </li>';
}
$chat_messages_html .= '</ul>';
Ternary for the win!
$html = '<ul>';
foreach ($arr as $val)
{
$html .= '<li>'. (NULL !== $val ? 'something' : 'something else') .'</li>';
}
$html .= '</ul>';
Split the assignment into parts so you can fit in the condition.
$chat_messages_html = '<ul style="margin:10px;">';
foreach ($chat_messages->result() as $chat_message)
{
$chat_messages_html .= '<li>';
if ($chat_message->smgs_id != NULL) {
$chat_messages_html .= '<a class="save" href="#" id="' . $chat_message->id . '">Save</a> | ';
}
$chat_messages_html .= '</li>';
}
$chat_messages_html .= '</ul>';
Try this:
$chat_messages_html = '<ul style="margin:10px;">';
foreach ($chat_messages->result() as $chat_message)
{
if ($chat_message->smgs_id != NULL) {
$chat_messages_html .= '<li>' . $chat_message->smsg_id . '<a class="save" href="#" id="'.$chat_message->id.'">Save</a> | </li>';
}
}
$chat_messages_html .= '</ul>';

Categories