Multilingual HTML page with PHP - how to add titles to links - php

I implemented text switching system as portrayed here http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html .
Every link is defined like so:
<li><?php echo $lang['MENU_REGISTER']; ?></li>
By this logic, adding a title/description to a link would work like so:
<a title=<?php echo $lang['MENU_REGISTER']; ?> href="#">
<?php echo $lang['MENU_REGISTER']; ?></a>
I'm under impression that this does not work the way as I wish it would.
Any suggestions?

You are missing opening and closing quotes:
title="<?php echo $lang['MENU_REGISTER']; ?>"
and preferably addslashes for other quotes:
title="<?php echo addslashes($lang['MENU_REGISTER'];) ?>"

Related

PHP if database has url echo URL using hyperlinked button

I am trying to see if a file in my database exists, and if it does, show that file as a hyperlinked button. The file is a URL. I am able to get the file to show accordingly, but the button shows even if there is no hyperlink. I have been researching everywhere to find out how I can echo the button only if there is a corresponding file but have had no luck. Just can get it right. Here is what I have and appreciate the help.
<?php if (file_exists($video))?><a class="videobutton" href="<?php echo $video; ?>"></a>
So based on Your Comment that you want to hide the whole button if the file is not found so this is how to do it :
<?php
if (file_exists($video)): ?>
<a class="videobutton" href="<?php echo $video; ?>"></a>
<?php endif; ?>
OR
if (file_exists($video)){ ?>
<a class="videobutton" href="<?php echo $video; ?>"></a>
<?php } ?>
<?php if (file_exists($video)){?>
<a class="videobutton" href="<?php echo $video; ?>"></a>
<?php }
else {
///
} ?>
In my opinion you missed the "{" brackets within the if statement.

Add hreflang to magento language switcher

I am trying to add hreflang="ca-fr" or "ca-en" my code is below
<?php if ($_lang->getId() != $this->getCurrentStoreId()): ?>
<li class="language-<?php echo $this->htmlEscape($_lang->getCode()); ?>">
<a href="#" onclick="changeLang('<?php echo $_lang->getCurrentUrl() ?>')"
hreflang="<?php echo $this->htmlEscape($_lang->getCode()); ?>"
id="link-<?php echo $_lang->getId() ?>"><?php echo $frontendNameAssign[$_lang->getId()]; ?></a>
</li>
<?php endif; ?>
I added the 4th line to the file but this displays as "ca_us_english", is there a way to amend this?
I have also tried amending Miscellaneous Scripts with no joy, any help would be grateful.
thanks
Mel
Well in this case you are "right" and Magento is "wrong".
What Magento is actually presenting you as $_lang is not really the lang, but one of your store.
So ca_us_english is the code of your english candian store in your backend (you can have a look at it in the admin under System > Manage Stores)
To get the actual local set for this store you have to use this line of code :
Mage::getStoreConfig('general/locale/code', $_lang->getId())

WP is_front_page and displaying images

I'm working on a new WordPress theme (haven't touched WP themes since like 2.8...) and I'm attempting to make an if/else statement that changes the behavior of my top navigation bar based on whether the page is a front_page or not.
Essentially what I'm after is if the page is a front_page, then display the logo image. If the page is any other kind of page, then display a simple H1 element with the company name:
<li class="name">
<?php
if (is_front_page()) {
echo '<a href="<?php bloginfo("url"); ?>';
echo '<img src="' .bloginfo( "template_directory" ). '/images/logo-dsi.png" alt="DSI" />';
echo '</a>';
} else {
echo '<a href="' .home_url(). '">';
echo '<h1 class="logo">DSI</h1>';
echo '</a>';
}
?>
</li>
The else statement is working fine. But my if statement is not producing the img tag correctly. It echoes the literal bloginfo('template_directory') url and not inserting that into the img src, which is what I want it to do. It's trying to find an image at this url: localhost/images/logo-dsi.png
Obviously my syntax on line 5 is wrong somewhere, but I'm also not sure if I'm going about this the right way. Is there a better solution to what I'm trying to accomplish here?
Thanks!
The later is correct however
echo '<a href="<?php bloginfo("url"); ?>';
Should be
echo '<a href="' . bloginfo("url") . '">';
I tried this out and it worked:
<li class="name">
<a href="<?php bloginfo('url'); ?>">
<?php
if (is_front_page()) { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/logo-dsi.png" alt="DSI" />
<?php
} else { ?>
<h1 class="logo">DSI</h1>
<?php } ?>
</a>
</li>
Apologies, I should have hacked away at it a little longer. But maybe this will become helpful to someone else.
I knew echos in WordPress looked bad for a reason... ;-)
Also I'm sure there's a prettier way to write this out, but for now, that code does the trick.

Avoid showing icons that don't contain a value

I've already been searching for a couple of hours for a solution. What I want to do is make icons that don't got a value (in the php) not show up.
Here's an image of what I currently have.
So for instance if only twitter and facebook got values, they should only appear on the screen.
Below is the code, and I hope someone got a solution for this.
Cheers!
<ul class="social-buttons">
<div class="wrapping">
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<a class="social" href="http://facebook.com/<?php echo $profile_data['facebook']; ?>" target="_blank"><li class="facebook"></li></a>
<a class="social" href="skype:<?php echo $profile_data['skype']; ?>?chat"><li class="skype"></li></a>
<a class="social" href="http://instagram.com/<?php echo $profile_data['instagram']; ?>"><li class="instagram"></li></a>
<a class="social" href="http://dribbble.com/<?php echo $profile_data['dribbble']; ?>"><li class="dribbble"></li></a>
<a class="social" href="http://linkedin.com/in/<?php echo $profile_data['linkedin']; ?>"><li class="linkedin"></li></a>
</div>
</ul>
You need to use the if statement with !empty(). The !empty() checks if a variable is NOT empty. Than proceed with the code. Like the example given here:
<?php
if(!empty($profile_data['twitter'])){
?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php
}
?>
If the variable is empty, it wont give the outputed code, in your case the <a> with the icon.
i think you can do like:
<ul class="social-buttons">
<div class="wrapping">
<?php if $profile_data['twitter'] {?>
<a class="social" href="http://twitter.com/<?php echo $profile_data['twitter']; ?>" target="_blank"><li class="twitter"></li></a>
<?php } ?>
....
</ul>
Imo, a probably better way to do this is to perform a pre-processing of the data i.e. $profile_data, before you use it in your view so the view would no longer need to handle the processing logic. After which, the view can output your links by using a more concise construct e.g. for loop, that does not use any conditional branching.

What's the final solution to set html attributes with PHP?

I'm now doing it this way:
<a title="<?php echo $title; ?>">...
But it will brreak when " is included in $title.
Not that it's "the final solution", but obviously you need to escape any literal string that isn't mean to contain HTML. In this case:
<a title="<?php echo htmlspecialchars($title); ?>">
You should run that through htmlspecialchars first to make sure your HTML won't break.
You should translate special characters into HTML entities first, easily done with htmlentities().
<a title="<?php echo htmlentities($title); ?>">

Categories