Weird problem with PHP Breadcrumb Script - php

I'm using Mick Sears' php breadcrumb script - found here:
http://www.roscripts.com/PHP_breadcrumbs-118.html
I've used this script several times with no problems. But with this one site I'm having the weirdest problem... Home page - fine. Level 1 page - fine. But every time I move to a level2 page, the correct level1 crumb is replaced by "Help". The link on the crumb is the correct one for the help page. This happens even if I clear all browser caches and don't go to the Help section of the site at all.
The site is http://www.fastexas.org. The script is there, but I gave the breadcrumb div display:none; until I can figure this out.
This script seems to have been around awhile and I'm wondering if anyone else has seen this problem.

The Breadcrumb Script:
<?php
class Breadcrumb{
var $output;
var $crumbs = array();
var $location;
function Breadcrumb(){
if ($_SESSION['breadcrumb'] != null){
$this->crumbs = $_SESSION['breadcrumb'];} }
function add($label, $url, $level){
$crumb = array();
$crumb['label'] = $label;
$crumb['url'] = $url;
if ($crumb['label'] != null && $crumb['url'] != null && isset($level)){
while(count($this->crumbs) > $level){
array_pop($this->crumbs); }
if (!isset($this->crumbs[0]) && $level > 0){
$this->crumbs[0]['url'] = "/index.php";
$this->crumbs[0]['label'] = "Home";}
$this->crumbs[$level] = $crumb;}
$_SESSION['breadcrumb'] = $this->crumbs;
$this->crumbs[$level]['url'] = null;}
function output(){
echo "<ul>";
foreach ($this->crumbs as $crumb){
if ($crumb['url'] != null){
echo "<li> <a href='".$crumb['url']."' title='".$crumb['label']."'>".$crumb['label']."</a></li> ";} else {
echo "<li class='last'>".$crumb['label']."</li> ";}}
echo "</ul>";}}
?>
Each page begins with something like:
<?php session_start();
$level= '1';
$label= 'Honors Circle';
$url= '/honors/'; include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level); ?>
or
<?php
session_start();
$level= '2';
$label= 'Districts';
$url= '/honors/district.php';
include($_SERVER['DOCUMENT_ROOT']."/includes/Breadcrumb.php");
$trail = new Breadcrumb();
$trail->add($label, $url, $level);
?>
And to print the breadcrumb trail:
<div id="breadcrumb"><?php $trail->output(); ?></div>

Related

How to refresh with ajax in php?

I'm trying to make a likes/claps/heart system on my site and found this site (https://www.techolac.com/wordpress/how-to-add-likes-to-posts-in-wordpress-without-a-plugin/) that taught how to do it. I made some adjustments the problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Functions.php / Wordpress
// Add buttons to top of post content
function ip_post_likes($content) {
ob_start();
?>
<a href="<?php echo add_query_arg('post_action', 'like'); ?>">
<span class="icon-claps pr-2"><?php echo get_like_count('likes') ?></span>
</a>
<?php
$output = ob_get_clean();
return $output . $content;
}
add_filter('the_content', 'ip_post_likes');
//Get like
function get_like_count($type = 'likes') {
$current_count = get_post_meta(get_the_id(), $type, true);
return ($current_count ? $current_count : '');
}
//Process like
function ip_process_like() {
$processed_like = false;
$redirect = false;
// Check if like
if(is_singular('post')) {
if(isset($_GET['post_action'])) {
if($_GET['post_action'] == 'like') {
// Like
$like_count = get_post_meta(get_the_id(), 'likes', true);
if($like_count) {
$like_count = $like_count + 1;
}else {
$like_count = 0;
}
$processed_like = update_post_meta(get_the_id(), 'likes', $like_count);
}
if($processed_like) {
$redirect = get_the_permalink();
}
}
}
// Redirect
if($redirect) {
wp_redirect($redirect);
die;
}
}
add_action('template_redirect', 'ip_process_like');
The problem is that when I do like the page refresh and I just wanted the number to refresh and not the whole page.
I saw what I could do in AJAX but i didnt know how.
Image:
This is the whole code. I am using wordpres and twig / timber

PHP pageName pulling from other page

