Conditional Back OR Close link in PHP - php

I've tried to find a solution to this a few different times, but keep coming up empty on a working solution. Essentially, I'm trying to find a way to have a link that is either a "close" or a "back" link in functionality. The condition being IF the use just came from another page within the same site OR if the user came into this page from an external link (search engine or otherwise).
Here's a couple links for further explanation and examples of how its NOT working:
This link shows a "list" or archive of posts: http://hillsiderancho.com/care-ministries/
Once a user goes into one of those posts (http://hillsiderancho.com/care-ministries/celebrate-recovery/), the link in the top right should read "Back", and should simply act as the browser back button, to allow the user to go back to viewing the list of pages (or possibly some other page that lead them to this post).
The other use case would be if the user comes into the post from an external site, the button should read "Close" and then point the user to the list page, not a "browser back" function that takes them away from the site again. We'd like to keep them in the site and let them explore more from the same area.
This is the code I have in place, and the "Back" button has a different functionality in nearly all of the different posts in this section... seeming as though its never hitting the "else" condition, and only coming up with some other strange referrer URL. (I just noticed a typo on my href in the "else" condition, but either way, its not getting to that page either):
<div class="post-close-btn">
<?php
$previousPage = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$url = site_url();
if ($previousPage = $url){ ?>
<a href="<?php echo $_SERVER['HTTP_REFERER']; ?>" class="primaryBtn">
<div class="inner vAlign">
<span class="text">BACK</span>
</div>
</a>
<?php } else { ?>
<a href="/care-ministries/" class="primaryBtn">
<div class="inner vAlign">
<span class="text">Close</span>
</div>
</a>
<?php } ?>
Can anyone help with this?

You are setting the variable in your if statement, not checking its value.
if ($previousPage = $url)
should be:
if ($previousPage == $url)

Here's the code that ended up working, after getting my variable corrected (thanks Robyn Overstreet)
<?php
$previousPage = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$url = site_url();
$url = preg_replace('#^https?://#', '', $url);
if ($previousPage == $url){ ?>
<a href="javascript:history.back();" class="secondaryBtn">
<div class="inner vAlign">
<span class="text">BACK</span>
</div>
</a>
<?php } else { ?>
<a href="/care-ministries/" class="secondaryBtn">
<div class="inner vAlign">
<span class="text">Close</span>
</div>
</a>
<?php } ?>
Also, I found the bit I needed to fix the URL (and get it to match up) here: https://stackoverflow.com/a/9549893/4842348
And finally, I should note that my "if" statement href was pointing back to the site homepage (duh, thats what it was told to do) - so I swapped in a basic JS history.back() - that works just fine.

Related

PHP deep inside HTML tags and attributes

I've come across a situation where there's a gnarly mix of HTML and PHP (well at least it seems that way to me because I'm not an expert in PHP). Currently, there's a hard-coded URL that I'd like to generalize using a PHP function. However, this is where I'm running into issues as this mix is getting rather complex.
After spending over 2 hours on this, I think I'm at a point where looking through topics on this doesn't seem to discuss this particular use case, and lots of trial-and-error isn't yielding the desired results.
Inside my template, I have the following code for my sidebar:
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<?php
// First attempt HTML in PHP
$home_url = get_home_url();
echo '<div class="field-title"><a class="field-value rating" target="_blank" href=' . $home_url . '/rating-criteria/' . '>Rating:</a> <span class="rating">' . the_field('rating') . '</span></div>'; ?>
// Second attempt PHP in HTML
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php the_field('rating'); ?></span></div>
// The hard-coded URL that I'm trying to generalize
<div class="field-title"><a class="field-value difficulty" target="_blank" href="https://s1.temporary-access.com/~allacros/sandbox2/difficulty-criteria/">Difficulty:</a> <span class="difficulty"><?php the_field('difficulty'); ?></span></div>
...
The results of this code can be seen in the sidebar in:
https://s1.temporary-access.com/~allacros/sandbox2/california-switzer-falls.html
In that sidebar (beneath "About Switzer Falls" below the Hero Image), you can see 2 Ratings.
The first one has the correct link, but the formatting is off as the "2" is not where it's supposed to be and it's unformatted.
The second one has the correct formatting, but it has the incorrect link.
Anyone know what I'm doing wrong?
Thanks
For the first line, looks like there is "echo" statement in your function the_field("rating"), which cause the rating "2" output first before your "echo" execute.
For the second line, there is error at "$php", which should be "?php".
Change your 2nd statement to the following:
<div class="field-title"><a class="field-value rating" target="_blank" href="<$php $homeurl = get_home_url(); echo $homeurl; ?>/rating-criteria">Rating:</a> <span class="rating"><?php echo the_field('rating'); ?></span></div>
In addition to what others have mentioned about the $ where a ? should be, you could maybe even simplify things for yourself by doing something like this for the link. All you've got to do is echo the $home_url variable you're creating at the start.
link
It's quite simple, really. There's a number of things you are doing on one single line that you don't actually need to do...
<h4>About <?php the_title(); ?> </h4>
<div id="about-this-waterfall-acf">
<div class="field-title">
<a class="field-value rating" target="_blank" href="<?php echo home_url(); ?>/rating-criteria/">Rating:</a>
<span class="rating"><?php the_field('rating'); ?></span>
</div>
Now, things to note... I split the HTML over multiple lines instead of trying to keep it on just one. I have also removed the bulk of it from PHP processing altogether, as the static HTML doesn't need to go through PHP. This cleans it up immensely.
Lastly, if you look back at your code, you were echoing out the return value of the_field. This is where your random '2' is coming from, most likely. the_field should be outputting the value itself, so you should not concatenate it's return value on then output it.

Why is this XML attribute showing up twice

In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code

php multi lang site clean url

I'm developing a multilang site. The content generated on the varible that passes in the url. Exemple for about us page my url is: domain.com/file.php?id=1 I got one main file and in that file the query gets the id of the selected menu.
If I change the language my url turns to domain.com/file.php?id=1&lang=en. Every time I change the language my url adds one more lang like this: domain.com/file.php?id=1&lang=en&lang=fr&lang=de&lang=en.....
in other multilang project I used this: header("location: ".$_SERVER['SCRIPT_NAME']); But the it were less dynamic pages. like this: domains.com/aboutus.php. I mean: the number of pages were static. The user cannot add or remove pages.
This time because I pass the page id in the url I tried header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']); but it gives an redirect cycle error every time I try to change the lang.
UPDATE
Code to select the languages:
<?php $actual= $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"];?>
<div id="langContainer">
<span><a <?php if ($_SESSION['idLang']=='en') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=en">EN</a></span>
<span><a <?php if ($_SESSION['idLang']=='fr') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=fr">FR</a> </span>
<span><a <?php if ($_SESSION['idLang']=='es') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=es">ES</a></span>
<span><a <?php if ($_SESSION['idLang']=='de') {echo"class='active'";}?> href="<?php echo $actual ?>&lang=de">DE</a></span>
</div>
in my session.php
if (!isset($_SESSION["idLang"]) )
$_SESSION["idLang"] = 'en';
if (#isset($_GET["lang"])){
$_SESSION["idLang"] = $_GET['lang'];
//header("location: ".$_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING']);
}
So my question is if there's anyway I can get my url cleaner, hidding the lang variables?
Thanks
Dynamically build query parameters, replacing exiting ones in the current URL:
<a href="...?<?php echo http_build_query(array('lang' => 'foo') + $_GET); ?>">

Stripping Specific HTML & Content from a page with PHP for RSS

I am building a mobile version of my company website, and one thing we are in need of is an RSS feed.
I have the RSS pulling in fine with this code:
<?php
$url = 'http://www.someurl.com/rss/articles';
$feed = simplexml_load_file($url, 'SimpleXMLIterator');
$filtered = new LimitIterator($feed->channel->item, 0, 15);
foreach ($filtered as $item) { ?>
<li data-icon="false">
<h2><?php echo $item->title; ?></h2>
<p class="desc"><?php echo $item->description; ?></p>
<br />
<p class="category"><b><?php echo $item->category; ?></b></p>
<a class="link" href="<?php echo $item->link; ?>">Read More</a>
<br />
<p class="pubDate"><?php echo $item->pubDate; ?></p>
<br />
</li>
<?php } ?>
What I would like to do is utilize either the fopen() or file_get_contents() to handle the clicking of the 'Read More' link and strip all of the contents of the incoming page except for the <article> tag.
I have searched Google the past day, and have not been successful in finding any tutorials on this subject.
EDIT:
I would like to load the stripped HTML contents into their own view within my framework.
SECOND EDIT:
I would just like to share how I solved this problem.
I modified my $item->link; to be passed through the URL as a variable:
Read More
On the article.php page, I collect the variable with a if() statement:
if (isset($_GET['rss_url']) && is_string($_GET['rss_url'])) {
$url = $_GET['rss_url'];
}
Then building on the suggestions of the comments below, I built a way to then collect the incoming URL and strip the necessary tags to then format for my mobile view:
<div id="article">
<?php
$link = file_get_contents($url);
$article = strip_tags($link, '<title><div><article><aside><footer><ul><li><img><h1><h2><span><p><a><blockquote><script>');
echo $article;
?>
</div>
Hopefully this helps anyone else who may encounter this problem :)
I'm not sure if I understand it correctly but are you trying to output the contents on the current page whenever someone clicks the more link?
I would probably use Javascipt to do that, maybe jQuery's .load() function which loads html from another page and allows you to load only specific fragments of a page.. but if you need to use php I would look into Simple HTML DOM Parser
$html = file_get_html($yourUrl);
$article = $html->find('article', 0); // Assuming you only have 1 article/page
echo $article;
The only way I can see is to set up your own separate script to route the links through.
So, instead of echo $item->link use
echo 'LinkProcessor.php?link='.$item->link
Then, setup a script called LinkProcessor.php and use file_get_contents on that page. You can then process the XML to only show the article tag and echo the results:
$article = file_get_contents($_GET['link']);
$xml = new SimpleXMLElement($article);
$articleXml = $xml->xpath('//article');
echo articleXml[0];
Note that the code is untested, but it should be OK.

How to get the URL of the current page with the title and post it to Digg?

We have a Joomla site and have purchased a template from Gavick.
I need to change the code which is part of a template we have purchased.
If I hardcode the parameters associated to the Digg button as follows then I am taken to Digg's website submission link form and can add the details.
The current code in the template is as follows:
<?php if($this->template->params->get("icon2", 1) == 1) : ?>
<a href="<?php echo $this->template->params->get("icon2_link", ''); ?>"
class="social_icon" id="social_icon2" target="_blank">Digg</a><?php endif; ?>
What I need to do is get the URL of the current page along with the title and post this to Digg.
Never used Joomla but...
<?php if($this->template->params->get("icon2", 1) == 1) : ?>
<a href="http://digg.com/submit?Url=<?php echo $_SERVER["REQUEST_URI"]?>&title=<?=mainframe->getPageTitle()?>&no_mobile=1"
class="social_icon" id="social_icon2" target="_blank">Digg</a><?php endif; ?>
I don't know variable for title.

Categories