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>';
Related
I would like to repeat the following code 15 times or more on a single page:
$html .= '<select name="select_product_category"
class="dropdown_list"><option value="0">All
categories</option>';
foreach($categories as $value) {
$html .= '<option value="' . $value['category_id'] .
'"';
if($value['category_id'] === $active_category_id)
$html .= ' checked';
$html .= '>' . $value['category_name'] . '</option>';
}
$html .= '</select>';
Is there a better way to achieve this than just repeat this particular block of code? Thanks for your support.
Try to use functions as suggested by #Don't Panic
<?php
function generateSelectBoxHTML()
{
$html = '<select name="select_product_category" class="dropdown_list"><option value="0">All categories</option>';
foreach ($categories as $category) {
$checked = '';
if ($category['category_id'] === $activeCategoryId) {
$checked = ' checked="checked"';
}
$html .= sprintf('<option value="%s"%s>%s</option>', $category['category_id'], $checked, $category['category_name']);
}
$html .= '</select>';
return $html;
}
$html .= generateSelectBoxHTML();
// ...
$html .= generateSelectBoxHTML();
$html .= generateSelectBoxHTML();
$html .= generateSelectBoxHTML();
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.
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
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.
This is my first shot at creating a combination filter based on David DeSandro's Isotope. I created this function based on an already existing implementation of the Isotope filter on my Website. What it does is outputs WordPress categories and implements them as Isotope filters. As of right now everything works very well. What I am trying to do is style each if elseif statement into its own column after it extracts the data. For example:
**<div class="column"><--I would like to add a div like this here**
elseif (cat_is_ancestor_of(156, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
**</div><--and here**
Below is the beginning of my function that I have written to accomplish this. As of now it works, but it outputs the data (in this case categories & child categories) in a neat single column list. I would ultimately like to style this list of categories so that each parent is in it's own column.
This is where I am running into difficulty as I can't seem to find a way to separate the if, elseif statements with div's without getting this error:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in /Users/djmorigeau/localhost/wordpress/wp-content/themes/skittles-child/functions.php on line 89. This is the error that I receive when I wrap each if, elseif statement with <div class="col_3">code</div> Any ideas on how I can group the results into columns?
function color_filter($categories, $type = 'blog') {
$catArgs = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'category',
'pad_counts' => false );
$catList = get_categories($catArgs);
if(isset($categories[0])) {
$categories = unserialize($categories[0]);
if (function_exists('color_filter')) {
$option = '<div class="megamenu_container megamenu_dark_bar megamenu_dark"><ul class="megamenu">';
$option .= '<li><a class="megamenu_drop" href="#">Filter<em class="dropdown-arrow"></em></a>';
$option .= '<div class="dropdown_fullwidth"><div class="mpcth-'.$type.'-categories mpcth-filterable-categories">';
$option .= '<ul>';
$option .= '<li class="active" data-link="post">'.__('All', 'mpcth').'</li>';
foreach ($catList as $filtered) {
if((isset($categories[$filtered->slug]) && $categories[$filtered->slug] == 'on') || !isset($categories[$filtered->slug])) {
if (cat_is_ancestor_of(37, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(156, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(176, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
elseif (cat_is_ancestor_of(188, $filtered)) {
$option .= '<li class="child-style" data-link="'.$filtered->slug.'">';
$option .= '<a href="" title="'.$filtered->slug.'">';
$option .= ' ';
$option .= $filtered->name;
$option .= '</a></li>';
}
else {
$option .= '<li data-link="'.$filtered->slug.'">';
$option .= ''.$filtered->name.'</li>';
}
}
}
$option .= '</ul></div></div></li></ul></div>';
echo $option;
}
}
}
I think you mean this:
elseif(...) {
$option .= '<div class="column">';
// your code
$option .= '</div>';
}
or you mean this:
$done = false;
$option .= '<div class="column">';
if (cat_is_ancestor_of(37, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(156, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(176, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done && cat_is_ancestor_of(188, $filtered)) {
// your code
$done = true;
}
$option .= '</div>';
$option .= '<div class="column">';
if (!$done) {
// your code
}
$option .= '</div>';