PHP if database has url echo URL using hyperlinked button - php

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.

Related

Why is it everytime i try too open up PHP script inside my HREF it kills my site and wont run anything after this?

Im trying to use the following code:
<a class="brand" href="<?php echo site_url('admin/dashboard'); ?>"><?php echo $meta_title; ?></a>
It loads an empty navbar but wont load anything under and there are no links.
cheers.
I didn't understand your question. Give more details.
What is the result of the following?
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
echo site_url('admin/dashboard');
echo $meta_title;
?>

Make a php a href link with variable clickable

i have a question.
I have this code to get an profile name as an link.
<a href=<?php echo $userpro->permalink( get_the_author_meta('ID') );
?>><?php echo $user_id=get_the_author_meta('display_name');?></a>
The link isnt clickable. I am a newbie in coding.
So maybe somebody can help me :)
Greetings
Try this:
<a href="<?php echo $userpro->permalink(get_the_author_meta('ID')); ?>">
<?php echo get_the_author_meta('display_name'); ?>
</a>
If it doesnt work... please supply the value and initialisation of $userpro!

Passing URL does not output variable with echo

The link page:
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
The displayProduct.php page:
<?php
echo $_GET["productID"];
?>
This is my tester, to see if clicking on the link will pass the data from page 1 to displayProduct page.
It successfully passes the variable in the ' URL ' bar but the echo does not display that variable.
Am I missing something?
You missed an echo and have some weird concatenation.
<a href="displayProduct.php?productID=<?php'.$pID.'?>">
should be:
<a href="displayProduct.php?productID=<?php echo $pID; ?>">
<a href="<?php echo 'displayProduct.php?productID=' . $pID; ?>">
The shortest way:
<a href="displayProduct.php?productID=<?php echo $pID ?>">

Php if empty variable statement

I'm trying to build an if statement for when a variable is filled with a url it will display a button and link to the site, and when the variable is empty the link and button will not display. So far I got it to where it is displays the button and links but making it into an if statement keeps on breaking my site. If you see a problem with the code below, please help. Thanks
<div id="social_icon">
<?php if (isset($fburl))
{
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"
height="30"></a>;
}
else
{
//dont show anything
}
?>
</div>
You're trying to use HTML within your PHP code, so PHP sees this as an unexpected variable/string. Either use echo for this, or close the PHP statement, and then write your HTML.
Either:
<div id="social_icon">
<?php if(isset($fburl)){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Or:
<div id="social_icon">
<?php if (isset($fburl)){
echo '<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />';
}else{
//dont show anything
} ?>
</div>
Edit
Actually, I would assume it's not outputting anything because your if statement is checking for $fburl whereas you're echoing the link as $options['fburl']. If the facebook url is located at $options['fburl'], try:
<div id="social_icon">
<?php if(isset($options['fburl'])){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Edit 2
If the options are set but don't contain a link, you will also need check for that:
<div id="social_icon">
<?php if(isset($options['fburl']) && !empty($options['fburl'])){ ?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png" width="30" height="30" />
</a>
<?php }else{
//dont show anything
} ?>
</div>
Syntax error, change it to:
<?php if (isset($fburl))
{
//missed end tag here
?>
<a href="<?php echo $options['fburl']; ?>">
<img src="http://farm6.staticflickr.com/5534/9135106179_43deba3d15_o.png"" width="30"
height="30"></a>;
<?php
//add another php start tag
}
else
{
//dont show anything
}
?>

Javascript Yii: Session script

Okay, so basically whenever someone gets on the site the icons are all the same color (White) I want this code to do the follow thing : Whenever someone goes to another page (by clicking on one of the other icons) the icon of the page where he is going should have a different color (blue, this is done by loading another image) but only that page, this should also stay whenever he refreshes the page?
This is the current code that I have for a single icon, its not working yet and im not sure what is wrong..
<?php Yii::app()->session['/home'] = '1';
$home = Yii::app()->session['/home'];
//echo Yii::app()->session['home']; // Prints "value"
if (!empty($home)){ ?>
<a href="<?php echo $this->createUrl("/admin/survey/sa/index")."/home"; ?>">
<img src='<?php echo $sImageURL;?>home.png' alt='<?php $clang->eT("Default administration page");?>' width='<?php echo $iconsize;?>' height='<?php echo $iconsize;?>'/></a>
<?php
}
else{?>
<a href="<?php echo $this->createUrl("/admin/survey/sa/index")."/home1" ?>">
<img src='<?php echo $sImageURL;?>home1.png' alt='<?php $clang->eT("Default administration page");?>' width='<?php echo $iconsize;?>' height='<?php echo $iconsize;?>'/></a>
<?php
}
?>
I agree with Ivo; I think it would be best to do this using CSS classes. You would have the default CSS class have a background image of home1.png, and the blue one have a background image of home.png. If I were doing this, I would use CSS sprites as well.
But, working with what you've got. Here's one way to do it in Yii:
<?php
// Get the current URI
$currentUri = Yii::app()->request->requestUri;
if($currentUri == '/admin/survey/sa/index'){
?>
<a href="<?php echo $this->createUrl("/admin/survey/sa/index")."/home1" ?>">
<img src='<?php echo $sImageURL;?>home1.png' alt='<?php $clang->eT("Default administration page");?>' width='<?php echo $iconsize;?>' height='<?php echo $iconsize;?>'/>
</a>
<?php
}else{
?>
<a href="<?php echo $this->createUrl("/admin/survey/sa/index")."/home1" ?>">
<img src='<?php echo $sImageURL;?>home1.png' alt='<?php $clang->eT("Default administration page");?>' width='<?php echo $iconsize;?>' height='<?php echo $iconsize;?>'/>
</a>
<?php
}
?>
For your other icons you can then reuse the $currentUri variable. You don't need to set any sessions. When you refresh the page the icon will still remain colored.

Categories