Avoid showing icons that don't contain a value - php

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.

Related

PHP syntax for opening a new tab on click

I need to add target="_blank" property somewhere so that when logos are clicked they are opened in a new tab.
I am working on a website that uses the following PHP code to reference links in logos:
<?php if($cta): ?>
<div class="product-logos-container">
<?php foreach($cta as $_cta): ?>
<span class="card" <?php if($_cta['banner_link']): ?>onclick="window.location.href='<?php echo $_cta['banner_link'] ?>'"<?php endif ?>>
<span class="detail" style="opacity: 0;">
<span class="inner"><?php echo $_cta['banner_title'] ?></span>
</span>
<img src="/clear_cta/<?php echo $_cta['banner_filename'] ?>" alt="<?php echo $_cta['banner_title'] ?>" class="image"/>
......
Now I'm fairly certain that I need to add target="_blank" to the 4th line. I'm not sure which syntax is right for it, and after doing some research I'm starting to think that window.location might be the problem. If so please advise an alternative code that would open links when clicked in new tabs.
Use window.open instead of window.location
window.open('<?php echo $_cta['banner_link'] ?>',
'_blank' // <- This is what makes it open in a new window.
);
Only we can use target="_blank" inside anchor tags () to tell the browser where the linked document should be loaded.
Try This
1
<span>
See my portfolio
</span>
2
<a target = '_blank' href=view_rfp_detail.php?sna=$sna >$sna</a>
In Your case this is more of a JavaSript problem. The important part is
window.location.href=something
It is very complicated to open a new window or tab from JavaScript. This is the infamous popup. The browser started to block it by default long time ago.
You can try to rewrite this part
<span class="card" <?php if($_cta['banner_link']): ?>onclick="window.location.href='<?php echo $_cta['banner_link'] ?>'"<?php endif ?>>?>onclick="window.location.href='<?php echo $_cta['banner_link'] ?>'"<?php endif ?>>
Into something like
<a class="card" <?php if($_cta['banner_link']): ?>href="<?php echo $_cta['banner_link'] ?>" target="_blank"<?php endif ?>>
and change the closing tag too.

How to change a drop-down-menu item to a selection item

i want to change the dropdown language item in opencart to a static selection model, so both languages can be shown always and the user doesnt have to hover on the language flag to see the other language.
So I think i should change the language file in theme, and in following you can see the code, so what parts should i change? I believe I should change the type of the item from dropdown to something else?
<div id="language">
<div class="btn-group">
<button class="dropdown-toggle" type="button" data-hover="dropdown">
<?php echo $current_language; ?> <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<?php foreach ($languages as $language): ?>
<?php if ($type === 'flag'): ?>
<li><a onclick="$(this).closest('form').find('input[name=\'code\']').val('<?php echo $language['code']; ?>'); $(this).closest('form').submit();"><img width="16" height="11" src="<?php echo Journal2Utils::staticAsset('image/flags/' . $language['image']); ?>" alt="<?php echo $language['name']; ?>" title="<?php echo $language['name']; ?>" /></a></li>
This isn't really a programming question this is more of a task that you're looking for someone to do for you for free.
You should look for an extension in the marketplace (https://www.opencart.com/index.php?route=marketplace/extension) and if there isn't one you should pay a developer from the Commercial Forum (https://forum.opencart.com/viewforum.php?f=88).

Anchor Tag automatically calling href Without Click in Codeiginitor

Here in My View:
<div class="dropdown">
<button class="btn dropdown-toggle" type="button" data- toggle="dropdown">Dropdown Example
<span class="caret"></span></button>
<ul class="dropdown-menu">
<?php
foreach($site as $sites)
{
echo '<li >"'.$sites->site_title.'" </li>';
}
?>
</ul>
</div>
<?php
}
?>
i want to redirect when user click on $sites->site_title
but how it working is it automatically redirects to url
enter code here"<li><a href='shop/viewSiteId?id=".$sites->site_id."'>".$sites->site_title."</a></li>";
and get that id by using GET method
A redirect is the programmatic way to send a browser to a URL. In other words, a call to redirect is like clicking a link. They are not intended to work together in the way you have tried.
<?php
foreach($site as $sites) : ?>
<li>
<a href='<?= base_url("shop/viewSiteId?={$sites->site_id}"); ?>'><?=$sites->site_title; ?></a>
</li>
<?php endforeach;
If you are not familiar with the syntax, know that <?= is the shortcut way to write <?php echo
I've also used PHP Alternative Syntax for Control Structures and dropped in and out of the PHP processor a bunch of times. For me, that is the easier way to read and write this kind of thing. (Your mileage may vary.)

