I am trying to include a php variable inside this php script that displays an html link. What i need is to include my php $row['vin'] variable in the href html link after the ? to pass a value to the page i am linking to. Top code block works but i still need that php variable, bottom is an example of what ive tried which will not work.
Works but missing my php variable:
<?php if($row['instock'] == "Yes")
{
echo '<a href="orderForm.php?">
<span class="glyphicon glyphicon-plus" aria-hidden="true">
</span>
</a>';
}
?>
Does not work:
<td>
<?php if($row['instock'] == "Yes")
{
echo '<a href="orderForm.php?'. $row['vin']">
<span class="glyphicon glyphicon-plus" aria-hidden="true">
</span>
</a>';
}
?>
</td>
You've forgotten the rest of the concatenation logic. It's just a syntax error:
<td>
<?php if($row['instock'] == "Yes")
{
echo '<a href="orderForm.php?'. $row['vin'] . '">
<span class="glyphicon glyphicon-plus" aria-hidden="true">
</span>
</a>';
}
?>
</td>
An alternative to using PHP to echo out great chunks of mostly static content is to instead, drop in and out of the PHP context (ie <?php ... ?>) when necessary and use it like a templating language.
For example
<td>
<?php if ($row['instock'] == "Yes"): ?>
<a href="orderForm.php?<?= htmlspecialchars($row['vin']) ?>">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</a>
<?php endif ?>
</td>
See http://php.net/manual/control-structures.alternative-syntax.php
Related
Context :
When I use a PHP echo <?= &var ?> in HTML, it causes white space in my HTML element.
I have already tried to modify the file with another IDE to be sure that it is not an issue of indents but I have the same result. Same result on Chrome or Firefox...
Problem :
The filter of the plugin tablesorter also filters the white spaces and that does not return me the expected result. It works if I manually remove spaces
Question :
Is this a PHP syntax error or something else?
My PHP code :
<td class="hidden-sm hidden-xs date">
<?php if ($approval['STACODEM'] == '200'):?>
<span data-toggle="popover" data-placement="top" data-trigger="hover focus" data-container="body" data-content="<?= $imgDuesDaysPopover ?>">
<img src="<?= $imgDueDays ?>" alt="" name="imgDueDays"/>
</span>
<?php endif ?>
<?= $dueDate ?>
</td>
In the Chrome viewer :
The line with a date :
If I filter, no results found :
It's because of your html layout. You can inline your code to solve this.
<td class="hidden-sm hidden-xs date">Everything in here</td>
Or you can customize how the tablesorter orders your rows.
Easiest inlining would be by assigning the result of your if clause to a variable.
<?php
$img = '';
if ($approval['STACODEM'] == '200') {
$img = <<<IMG
<span data-toggle="popover" data-placement="top" data-trigger="hover focus" data-container="body" data-content="{$imgDuesDaysPopover}">
<img src="{$imgDueDays}" alt="" name="imgDueDays"/>
</span>
IMG;
}
?>
<td class="hidden-sm hidden-xs date"><?= $img.$dueDate ?></td>
PS: But be careful, your $img variable will also have spaces if you don't inline it. (Don't know if your tablesorter completely ignores the html.)
Thanks to Ozan Kurt, I have stored everything in a variable so the code remains readable.
<?php
$html = '';
if ($approval['STACODEM'] == '200') {
$html .= '<span data-toggle="popover" data-placement="top" data-trigger="hover focus" data-container="body" data-content="' . $imgDuesDaysPopover . '">';
$html .= '<img src="' . $imgDueDays . '" alt="" name="imgDueDays"/>';
$html .= '</span>';
}
$html .= $dueDate;
?>
<td class="hidden-sm hidden-xs date"><?=$html?></td>
I want to use html syntax in php and i want to use . instead of <?php ?> my code ig given below ,
$delete='<a class="confirm" onclick="return delete_event(<?php echo $value->id; ?>, '<?php echo base_url() . 'webtv/delete_channels' ?>', '<?php echo current_full_url(); ?>');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
Please help me.
<?php
$value = "value_1"; //$value->id;
$baseurl = "base_url"."/webtv/delete_channels"; //base_url()."/webtv/delete_channels";
$fullurl = "current_url"; //current_full_url();
$delete="<a href='#' class='confirm' onclick=\"return delete_event('".$value."','".$baseurl."','".$fullurl."')\" >Click me</a>";
echo $delete;
?>
<script>
function delete_event(var1,var2,var3){
alert(var1+" -- "+var2+" -- "+var3);
}
</script>
You can use sprintf function to solve this issue.
Solution is:
$delete = sprintf('<a class="confirm" onclick="return delete_event(%d, \'%s\',\'%s\');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i>jkhklh</a>', $value->id, base_url() . 'webtv/delete_channels', current_full_url());
I cleaned up your php syntax. I don't quite understand what you're trying to do, but the declaration below should give you an a tag containing what you want (a confirm delete button):
$delete='<a class="confirm" onclick="return delete_event($value->id, '.base_url().'webtv/delete_channels, '.current_full_url().');" href="" ><i class="glyphicon glyphicon-trash text-red del" title="Delete"></i></a>';
If you echo this on your webpage, assuming all of your other links are correct, it should work.
I have this code on my php file for navbar:
<?php if(!$session->is_logged_in()) {
echo '
<a href="login.php" role="button" aria-expanded="false">
Login <span class="label"> login to system</span> </a>
</li>';}
else
{
echo '
<a href="#!" class="dropdown-toggle" role="button" aria-expanded="false">
' . $session->user_name; . '<span class="badge bg-default">2</span> <span class="caret"></span> <span class="label">it is you</span>
</a>';
}
?>
I check if the session is set using (!$session->is_logged_in())
If it is not set I should get a login button on navbar.
If the session is set, I should get his username ($session->user_name).
On my website I have a preloader (astral-gaming.com) but after inserting this code and uploading this file, every page which had included it, isn't shows.
It's just the preloader and it doesn't go to the page.
After deleting that code, everything is fine.
What I should do?
Remove the ; after$session->user_name;
OK, I know that I am being stupid about this, but I can't seem to figure out how I am being so.
I have variables for social media links in a separate .php file so that my client can change them without having to see too much code.
I include that .php file in my normal code where I want the links to be. But, for some reason the value of the variable is not being transmitted. I even tried to make a "global" variable. Here is my code.
File: socialMediaURLs.php
<?php
global $facebook = 'https://www.facebook.com/MenCoachingMen';
$googlePlus = 'https://plus.google.com/104275309033865331192/posts';
$twitter = 'https://twitter.com/MenCoachingMen';
$rss = 'http://mencoachingmen.org/category/podcast/feed/';
$vimeo = 'https://vimeo.com/mencoachingmen';
$youtube = 'https://www.youtube.com/channel/UCy_Pth5x-O7rcX9nMx1e8qw';
?>
File: socialLinks.php
<?php include './socialMediaURLs.php'; ?>
<div class="social_links_wrapper">
<a href="<?php echo $facebook;?>">
<div class="sl_facebook">
<i class="fa fa-facebook fa-5x"></i>
</div>
</a>
<a href="<?php echo $googlePlus;?>">
<div class="sl_googlePlus">
<i class="fa fa-google-plus fa-5x"></i>
</div>
</a>
<a href="<?php echo $twitter;?>">
<div class="sl_twitter">
<i class="fa fa-twitter fa-5x"></i>
</div>
</a>
<a href="<?php echo $rss;?>">
<div class="sl_rss">
<i class="fa fa-rss fa-5x"></i>
</div>
</a>
<a href="<?php echo $vimeo;?>">
<div class="sl_vimeo">
<i class="fa fa-vimeo-square fa-5x"></i>
</div>
</a>
<a href="<?php echo $youtube;?>">
<div class="sl_youtube">
<i class="fa fa-youtube fa-5x"></i>
</div>
</a>
</div>
I ran a test like so:
<?php
if($facebook){
echo $facebook;
}else{
echo 'Facebook NULL';
}
?>
I put this after the include statement and before the rest of the code. And it prints out 'Facebook NULL'. So, I know that the value is not being transmitted. Now, if I put in the included .php file (where the variables are stored) a echo "Hello World!"; line, "Hello World!" does print out on the screen. So, I know that the file is being included correctly (ie the path is correct).
I then placed this code after the 'include' and before the rest of the code:
$facebook = 'https://www.facebook.com/MenCoachingMen';
When I do that, the URL is included within the page. So, I know that my php statements within the code is correct. That means that it has to be a transmission of the value of the variable after including it. Please help. I know this must be a stupid mistake somewhere. Thank you.
A global variable must be used inside a function.
function name(){
global $globalvar = "some info";
}
to pass a variable OUT of it and into the script. You can't pass variables to other scripts using global variables. Consider using $_SESSION['name'] variables to pass variables from script to script.
http://php.net/manual/en/book.session.php
<?php include 'socialMediaURLs.php'; ?> <--- top main public_html/www
<?php include 'directory1/socialMediaURLs.php'; ?> <-- one folder deep
<?php include 'directory1/directory2/socialMediaURLS.php'; ?> <-- two folders deep
//calling a file FROM two folders deep TO A FILE IN the root folder!
<?php include('../../socialMediaURLS.php'; ?> <--- main index public_html/www
//ETC......
<?php include('../../directory1/socialMediaURLS.php'; ?>
I hope this helps. Think of it like DIR and CD commands.
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
}
?>