I'm working on my script site I have the url setup like this with PHP.
movies.php?pages=action
But every time a member goes to the url it pulls the page name from movies.php how can I fix this issue I have pasted my code down below.
<?php
// StreamPlex Page System - Play Page
DEFINE("IN_STREAMPLEX", 1);
$pageName = "All Movies";
require_once('inc/config.php');
require_once('inc/header.php');
$action = $_GET['action'];
$pages = $_GET['pages'];
if(isset($pages)) {
require_once("{$pages}.php");
} else {
?>
Then at the bottom of the page I have finished off with this code here
<?php include 'inc/footer.php'; ?>
<?php } ?>
Can somebody please help me in telling me what I'm doing wrong.
I have fix the issue i had change around the code at the top of the page from
<?php
DEFINE("IN_STREAMPLEX", 1);
$pageName = "All Movies";
require_once('inc/config.php');
require_once('inc/header.php');
$action = $_GET['action'];
$pages = $_GET['pages'];
if(isset($pages)) {
require_once("{$pages}.php");
} else {
?>
To this
<?php
// StreamPlex Page System - Play Page
DEFINE("IN_STREAMPLEX", 1);
$action = $_GET['action'];
$pages = $_GET['pages'];
if(isset($pages)) {
require_once("{$pages}.php");
} else {
$pageName = "All Movies";
require_once('inc/config.php');
require_once('inc/header.php');
?>
Now it works in loads with no problems.

Need some PHP assistance

how do i tell this php code that, when there's not a ?page=randompage in the url, it will trow the active to index.php automatically?
<?php
$query = "SELECT pagename, pagetitle FROM pages LIMIT 8";
$result = $mysqli->query($query);
echo "<ul>";
while ($row = $result->fetch_array(MYSQLI_ASSOC)){
echo "<li><a href=\"?page=".$row["pagename"]."\"";
if ($_GET['page'] == $row['pagename']) {
echo " class=\"active\""; } echo "> ".$row["pagetitle"]." </a></li>";
}
echo "</ul>";
?>
It's beacuse whenever my url looks like this http://localhost/greencph/ it shows a php error, because it does not know which site it is on, it works perfect as long as the url looks like this: http://localhost/greencph/?page=index.php a detailed explanation on how to fix this problem would be appreciated!
Please remember i'm a idiot, explain to me so i understand it. xD
The error is coming because, every time the value of $_GET is not getting set.
So, to deal with it,
get the value of $_GET['page'] in a variable.
So that, if we do not get $_GET, we will assign it a default value, that is index page.
$page = ! empty($_GET['page']) ? $_GET['page'] : 'index'; // set default value.
And change this line:
if ($page == $row['pagename']) {
So, the final modified code should be:
<?php
$query = "SELECT pagename, pagetitle FROM pages LIMIT 8";
$result = $mysqli->query($query);
$page = ! empty($_GET['page']) ? $_GET['page'] : 'index';
echo '<ul>';
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$class = '';
if ($page == $row['pagename']) {
$class = 'active';
}
echo '<li>' . $row["pagetitle"].' </li>';
}
echo "</ul>";
?>
Use the $_GET array:
<?php
if(isset($_GET['page'])) {
// Your code
...
}
?>
Problem is in this line
if ($_GET['page'] == $row['pagename']) {
You're not checking whether the index 'page' even exists. So, when you try to load the page without any GET parameters $_GET['page'] won't be even set and you'll get an error for accessing unset element.
To fix this simply check
if(isset($_GET['page']) {
...your code...
}

wordpress is_front_page() is not working (static front page)

I am using a WP - Alert plugin for a wordpress website. The issue is that it displays on all the pages even though it is set as display on home page only. (is_home). However, my home page is a static home page so I have set it to is_front_page and still it displays on all the pages. If you could just let me know if I am missing out anything??
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$display = true;
$homeOption = get_option('alert_show_on_home');
$pageOption = get_option('alert_show_on_page');
$postOption = get_option('alert_show_on_post');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
$display = true ;
}elseif($pageOption == 'yes' && is_page()){
$display = true ;
}elseif($postOption == 'yes' && is_single()){
$display = true ;
}else{
$display = false ;
}
if($display){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
I added this line to the code above and still it won't display on the static home page only.
} elseif($homeOption == 'yes' && is_front_page()){
$display = true ; }
Thanks in advance guys :)
WordPress loads functions.php before the $wp_query object has been set up with the current page. is_front_page is a wrapper around around $wp_query->is_front_page(), and if the query hasn't been set up, it's always going to return false.
For more on the topic, see this question : https://wordpress.stackexchange.com/questions/41805/is-front-page-only-works-in-theme-file-and-does-not-work-in-functions-php
To use it in your functions.php, you will have to wrap it in an action that is after the $wp_query object is instantiated.
add_action('wp', function () {
if (!is_front_page()) {
add_action('wp_print_styles', function () {
wp_deregister_style('wpos-slick-style');
wp_deregister_style('wpsisac-public-style');
}, 100);
}
});
Is this showing in your static home page now??
Try this
function wp_alert_add_sticky_alert(){
if(get_option('alert_show_on_site') == 'yes'){
$homeOption = get_option('alert_show_on_home');
$getID = get_the_ID();
if($homeOption == 'yes' && is_home ()){
?>
<div class="sticky-box">
<div class="sticky-inner"><?php echo stripslashes(get_option('wp_alert_message')); ?>
</div>
</div>
<?php
}
}
}
?>
Hope this works for you

Are PHP script read from Top to bottom?

Are PHP scripts reads from top to bottom? like HTML? because in this code
<?php require_once("./includes/connection.php")?>
<?php require_once("./includes/functions.inc.php"); ?>
<?php
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
?>
<?php include("includes/header.inc.php"); ?>
<table id="structure">
<tr>
<td id="navigation">
<ul class = "subjects">
<?php
$subject_set = get_all_subjects();
while($subject = mysql_fetch_array($subject_set))
{
echo "<li";
if($subject['id'] == $sel_subj['id']) {echo " class =\"selected\"";}
echo "><a href=\"content.php?subj=" . urlencode($subject["id"]) .
"\">{$subject["menu_name"]}</a></li>";
echo "<ul class = 'pages'>";
$page_set = get_pages_for_subject($subject['id']);
while($page = mysql_fetch_array($page_set))
{
echo "<li";
if($page['id'] == $sel_page['id']){echo " class = \"selected\"";}
echo"><a href=\"content.php?page=" . urlencode($page["id"]) .
"\">{$page["menu_name"]}</a></li>";
}
echo "</ul>";
}
?>
</ul>
</td>
<td id="page">
<?php if(isset($sel_subj)){?>
<h2><?php echo "{$sel_subj['menu_name']}";?></h2>
<?php } ?>
<?php if(isset($sel_page)){?>
<h2><?php echo "{$sel_page['menu_name']}"?> </h2>
<?php }?>
</td>
</tr>
</table>
<?php require("includes/footer.inc.php"); ?>
specifically this part
if(isset($_GET['subj']))
{
$sel_subj = get_subject_by_id($_GET['subj']);
$sel_page = NULL;
}else if(isset($_GET['page']))
{
$sel_subj = NULL;
$sel_page = get_page_by_id($_GET['page']);
}else
{
$sel_subj = NULL;
$sel_page = NULL;
}
How is this if-else block being called if it's on top of the page?
It is run top-to-bottom one time per page view. On the initial view, assuming the URL has no parameters, then neither $_GET['subj'] or $_GET['page'] will be set.
If the link pointing back to the same page is clicked, then the entire PHP file will be reprocessed. If that link contained subj or page in the URL as a query variable, then the corresponding if block will be executed and the page will be altered accordingly.
Think of the PHP server as dynamically creating some HTML file that is sent to the web browser. Once it is sent, the server is done, and the PHP code is "gone." The only way to run more PHP code is to request a new page, where the process starts over.
(Even AJAX follows the same principles, although generally then you are dealing with partial data requests as opposed to full page views.)
Yes, scripts are run from top to bottom. I don't understand why you think that if-else block is any different? Those if clauses are run to set the $sel_subj and $sel_page variables before the rest of the page is executed and output.
PHP scripts are executed from top-to-bottom. What exactly is your problem?

Categories