issue with generating links from database in PHP - php

First of all, I will try and keep this as simple as possible.
This is part of a larger script that generates a breadcrumb link list depending on which category the user is currently in on the page.
Something like:
Hats > Mens > Green
Each of the categories also have their corresponding links so the user can click back on it.
The problem I'm having is that for some reason, I'm getting an additional link inserted that is the current category, right before the top category.
For example, the output, should be:
<b>Category:</b>
Hats >
Mens >
Green
But I am getting this:
<b>Category:</b>
Hats >
<a href="categories.php?parent_id=175828&name=Green&keywords_search=" title="">
Mens >
Green
</a>
Note that the difference is that the current category link (but without the name) gets inserted almost at the very top again, in addition to an additional link closing tag at the very end as well.
What could be the problem? I've spent nearly 6 hours on this and can't figure it out, so I'd really appreciate it if someone could tell me what is wrong. Maybe I spent too much looking at this and am "stuck inside the box" :)
function category_navigator ($parent_id, $show_links = true, $show_category = true, $page_link = null, $additional_vars = null, $none_msg = null, $reverse_categories = false)
{
global $reverse_category_lang, $category_lang, $db;
(string) $display_output = NULL;
(int) $counter = 0;
$none_msg = ($none_msg) ? $none_msg : GMSG_ALL_CATEGORIES;
$page_link = ($page_link) ? $page_link : $_SERVER['PHP_SELF'];
if($parent_id > 0)
{
$root_id = $parent_id;
while ($root_id > 0)
{
$row_category = $db->get_sql_row("SELECT category_id, name, parent_id, hover_title FROM
" . DB_PREFIX . (($reverse_categories) ? 'reverse_categories' : 'categories') . " WHERE
category_id=" . $root_id . " LIMIT 0,1");
if($counter == 0)
{
$display_output = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$display_output = (!empty($display_output)) ? $display_output : $row_category['name'];
$ace = $row_category['category_id'];
$acename = $row_category['name'];
}
else if($parent_id != $root_id)
{
$hover = $db->get_sql_field("SELECT hover_title FROM " . DB_PREFIX . "categories WHERE category_id='".$ace."'","hover_title");
$category_name = ($reverse_categories) ? $reverse_category_lang[$row_category['category_id']] : $category_lang[$row_category['category_id']];
$category_name = (!empty($category_name)) ? $category_name : $row_category['name'];
$display_output = (($show_links) ? '<a href="' . $page_link . '?parent_id=' . $row_category['category_id']
. '&name=' . $category_name . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$row_category['hover_title'].'">' : '') . $category_name . (($show_links) ? '</a>' : '</a>')
. ' > <a href="'. $page_link . '?parent_id=' . $ace . '&name=' . $acename . ((!empty($additional_vars)) ? ('&' . $additional_vars) : '')
. '" title="'.$hover.'">' . $display_output . '</a>';
}
}
$counter++;
$root_id = $row_category['parent_id'];
}
$display_output = (($show_links && $show_category) ? '<b>Category:</b>' : '') . $display_output;
}
$display_output = (empty($display_output)) ? $none_msg : $display_output;
return $display_output;
}

Related

Display a line in color with PHP

I would like to ask you how to display a line (which belongs to a plugin datagrid) in color according to a specific condition.
I have made that :
$data = $mysqli->query($sqlf);
while($dnseuil = $data->fetch_array(MYSQLI_ASSOC)) {
$color = $dnseuil["st"]<=$dnseuil["tp"] ? '#FF0000' : '';
$classe = $dnseuil["st"]<=$dnseuil["tp"] ? '#FFFFFF' : '';
echo '<td bgcolor="' . $color . '"
style = "background-color:' . $color . ';"
class = "'.$classe.'"
data-st = "'.$dnseuil["st"].'"
data-tp = "'.$dnseuil["tp"].'">';
}
My SQL request works and the problem is that when the quantity of st is less than the quantity of tp, I would like to display the concerned line in red.
Do you have solutions ?
Thanks !

Simple Breadcrumb Issue

I've been trying to figure out how to make the last item in my breadcrumb have H1 tags in it. Here is a sample code without the H1.
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
echo '
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
echo '
</li>';
I'm not a PHPer, and I've tried experimenting with it code on this line:
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"' : '', '>';
Because it shows the "last" only on the last item of the breadcrumb.
I tired this below, but obviously it didn't work:
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
echo '
<li', ($link_num == count($context['lateral_navigation']) - 1) ? ' class="last"><h1>' : '', '>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
<div id="arrow">
<div class="inside"></div>
<div class="border"></div>
</div>
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
// Don't show a separator for the last one.
if ($link_num != count($context['lateral_navigation']) - 1)
echo '';
echo '
</h1', ($link_num == count($context['lateral_navigation']) - 1) , '></li>';
}
Any help with this?
Try this:
$count = count($context['lateral_navigation']); // get count
// Each tree item has a URL and name. Some may have extra_before and extra_after.
foreach ($context['lateral_navigation'] as $link_num => $tree)
{
$count--; // decrement by 1
echo ($count == 0) // if zero (last)
? '<li class="last"><h1>'
: '<li>';
// Show something before the link?
if (isset($tree['extra_before']))
echo $tree['extra_before'];
// Show the link, including a URL if it should have one.
echo $settings['lateral_navigation_link'] && isset($tree['url']) ? '
<a href="' . $tree['url'] . '">' . $tree['name'] . '
<div id="arrow">
<div class="inside"></div>
<div class="border"></div>
</div>
</a>' : '' . $tree['name'] . '';
// Show something after the link...?
if (isset($tree['extra_after']))
echo $tree['extra_after'];
// Don't show a separator for the last one.
if ($link_num != count($context['lateral_navigation']) - 1)
echo '';
echo ($count == 0) // if zero (last)
? '</h1></li>'
: '</li>';
}

