As per usual I have spent the past few hours on this but cannot work out a better way. I have a CMS and need to create a sitemap. I have a function of the works but is messy and not really fit for purpose and was wondering if anyone had any suggestions. I under there are for loops and the likes but I'm not all that use how to use them yet. I have included the function I already have as well as a demo of the database.
tbl_website_posts
| ID | Title | Parent ID |
|-----|-------------------|-----------|
| 1 | Parent 1 | 0 |
| 2 | Parent 2 | 0 |
| 3 | Child of 2 | 2 |
Functions.php
function generate_sitemap($site_id) {
global $dbcon;
$return = '<ul>';
$icon = array('page' => 'fa-file-word', 'post' => 'fa-newspaper', 'event' => 'fa-calendar-star', 'calander' => 'fa-calendar-alt', 'training' => 'fa-award', 'people' => 'fa-user-alt', 'group' => 'fa-users-class');
$query_1 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='0'");
while ($array_1 = mysqli_fetch_array($query_1, MYSQLI_ASSOC)) {
$parent_id_1 = $array_1['id'];
$query_2 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_1'");
if (mysqli_num_rows($query_2) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_1['post_type']] . '"}\'>' . $array_1['post_title'];
$return .= '<ul>';
while ($array_2 = mysqli_fetch_array($query_2, MYSQLI_ASSOC)) {
$parent_id_2 = $array_2['id'];
$query_3 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_2'");
if (mysqli_num_rows($query_3) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_2['post_type']] . '"}\'>' . $array_2['post_title'];
$return .= '<ul>';
while ($array_3 = mysqli_fetch_array($query_3, MYSQLI_ASSOC)) {
$parent_id_3 = $array_3['id'];
$query_4 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_3'");
if (mysqli_num_rows($query_4) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_3['post_type']] . '"}\'>' . $array_3['post_title'];
$return .= '<ul>';
while ($array_4 = mysqli_fetch_array($query_4, MYSQLI_ASSOC)) {
$parent_id_4 = $array_4['id'];
$query_5 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_4'");
if (mysqli_num_rows($query_5) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_4['post_type']] . '"}\'>' . $array_4['post_title'];
$return .= '<ul>';
while ($array_5 = mysqli_fetch_array($query_5, MYSQLI_ASSOC)) {
$parent_id_5 = $array_5['id'];
$query_6 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_5'");
if (mysqli_num_rows($query_6) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_5['post_type']] . '"}\'>' . $array_5['post_title'];
$return .= '<ul>';
while ($array_6 = mysqli_fetch_array($query_6, MYSQLI_ASSOC)) {
$parent_id_6 = $array_6['id'];
$query_7 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_6'");
if (mysqli_num_rows($query_7) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_6['post_type']] . '"}\'>' . $array_6['post_title'];
$return .= '<ul>';
while ($array_7 = mysqli_fetch_array($query_7, MYSQLI_ASSOC)) {
$parent_id_7 = $array_7['id'];
$query_8 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_7'");
if (mysqli_num_rows($query_8) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_7['post_type']] . '"}\'>' . $array_7['post_title'];
$return .= '<ul>';
while ($array_8 = mysqli_fetch_array($query_8, MYSQLI_ASSOC)) {
$parent_id_8 = $array_8['id'];
$query_9 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_8'");
if (mysqli_num_rows($query_9) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_8['post_type']] . '"}\'>' . $array_8['post_title'];
$return .= '<ul>';
while ($array_9 = mysqli_fetch_array($query_9, MYSQLI_ASSOC)) {
$parent_id_9 = $array_9['id'];
$query_10 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_9'");
if (mysqli_num_rows($query_10) > 0) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_9['post_type']] . '"}\'>' . $array_9['post_title'];
$return .= '<ul>';
while ($array_10 = mysqli_fetch_array($query_10, MYSQLI_ASSOC)) {
$parent_id_10 = $array_10['id'];
$query_11 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_10'");
if (mysqli_num_rows($query_11) > 0) {
$return .= '<li>' . $array_10['post_title'];
$return .= '<ul>';
while ($array_11 = mysqli_fetch_array($query_11, MYSQLI_ASSOC)) {
$parent_id_11 = $array_11['id'];
$query_12 = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id_11'");
if (mysqli_num_rows($query_12) > 0) {
$return .= '<li>' . $array_11['post_title'];
$return .= '<ul>';
while ($array_12 = mysqli_fetch_array($query_12, MYSQLI_ASSOC)) {
$return .= '<li>' . $array_12['post_title'] . '</li>';
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_11['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_10['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li>' . $array_9['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_8['post_type']] . '"}\'>' . $array_8['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_7['post_type']] . '"}\'>' . $array_7['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_6['post_type']] . '"}\'>' . $array_6['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_5['post_type']] . '"}\'>' . $array_5['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_4['post_type']] . '"}\'>' . $array_4['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_3['post_type']] . '"}\'>' . $array_3['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_2['post_type']] . '"}\'>' . $array_2['post_title'] . '</li>';
}
}
$return .= '</ul>';
$return .= '</li>';
} else {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array_1['post_type']] . '"}\'>' . $array_1['post_title'] . '</li>';
}
}
$return .= '</ul>';
return $return;
}
The function is also very limited to only going so many children in. If anyone has and suggestions on where I could start that would be amazing. Thank you in advance for your time.
Alan.
I'd use a recursive function for this.
Disclaimer: this is untested code I wrote in the StackOverflow editor, but it looks legit to me. YMMV, use it mostly for inspiration :-)
function generate_sitemap($site_id, $parent_id=0) {
global $dbcon;
$return = '';
$query = mysqli_query($dbcon, "SELECT * FROM tbl_website_posts WHERE post_website='$site_id' AND post_parent='$parent_id'");
// side note: once you get this working it will be worth the effort to turn
// this query into a prepared statement. Using variables in your query like
// this puts you at risk of a SQL injection attack.
if (mysql_num_rows($query) > 0) {
$return = '<ul>';
$icon = array('page' => 'fa-file-word', 'post' => 'fa-newspaper', 'event' => 'fa-calendar-star', 'calander' => 'fa-calendar-alt', 'training' => 'fa-award', 'people' => 'fa-user-alt', 'group' => 'fa-users-class');
while ($array = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$return .= '<li data-jstree=\'{"icon":"fal ' . $icon[$array['post_type']] . '"}\'>' . $array['post_title'];
$return .= generate_sitemap($site_id, $array['id']);
$return .= '</li>';
}
$return .= '</ul>';
}
return $return;
}
So basically, start out assuming the parent won't have any child pages (initial $return value is an empty string, so we don't return an empty <ul></ul>).
Only if there are children, start building the <ul> with all child pages.
Inside the <li> for a child page, have the function call itself with the child's id as the new parent id. If the child has no child pages, the nested function call will return an empty string. Otherwise, it will return a <ul> with all of its children.
Related
i have this code:
$Maxcount = 10;$count = 1;$query = "SELECT pages.paid,
pages.pag_desc,
pages.isActive,
pages.pag_pag_id,
pages.pag_type,
parent.pag_desc,";while ($count <= $Maxcount) {$query .= "parent".$count.".pag_desc AS parent".$count;if ($count != $Maxcount) {$query .= ", ";$count++;} $query .= " FROM pages ";$query .= "LEFT JOIN pages parent ON pages.pag_pag_id = parent.paid ";$count = 1;while ($count <= $Maxcount) { if ($count === 1) {
$query .= "LEFT JOIN pages parent".$count." ON parent.pag_pag_id = parent".$count.".paid "; }else {
$query .= "LEFT JOIN pages parent".$count." ON parent".($count-1).".pag_pag_id = parent".$count.".paid " }$count++;}$query .="LEFT JOIN tblgroups_rights_gri on pages.paid = tblgroups_rights_gri.gri_pag_id
INNER JOIN users_usergroups on tblgroups_rights_gri.gri_usrg_id = users_usergroups.gid
WHERE uid = '$uid' AND $where AND pages.isActive=1";
$result = $db->query($query);
if (mysqli_num_rows($result)) {
$menu = array();
while ($row = $result->fetch_assoc()) {
$menu[] = $row;
}
}
and for showing the menu i used this recursive function :
foreach($menu as $row){
$x[$row['parent3']][$row['parent2']][$row['parent1']][$row['pag_desc']]=$row['pag_desc'];
}function recursive($x, $out) { if (is_array($x)){
//$out .= "<ul>";
foreach($x as $i => $data) {
if ($i === '') {
$out = recursive($data, $out);
continue;
}
$out .= "<li>";
if (is_array($data)){
$out .= ' <a href="#" class="dropdown-toggle" data-toggle="dropdown">';
}
else
{
$out .= '<a href="#">';
}
$out .= $i;
if (is_array($data)){
$out .= '<b class="caret"></b></a>';
}
else
{
$out .= "</a>";
}
if (is_array($data)){
$out .= "<ul class='dropdown-menu'>";
$out = recursive($data, $out);
$out .= "</ul></li>";
}
else
{
$out .= "</li>";
}
}
}
return $out;
}
the result is that its showing only the parent and the first child but in database the child have another child etc...
for example:
parent user have a child admin and admin have another child called editor
but its only showing user as a parent and admin as a child
any help please? thank u in advance
i installed a Joomla template and some errors are appearing in homepage, the Errors are related to the Sp Page Builder component.
How can i fix these errors?
Here's a document with the erros - https://drive.google.com/open?id=0B1toGflgmV7fZi1SQ051QWZxZFE
And the site.php
<?php
/**
* Flex 1.0 #package SP Page Builder
* Template Name - Flex
* #author Aplikko http://www.aplikko.com
* #copyright Copyright (c) 2015 Aplikko
* #license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/
// no direct access
defined('_JEXEC') or die;
JLoader::register('JHtmlString', JPATH_LIBRARIES.'/joomla/html/html/string.php');
AddonParser::addAddon('sp_latest_posts','sp_latest_posts_addon');
function get_categories($parent=1) {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('*')
->from($db->quoteName('#__categories'))
->where($db->quoteName('extension') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('published') . ' = ' . $db->quote(1))
->where($db->quoteName('parent_id') . ' = ' . $db->quote($parent))
->order($db->quoteName('created_time') . ' DESC');
$db->setQuery($query);
$cats = $db->loadObjectList();
$categories = array($parent);
foreach ($cats as $key => $cat) {
$categories[] = $cat->id;
}
return $categories;
}
function sp_latest_posts_addon($atts){
extract(spAddonAtts(array(
"title" => '',
"heading_selector" => 'h3',
"title_fontsize" => '',
"title_text_color" => '',
"title_margin_top" => '',
"title_margin_bottom" => '',
"show_image" => '',
"show_date" => '',
"show_category" => '',
"show_intro_text" => '',
"show_author" => '',
"item_limit" => '',
"intro_text_limit" => '100',
"column_no" => '3',
"image_alignment" => '',
"category" => '',
"style" => '',
"class" => '',
), $atts));
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
// Database Query
require_once JPATH_SITE . '/components/com_content/helpers/route.php';
// Access filter
$access = !JComponentHelper::getParams('com_content')->get('show_noauth');
$authorised = JAccess::getAuthorisedViewLevels(JFactory::getUser()->get('id'));
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select('a.*')
->from($db->quoteName('#__content', 'a'))
->select($db->quoteName('b.alias', 'category_alias'))
->select($db->quoteName('b.title', 'category'))
->join('LEFT', $db->quoteName('#__categories', 'b') . ' ON (' . $db->quoteName('a.catid') . ' = ' . $db->quoteName('b.id') . ')')
->where($db->quoteName('b.extension') . ' = ' . $db->quote('com_content'))
->where($db->quoteName('a.state') . ' = ' . $db->quote(1))
->where($db->quoteName('a.catid')." IN (" . implode( ',', get_categories($category) ) . ")")
->where($db->quoteName('a.access')." IN (" . implode( ',', $authorised ) . ")")
->order($db->quoteName('a.created') . ' DESC')
->setLimit($item_limit);
$db->setQuery($query);
$items = $db->loadObjectList();
// End Database Query
$style == 'flex' ? $flex_style = ' flex' : '';
$style == 'blog' ? $blog_style = ' blog' : '';
$blog_style = $output = '<div class="sppb-addon sppb-addon-latest-posts'.$flex_style.$blog_style.' sppb-row ' . $class . '">';
if ($title) {
$output .= '<div class="sppb-section-title">';
$output .= '<'.$heading_selector.' class="sppb-addon-title" style="' . $title_style . '"> ' . $title . '</' . $heading_selector . '>';
$output .= '</div>'; // END :: title
}
$output .= '<div class="sppb-addon-content">';
$output .= '<div class="latest-posts clearfix">';
foreach(array_chunk($items, $column_no) as $items) {
$output .= '<div>';
foreach ($items as $item) {
$item->slug = $item->id . ':' . $item->alias;
$item->catslug = $item->catid . ':' . $item->category_alias;
$item->user = JFactory::getUser($item->created_by)->name;
if ($access || in_array($item->access, $authorised)) {
// We know that user has the privilege to view the article
$item->link = JRoute::_(ContentHelperRoute::getArticleRoute($item->slug, $item->catid, $item->language));
$item->catlink = JRoute::_(ContentHelperRoute::getCategoryRoute($item->catslug, $item->catid, $item->language));
} else {
$item->link = JRoute::_('index.php?option=com_users&view=login');
$item->catlink = JRoute::_('index.php?option=com_users&view=login');
}
$tplParams = JFactory::getApplication()->getTemplate(true)->params;
$params = $item->params;
$attribs = json_decode($item->attribs);
$images = json_decode($item->images);
$imgsize = $tplParams->get('blog_list_image', 'default');
$intro_image = '';
if(isset($attribs->spfeatured_image) && $attribs->spfeatured_image != '') {
if($imgsize == 'default') {
$intro_image = $attribs->spfeatured_image;
} else {
$intro_image = $attribs->spfeatured_image;
$basename = basename($intro_image);
$list_image = JPATH_ROOT . '/' . dirname($intro_image) . '/' . JFile::stripExt($basename) . '_'. $imgsize .'.' . JFile::getExt($basename);
if(file_exists($list_image)) {
$intro_image = JURI::root(true) . '/' . dirname($intro_image) . '/' . JFile::stripExt($basename) . '_'. $imgsize .'.' . JFile::getExt($basename);
}
}
} elseif(isset($images->image_intro) && !empty($images->image_intro)) {
$intro_image = $images->image_intro;
}
if($column_no == '1') {
if ($show_image) {
$image_alignment == 'left' ? $img_column = 'sppb-col-sm-4 column-1 pull-left match-height' : $img_column = 'sppb-col-sm-4 column-1 pull-right match-height';
}
if ($show_image) {
$image_alignment == 'right' ? $content_column = 'sppb-col-sm-8 column-1 pull-left match-height' : $content_column = 'sppb-col-sm-8 column-1 pull-right match-height';
} else {
$image_alignment == 'right' ? $content_column = 'sppb-col-sm-12 column-1' : $content_column = 'sppb-col-sm-12 column-1';
}
$h2style = ' style="font-size:180%;line-height:1.4;"';
$img_wrapper_margin = ' style="margin:0;"';
if ($image_alignment == 'left') {
$inner_padding = ' style="padding:0 0 0 30px;"';
} else {
$inner_padding = ' style="padding:0 30px 0 0;"';
}
}
// match-height
$column_no > '1' ? $match_height = ' match-height' : '';
// Flex Style
if($style == 'flex') {
$output .= '<div class="latest-post sppb-col-sm-' . round(12/$column_no) . ' columns-'.$column_no.'">';
$output .= '<div class="latest-post-item">';
if($column_no == '1') {
$output .= '<div class="row-fluid">';
}
if(!empty($intro_image) || (isset($images->image_intro) && !empty($images->image_intro))) {
if ($show_image) {
if($column_no == '1') {
$output .= '<div style="padding:0" class="'.$img_column.'">';
}
$output .= '<div class="img-wrapper">';
$output .= '<img class="post-img" src="' . $intro_image . '" alt="' . $item->title . '" /><div class="caption-content">' . $item->title . '<em class="caption-category"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_IN') .'</span>'. $item->category . '</em></div>';
$output .= '</div>';
if($column_no == '1') {
$output .= '</div>';
}
}
}
if($column_no == '1') {
$output .= '<div'.$inner_padding.' class="'.$content_column.'">';
}
$output .= '<div class="latest-post-inner match-height">';
if (($show_date || $show_intro_text || $show_author) != 1) {
$output .= '<h2 style="margin:0" class="entry-title">' . $item->title . '</h2>';
} else {
$output .= '<h2'.$h2style.' class="entry-title">' . $item->title . '</h2>';
}
if ($show_date) {
$output .= '<div class="entry-meta"><span class="entry-date">' . JHtml::_('date', $item->created, 'DATE_FORMAT_LC1') . '</span></div>';
}
if ($show_intro_text) {
$output .= '<p class="intro-text" >' . JHtml::_('string.truncate', strip_tags($item->introtext), $intro_text_limit) . '</p>';
}
$show_author || $show_category ? $output .= '<hr />' : '';
if ($show_author) {
$output .= '<span class="post-author"><span class="entry-author">' . JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_BY'). '</span> ' . $item->user . '</span>';
}
if ($show_category) {
$show_author ? $posted_in_category = ' cat-inline' : '';
$output .= '<span class="category'.$posted_in_category.'"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_CATEGORY') .'</span>'. $item->category . '</span>';
}
if($column_no == '1') {
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
if($column_no == '1') {
$output .= '<div class="post-divider"></div>';
}
$output .= '</div>';
// Default & Blog styles
} else {
$output .= '<div class="latest-post sppb-col-sm-' . round(12/$column_no) . ' columns-'.$column_no.'">';
$output .= '<div class="latest-post-inner' . $match_height . '">';
if($column_no == '1') {
$output .= '<div class="row-fluid">';
}
if ($show_image) {
if($column_no == '1') {
$output .= '<div class="'.$img_column.'">';
}
$output .= '<div'.$img_wrapper_margin.' class="img-wrapper">';
$output .= '<img class="post-img" src="' . $intro_image . '" alt="' . $item->title . '" />';
$output .= '</div>';
if($column_no == '1') {
$output .= '</div>';
}
}
if($column_no == '1') {
$output .= '<div class="'.$content_column.'">';
}
if ($show_date) {
$output .= '<div class="entry-meta"><span class="entry-date"> ' . JHtml::_('date', $item->created, 'DATE_FORMAT_LC1') . '</span></div>';
}
$output .= '<h2'.$h2style.' class="entry-title">' . $item->title . '</h2>';
if ($show_intro_text) {
$output .= '<p class="intro-text" >' . JHtml::_('string.truncate', strip_tags($item->introtext), $intro_text_limit) . '</p>';
}
$show_author || $show_category ? $output .= '<hr />' : '';
if ($show_author) {
$output .= '<span class="post-author"><span class="entry-author">' . JText::_('COM_SPPAGEBUILDER_ADDON_POSTED_BY'). ' ' . $item->user . '</span></span>';
}
if ($show_category) {
$show_author ? $posted_in_category = ' cat-inline' : '';
$output .= '<span class="category'.$posted_in_category.'"><span class="posted-in">'. JText::_('COM_SPPAGEBUILDER_ADDON_CATEGORY') .'</span>'. $item->category . '</span>';
}
if($column_no == '1') {
$output .= '</div>';
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
}
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$column_no == '1' ? $column_no_1 = '.column-1 {margin:10px auto;padding:0!important;}' : '';
// Add styles #media rulepost-img
if($style == 'flex') {
$custom_style = ''
. '#media screen and (max-width: 768px) {'
. $column_no_1
. '.img-wrapper a {font-size:150%;line-height:1.5;}'
. '}';
$doc->addStyleDeclaration($custom_style);
}
if ($column_no>='3') {
$custom_style_3 = ''
. '#media screen and (min-width: 992px) and (max-width: 1199px){'
. '.columns-'.$column_no.'{width:33.3333%;}'
. '}'
. '#media screen and (min-width: 768px) and (max-width: 991px){'
. '.columns-'.$column_no.'{width:50%}'
. '}';
$doc->addStyleDeclaration($custom_style_3);
}
if($column_no=='5') {
$custom_style_5 = ''
. '.columns-'.$column_no.' {width:20%}'
. '#media screen and (min-width: 992px) and (max-width: 1199px){'
. '.columns-'.$column_no.'{width:33.3333%;}'
. '}'
. '#media screen and (min-width: 768px) and (max-width: 991px){'
. '.columns-'.$column_no.'{width:50%}'
. '}'
. '#media screen and (max-width: 767px){'
. '.columns-'.$column_no.'{width:100%}'
. '}';
$doc->addStyleDeclaration($custom_style_5);
}
return $output;
}
Thanks!
There is many errors, but all of them are variable that have not been declared before using it, as example :
$style == 'flex' ? $flex_style = ' flex' : '';
$style == 'blog' ? $blog_style = ' blog' : '';
$blog_style = $output = '<div class="sppb-addon sppb-addon-latest-posts'.$flex_style.$blog_style.' sppb-row ' . $class . '">';
In this case $flex_style and $blog_style are not declared, you should write this instead :
$flex_style = style == 'flex' ? ' flex' : '';
$blog_style = $style == 'blog' ? ' blog' : '';
That is just an example, but if you search a little you'll find other issue like this one.
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.
I what to put a span element for $term['nodes']
I have tried to put after bracket and between but nothing works for me
if (isset($term['nodes'])) {
$term['name'] = $term['name'] . ' (' . $term['nodes'] . ')';
}
here is the all functin
function bootstrap_taxonomy_menu_block($variables) {
$tree = $variables['items'];
$config = $variables['config'];
$num_items = count($tree);
$i = 0;
$output = '<ul class="nav nav-pills nav-stacked">';
foreach ($tree as $tid => $term) {
$i++;
// Add classes.
$attributes = array();
if ($i == 1) {
$attributes['class'][] = '';
}
if ($i == $num_items) {
$attributes['class'][] = '';
}
if ($term['active_trail'] == '1') {
$attributes['class'][] = 'active-trail';
}
if ($term['active_trail'] == '2') {
$attributes['class'][] = 'active';
}
// Alter link text if we have to display the nodes attached.
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
}
// Set alias option to true so we don't have to query for the alias every
// time, as this is cached anyway.
$output .= '<li' . drupal_attributes($attributes) . '>' . l($term['name'], $term['path'], $options = array('alias' => TRUE));
if (!empty($term['children'])) {
$output .= theme('taxonomy_menu_block__' . $config['delta'], (array('items' => $term['children'], 'config' => $config)));
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
i what this for the bootstrap cdn class , i have move the function on template.php , of drupal theme , but the span element is in plain text in browser
Try this:
if (isset($term['nodes']))
{
$term['name'] = $term['name'] . ' (<span>' . $term['nodes'] . '</span>)';
echo $term['name']; // To see the output
}
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;
}