I would like my breadcrumb script to display the page name only(example: pic), instead of page name + query string if exists(example: pic).
This is my breadcrumb script:
<?php
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
echo '<li class="active">';
echo ucfirst(str_replace(array(".php","_",),array(""," "),$crumb) . ' ');
echo '</li>';
}
?>
How do I exclude everything after ".php"?
Thank you for your help
use parse_url() function for getting the parts of the url
You may use this after some editing
<?php
$parsed=parse_url($_SERVER["REQUEST_URI"]);
$crumbs = explode("/",$parsed['path']);
print_r(parse_url($_SERVER["REQUEST_URI"]));
foreach($crumbs as $crumb){
echo '<li class="active">';
echo ucfirst(str_replace(array(".php","_",),array(""," "),$crumb) . ' ');
echo '</li>';
}
?>
<?php
$crumbs = explode("/",$_SERVER["PHP_SELF"]);
$html="";
foreach($crumbs as $crumb){
$crumb = ucfirst(str_replace(array(".php","_"),array(""," "),$crumb));
if(trim($crumb)!=""){
$html .= '<li class="active">'.$crumb.'</li>';
}
}
$html = "<li>".ucfirst($_SERVER["HTTP_HOST"])."</li>".$html;
echo $html;
?>
Related
I have attached a breadcrumb and is now running. but how to tidy up the results in order to function.
this code
<?php
$crumbs = explode("/",$_SERVER["REQUEST_URI"]);
foreach($crumbs as $crumb){
ucfirst(str_replace(array(".php","_"),array(""," "),$crumb) . ' ');
}
$urls = "http://example.com";
foreach($crumbs as $crumb){
$urls .= "/".$crumb;
echo '<li><a href="'.$urls.'">';
echo $crumb;
echo '</a></li>';
}
?>
and the result is
Home / / pages / contact
How to remove one slash after home?
and result for the url
http://example.com//pages/contact
i've tried to fix but still not solved
Add each 'breadcrumb' to an array in your loop, and then implode the array after the loop, adding the slashes.
$urls = "http://example.com";
$breadcrumbs = array();
foreach($crumbs as $crumb){
$breadcrumbs[] = "<li><a href='$urls/$crumb'>$crumb</a></li>";
}
echo '/'.implode('/',$breadcrumbs);
I got an error saying
Object of class DOMNodeList could not be converted to string on line
This is the line which contains the error:
$output .= '<li><a target="_blank" href="' . $postURL . '">' .
$title->nodeValue . '</a></li>';
DOM
My code:
$postTitle = $xpath->query("//tr/td[#class='row1'][3]//span[1]/text()");
$postURL = $xpath->query("//tr/td[#class='row1'][3]//a/#href");
$output = '<ul>';
foreach ($postTitle as $title) {
$output .= '<li><a target="_blank" href="' . $postURL . '">' . $title->nodeValue . '</a></li>';
}
$output .= '</ul>';
echo $output;
How can I resolve the error?
You're fetching two independent node list from the DOM, iterate the first and try to use the second one inside the loop as a string. A DOMNodeList can not be cast to string (in PHP) so you get an error. Node lists can be cast to string in Xpath however.
You need to iterate the location path that is the same for booth list (//tr/td[#class='row1'][3]) and get the $title and $url inside the loop for each of the td elements.
$posts = $xpath->evaluate("//tr/td[#class='row1'][3]");
$output = '<ul>';
foreach ($posts as $post) {
$title = $xpath->evaluate("string(.//span[1])", $post);
$url = $xpath->evaluate("string(.//a/#href)", $post);
$output .= sprintf(
'<li><a target="_blank" href="%s">%s</a></li>',
htmlspecialchars($url),
htmlspecialchars($title)
);
}
$output .= '</ul>';
echo $output;
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $menu[$key] . '">' . $menu[$key] . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu[1]); ?>
What i basically want to do is to pass a specific array value when i'm echoing out the menu.
I'm building a single page website with anchors and i want to pass value's so i can echo out the "top"-link.
I'm stuck at the point on how to pass the $key value trough the function.
**edit: I'm trying to print specific links. I want a function that is able to print out an link but i want to specify the link to print via the function argument.
for example:
<?php echo main_menu($key = '0'); ?>
result:
prints url: top
<?php echo main_menu($key = '2'); ?>
result:
prints url: photography
**
(A lack of jargon makes it a bit harder to explain and even harder to google.
I got my books in front of me but this is taking a lot more time than it should.)
You either need to pass the entire array and loop, or pass a single array item and not loop:
Single Item:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
$return .= '<a class="menu" href="index.php#' . $menu . '">' . $menu . '</a>' . PHP_EOL .'';
$return .= '</div>';
return $return;
}
echo main_menu($menu[1]);
Entire Array:
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach($menu as $value) {
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
echo main_menu($menu);
You don't need $menu[$key] just use the $value.
Should you not just be using $value inside your loop? And passing the entire array rather than one item of the $menu array?
$menu = array(
0 =>'top',
1 =>'photography',
2 =>'about'
);
<?php
function main_menu ($menu) {
$return = '<div class="menu_entry">' . PHP_EOL .'';
foreach( $menu as $key => $value)
{
$return .= '<a class="menu" href="index.php#' . $value . '">' . $value . '</a>' . PHP_EOL .'';
}
$return .= '</div>';
return $return;
}
?>
<?php echo main_menu($menu); ?>
Try:
echo main_menu($menu); // You will get your links printed
Instead of
echo main_menu($menu[1]); // In this case error is occured like : **Invalid argument supplied for foreach**
NOTE: You can use $value instead of $menu[$key]
I'm trying to take the values of an array and display it as a list. I've used a foreach to do this before but for some reason I can't figure out why it's not working this time around. In the code below, $output is the contents of an entire page generated through AJAX and is later echo'd out on the bottom of the script. Everything seems to be working except this one small section. I managed to get this exact same information to display on a separate static page (without the $output) so I'm not sure why it's not working here.
if(!empty($record['utilities'])) {
$output .= "<ul>";
foreach ($record['utilities'] as $eachUtility):
// $output .= "<li>" echo $eachUtility; "</li>";
$output .= "<li>" . $eachUtility . "</li>";
endforeach;
$output .= "</ul>";
// $output .= $record['utilities']; This works
}
Here's a working example of the same code that was hard coded on a separate page:
<?php if(!empty($record['utilities'])) { ?>
<ul>
<?php foreach ($record['utilities'] as $eachUtility): ?>
<?php echo ('<li>' . $eachUtility . '</li>'); ?>
<?php endforeach ?>
</ul>
<?php } ?>
Try this:
$output = "<ul>";
foreach ($record['utilities'] as $eachUtility) {
$output .= "<li>" . $eachUtility . "</li>";
}
$output .= "</ul>";
Can anyone how to implode a image file in breadcrumb in php.
this is my code
function fold_breadcrumb($variables) {
$breadcrumb = $variables['breadcrumb'];
if (!empty($breadcrumb)) {
// Use CSS to hide titile .element-invisible.
$output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
// comment below line to hide current page to breadcrumb
$breadcrumb[] = drupal_get_title();
$count= sizeof($breadcrumb);
/* print $count; */
$primarytitle=$breadcrumb[$count-2];
/* print $test; */
$output .= '<nav class="breadcrumb">' . implode(' » ', $breadcrumb) . '</nav>';
return $output;
}
}
Here i need to implode a css image file instead of »
Something like this maybe?
$img_breadcrumb = '<img src="/path/img_bcrumb.png">';
then
$output .= '<nav class="breadcrumb">' . implode($img_breadcrumb, $breadcrumb) . '</nav>';
What about simple analogy:
implode(' <img src="path/to/yourimage.file" /> ', $breadcrumb)
Or better:
implode(' <span class="separator"></span> ', $breadcrumb)
and define CSS background to your .separator class.
This is simple:
implode('<span class="separator"><img src="path/to/imagefile.ext" alt="yourimage.file"/></span>', $breadcrumb);
Hope this helps :)