Doesn't display php echo values from mysql

I have 2 tables:
TABLE users (id, name, image, user_group_id)
TABLE user_groups (id, name, menu)
column menu value contains something like this:
<img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
Now at img src its creating a problem.
'.$row_UserDetails['Image'].'
This doesn't display the value.
Why is that and how do we fix this?
I want to display results from recordset UserGroup like this:
Now the value coming from php mysql is: <img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
you see .$row_UserDetails['Image'].
This value must be abc.jpg which comes from Recordest UserDetails:
On this php page if i want to display user name, i use php echo like this: <?php echo $row_UserDetails['Name'];?>
When using echo in php you will need to escape the quotes as they cause problems.
<header id=\"header-navbar\" class=\"content-mini content-mini-full\"> <ul class=\"nav-header pull-right\"> <li> <div class=\"btn-group\"> <button class=\"btn btn-default btn-image dropdown-toggle\" data-toggle=\"dropdown\" type=\"button\"> <img src=\"../images/employees/\"" .$row_UserDetails['Image']. "\" alt=\"\"> <span class=\"caret\"></span> </button> <ul class=\"dropdown-menu dropdown-menu-right\"> <li> <a tabindex=\"-1\" href=\"my-profile.php\"> <i class=\"si si-user pull-right\"></i> My Profile </a> </li> <li> <a tabindex=\"-1\" href=\"../logout.php\"> <i class=\"si si-logout pull-right\"></i>Log out </a> </li> </ul> </div> </li> </ul>
</header>
You say you echo $row_UserDetails['Image'], but I don't see an echo statement anywhere. Also, if that PHP code is really in your database like that, I believe you are on the wrong track.
Much better would be to use some sort of placeholder, and replace that by your desired database value when rendering that menu.
Something like this:
// assume something like "... <img src="../images/employees/%{image}"> ..."
// in the menu column, where '%{image}' serves as a placeholder
echo str_replace('%{image}', $row['image'], $row['menu']);
You need to echo your tag or echo your data inside the src attribute to use the value return by $row_UserDetails['Image'].
If you have done so, this may be the problem.
You have a issue with cocatenation. Your code should be like this
<?php echo "<img src='../images/employees/".$row_UserDetails['Image']."' alt='test'/>"; ?>
This article mat help you : https://teamtreehouse.com/community/why-do-we-use-concatenation-when-outputting-this-img-tag-with-php
Best regards !

Quick PHP echo help

What is wrong with this?
echo '<a title="Last Chance" href="'.the_permalink().'" class="status open">Last Chance</a>';
As it's putting the the_permalink() before the <a instead of inside it.
Wordpress often echo's the content out of the function instead of returning it.
Use get_permalink() instead.
echo '<a title="Last Chance" href="'.get_permalink().'" class="status open">Last Chance</a>';
http://codex.wordpress.org/Function_Reference/get_permalink
http://codex.wordpress.org/Function_Reference/the_permalink
Actually it looks good to me (but see my edit comment).
Better is to embed PHP into HTML:
<a title="Last Chance" href="<?php the_permalink(); ?>" class="status open">
Last Chance
</a>
Edit: As #Marwelln found out, the_permalink() is already echoing data. Still, this is a better solution than echoing the HTML.
I guess you have echo "abc" in the the_permalink function. In order for this to work as you wish, you have to return "abc" instead of using echo.
Use it like this (not inside echo )
<a title="Last Chance" href=" <?php the_permalink() ?> " class="status open">Last Chance</a>
See http://codex.wordpress.org/Function_Reference/the_permalink for more details.

Categories