PHP/WordPress: override page_id in foreach loop

I have a WordPress site and currently the code it set to create titles based on the page title. The titles are: Interior House Painting, Exterior House Painting and Commercial Painting. I would like to override the titles to remove the word "house". This is the code currently:
<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
$page_post = get_post($page_id);
$page_custom_key = 'home_page_info';
$page_link = $page_post->post_name;
$li_class = $page_id == 36 ? 'noMargin' : '';
$title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
$title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
$title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";
echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
echo '<h2>' . $title . '</h2>';
echo get_post_meta($page_id, $page_custom_key, true);
echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}
?>
The output is this: Interior House Painting, Exterior Painting, Commercial Painting. How do I get "house" removed from "Interior House Painting"?
$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));
Though, I am a little curious as to why you would not prefer to just remove the word from the title altogether.
ADDITIONALLY: Somebody else mentioned using preg_replace. That's also entirely possible, but it would be best to do a case-insensitive search:
$title = trim(preg_replace('/house/i', '', $page_post->post_title));
Just use preg_replace('House'|| 'house','',/*Variable Page title is in*/);
You could try replacing the line:
echo '<h2>' . $title . '</h2>';
With something like this:
if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; }
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; }
else { echo '<h2>' . $title. '</h2>'; }

variable inside variable in a PHP function?

I have the following function.
The problem is that the $lang variable will vary depending on the $this->setts['site_lang']; . The actual problem is the following:
$condition_details['description_$lang']
(which isn't working). How do I get this to display
$condition_details['description_us']
or
$condition_details['description_fr']
depending on the $lang setting?
And here is the full function:
function shipped_drop_down($box_name = 'item_shipped_via', $selected = null, $form_refresh = null)
{
(string) $display_output = null;
$lang = $this->setts['site_lang'];
$sql_select_conditions = $this->query("SELECT id, description_".$lang." FROM " . DB_PREFIX . "advanced_shipping_carriers ORDER BY theorder,description_".$lang." ASC");
$display_output = '<select name="' . $box_name . '" ' . (($form_refresh) ? 'onChange = "submit_form(' . $form_refresh . ', \'\')"' : '') . ' style="font-size:10px; width: 120px;"> ';
while ($condition_details = $this->fetch_array($sql_select_conditions))
{
$display_output .= '<option value="' . $condition_details['id'] . '" ' . (($condition_details['id'] == $selected) ? 'selected' : '') . '>' . $condition_details['description_$lang'] . '</option> ';
}
$display_output .= '</select> ';
return $display_output;
}
You could do
$condition_details['description_'.$lang]
or use "
$condition_details["description_$lang"]
You could store your lang settings in arrays like this :
$lang['fr']['condition_details']
so you could use $lang[$selected_lang]['condition_details']
Use "double quotes" instead of 'single quotes'. That way the $lang will be parsed and replaced with the relevant value.
Use double quotes like so:
$condition_details["description_$lang"]

PHP Dynamic Count & Limit Menu items

I want to modify this code which works pretty good but (or I don't know because I'm new with php) I can't limit the number of li's displayed for the main elements in the menu. The actual code will echo all elements it finds, I want to limit the times
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
this line is echoed.. let's say to echo just the first 15 elements + a "MORE" button under which to display the rest of the elements as sub-menus.. (this is a 2 level horizontal menu). Can someone please help me? I really tried a lot but I'm not an expert in PHP..
Thanks!
<?php
require_once( '../../../inc/header.inc.php' );
require_once( DIRECTORY_PATH_INC . 'membership_levels.inc.php' );
require_once( DIRECTORY_PATH_ROOT . "templates/tmpl_{$tmpl}/scripts/TemplMenu.php" );
class SimpleMenu extends TemplMenu
{
function getCode()
{
$this->iElementsCntInLine = 100;
$this->getMenuInfo();
$this->genTopItems();
return $this->sCode;
}
function genTopItem($sText, $sLink, $sTarget, $sOnclick, $bActive, $iItemID, $isBold = false, $sPicture = '')
{
$sActiveStyle = ($bActive) ? ' id="tm_active"' : '';
if (!$bActive) {
$sAlt= $sOnclick ? ( ' alt="' . $sOnclick . '"' ) : '';
$sTarget = $sTarget ? ( ' target="_parent"' ) : '';
}
$sLink = (strpos($sLink, 'http://') === false && !strlen($sOnclick)) ? $this->sSiteUrl . $sLink : $sLink;
$sSubMenu = $this->getAllSubMenus($iItemID);
$sImgTabStyle = $sPictureRep = '';
if ($isBold && $sPicture != '') {
$sPicturePath = getTemplateIcon($sPicture);
$sPictureRep = "<img src='{$sPicturePath}' style='vertical-align:middle;width:16px;height:16px;' />";
$sText = ' ';
$sImgTabStyle = 'style="width:38px;"';
}
$sMainSubs = ($sSubMenu=='') ? '' : " {$sSubMenu} </a>";
$this->sCode .= "
<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>
<div id='submenu'>
<ul>
<li>{$sMainSubs}</li>
</ul>
</div>
</li>
";
}
}
$objMenu = new SimpleMenu();
echo "<ul id='ddmenu'>";
echo $objMenu->getCode();
echo "</ul>";
?>
It's difficult to tell exactly where you want to do this in your code, but I figure you're looking for something like:
<?php
for ($i = 0; i < 15; i ++){
echo "<li><a href='{$sLink}' {$sOnclick} target='_parent'>{$sPictureRep}{$sText}</a>"
}
echo "<li><a href='more' target='_parent'>More...</a>"
?>

Categories