If variable is link, echo link - php

I having issues getting a function to echo, where $lightbox_link1 = get_custom_field('lightbox_link1'). I'm fairly new to PHP.
Below is the defining function:
// Check for a lightbox link, if it exists, use that as the value.
// If it doesn't, use the featured image URL from above.
if(get_custom_field('lightbox_link1')) {
$lightbox_link1 = get_custom_field('lightbox_link1');
} else {
$lightbox_link1 = $image_full[0];
}
Echo Function:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '';
} ?>

<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
should be
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
= is used for assignment
== is used for comparison
=== is used for typesafe comparison
also you can't declare <?php ... ?> inside another <?php ... ?>
to get something like <?php ... <?php ... ?> ... ?>
take a look at what you did up to here:
<?php if ($lightbox_link1 = get_custom_field('lightbox_link1')) {
echo '<a href="<?php
Instead, using doublequotes in your echo statement will allow for the php variables inside to be parsed, so you could just do
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
to get
<?php if ($lightbox_link1 == get_custom_field('lightbox_link1')) {
echo "<a href='{$lightbox_link1}' data-rel='prettyPhoto[{$post_slug}]'></a>";
} ?>

Related

Dynamic variable in PHP [GET]

I'm trying to use the php get. everthing is working find exept the include page.
it's adding 1 at the end of include content
any ide how do i ride that?
example:
I love norway
I love norway1
Code:
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') $norge = include('norway.php');
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
We should use file_get_contents, instead of include.
$norge = file_get_contents("norway.php");
Use the output buffer functions to capture the output from the included script.
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') {
ob_start();
include("norway.php");
$norge = ob_get_clean();
}
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>

If session has value echo it else echo "all"

Update 2
I'll just leave this here for future references. But this is the solution I created with all the help I got here. Thanks!
<script>
function Refresh() {
location.reload();
}
</script>
<?php $number = $_SESSION["page_id"]; ?>
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]');
session_destroy();
echo ('<button class="btn btn-0001" onclick="Refresh()">Show All</button>');
}
else{ echo do_shortcode('[RICH_REVIEWS_SHOW num="all"]'); }
;
?>
Update
So I'm trying to just do a simple echo to see if the session is set using this code:
<?php if(isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo 'Set and not empty, and no undefined index error!');
};?>
But doing this breaks my page, I just get a blank page? How do I check if the session is set? When I do a echo of the session using this code:
<?php echo $_SESSION["page_id"]; ?>
It does output the correct session value?? What am I doing wrong?
I have a sessions saved with PHP and I'm using this so that the page ID from Wordpress is echo-ed in a shortcode do_shortcode('');
This is what my code looks like:
<?php $number = $_SESSION["page_id"]; ?>
<?php echo do_shortcode('[RICH_REVIEWS_SHOW category="page" num="all" id="'. $number .'"]'); ?>
<?php echo do_shortcode_all('[RICH_REVIEWS_SHOW category="page" num="all" id="all"]'); ?>
<?php echo $shortcode ;?>
<?php echo $shortcode_all ;?>
Now, what I would like to do is IF the page_id is not stored in the session it should echo all. So how do I go about this?
I found this code, and I think its something I need... But I'm not that great a programmer/coder...
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>
So, if I where to put these two together I would get something like this
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number)) {
echo $shortcode_all ;
}
// Evaluates as true because $var is set
if (isset($number)) {
echo $shortcode ;
}
?>
Am I in the right direction?
Solution
<?php
if (isset($_SESSION['page_id']) && !empty($_SESSION['page_id'])) {
echo('Set and not empty, and no undefined index error!');
};
?>
What am I supposed to feel from the missing (?
If you are using wordpress then you should use the session_start(); in wp-config.php file so please first put in you wp-config.php at top and then check.
<?php
$number = $_SESSION["page_id"];
// Evaluates to true because $var is empty
if (empty($number) || $number=='') {
echo $shortcode_all ;
}else{
echo $shortcode ;
}
?>

Switching from "if" to "elseif" breaks code

I'm using multiple if statements to check a containing div, and output an image based on the container name. The code was working fine until I add a final "else" or change the if's out to elseif and I can't figure out why that's happening. When I try to add the else or elseif, the entire page fails to load. Any idea why this is happening?
<?php
if($viewMore['container_type'] == 'Large IMG' || $viewMore['container_type'] == 'Gallery') {
$photoSql = "SELECT * FROM `cms_uploads` WHERE (`tableName`='site_content' AND `recordNum` = '".$viewMore['num']."' AND `fieldname`= 'large_images') ORDER BY `order`";
$photoResult = $database->query($photoSql);
$photoResultNum = $database->num_rows($photoResult);
$photoArray = array();
while($photoResultRow = $database->fetch_array($photoResult)) {
array_push($photoArray, $photoResultRow);
}
$large = 0; foreach ($photoArray as $photo => $upload): if (++$large == 2) break;
?>
<img class="features" src="<?php echo $upload['urlPath'] ?>">
<?php endforeach ?>
<?php } ?>
<?php
elseif($viewMore['container_type'] == 'Medium IMG') {
$photoSql = "SELECT * FROM `cms_uploads` WHERE (`tableName`='site_content' AND `recordNum` = '".$viewMore['num']."' AND `fieldname`= 'small_images') ORDER BY `order`";
$photoResult = $database->query($photoSql);
$photoResultNum = $database->num_rows($photoResult);
$photoArray = array();
while($photoResultRow = $database->fetch_array($photoResult)) {
array_push($photoArray, $photoResultRow);
}
$medium = 0; foreach ($photoArray as $photo => $upload): if (++$medium == 2) break;
?>
<img class="features" src="<?php echo $upload['urlPath'] ?>">
<?php endforeach; ?>
<?php } ?>
<?php else { ?> SOMETHING HERE <?php } ?>
EDIT:
Other notes
I've tried wrapping the break; in brackets because I thought that piece following the count might be messing with something. Also removing the counter altogether or adding a semi colon after the endforeach didn't help.
Whenever you close your PHP block, think about all the text/HTML outside it being put into PHP's echo function.
What gave me alarm bells was this part:
<?php } ?>
<?php else { ?> ...
What that translates into is:
if (...) {
} echo "[whitespace]"; else {
}
which clearly makes your else block unexpected.
You should not close the PHP block between your closing if and opening else, i.e. do this instead:
...
} else {
...

PHP if/else statement

How can I write the following statement in PHP:
If body ID = "home" then insert some html, e.g.
<h1>I am home!</h1>
Otherwise, insert this html:
<p>I'm not home.</p>
Doing it with native PHP templating:
<?php if ($bodyID==='home') { ?>
<h1>I am home!</h1>
<?php } else { ?>
<p>I'm not home!</p>
<?php } ?>
You can try using this :
$html = '';
if ( $body_id === 'home' )
{
$html .= '<h1>I am home!</h1>';
}
else
{
$html .= '<p>I\'m not home.</p>';
}
echo $html;
This will echo the html code depending on the $body_id variable and what it contains.
You can use a switch command like so:
switch($body)
{
case 'home': //$body == 'home' ?
echo '<h1>I am home!</h1>';
break;
case 'not_home':
default:
echo '<p>I'm not home.</p>';
break;
}
The default means that if $body does not match any case values, then that will be used, the default is optional.
Another way is as you say, if/else statements, but if within template / view pages you should try and use like so:
<?php if ($body == 'home'):?>
<h1>I am home!</h1>
<?php else:?>
<p>I'm not home!</p>
<?php endif; ?>
Assuming $bodyID is a variable:
<?php
if ($bodyID==='home') {
echo "<h1>I am home!</h1>";}
else {
echo "<p>I'm not home!</p>";}
?>
Personally I think that the best way to do that without refreshing and without having to set a variable (like $body or something like that) is to use a javascript code, this because "communications" between JS & PHP is a one-way communication.
<script language="javascript">
<!--
if( document.body.id === "home" ){
window.document.write("<h1>I am home!</h1>") ;
}
else{
window.document.write("<p>I'm not home!</p>") ;
}
-->
</script>
otherwise you can build a form and then take the body.id value using $_GET function... It always depends on what you've to do after you now body.id value.
Hope this will be usefull & clear.
you can try in the following way:
$body_id = "home";
if ($body_id == "home") {
echo "I am home!";
} else {
echo "I am not home!";
}
or
$body_id = "home";
if (strcmp($body_id, "home") !== 0) {
echo 'I am not home!';
}
else {
echo 'I am home!';
}
Reference:
https://www.geeksforgeeks.org/string-comparison-using-vs-strcmp-in-php/

If-statement: how to pull 2nd GET variable

How do I get this to pull my 2nd variable? (I already have a switch setup)
<body id="<?php if (! isset($_GET['page'])) { echo "home"; } else { $_GET['page']; echo $page; } ?>">
I have a switch statement that pulls the pages from
index.php?page=#####
and I have just added this part to my switch:
index.php?page=####&section=#####
Right now, if I am on page=photos, my code ends up being:
<body id="photos">
I need to make it so that if any link has the "sections" variable on it like this page=photos&section=cars it uses the same ID:
<body id="photos">
First of all, a HTML element can only have one id. So if you want to create a hybrid (e.g. page-section) you can do something like this:
<body id="<?php echo isset($_GET['page']) ? $_GET['page'] : "home"; echo isset($_GET['section']) ? ("-".$_GET['section']) : ''; ?>">
For more information on Ternary Operators in PHP (the ? and : I used in the echo statement) see http://php.net/manual/en/language.operators.comparison.php
I am not entirely sure I understand your question, but where you're doing:
$_GET['page']; echo $page;
What do you think is happening? You're echoing a variable that has no definition. If you want to echo the value passed in the url, just do:
echo $_GET['page'];
GET doesnt mean your getting the varible, its the method by which the variable was passed to he page. The possible methods are get (in the url) or post (not).
Wouldn't that be an if to find out it if the section was defined? i.e.
if(isset($_GET['section'])){
//create div
} elseif(isset($_GET['page']){
//create fallback div
}
Move the PHP code outside the body's id attribute for readability, and use else if. Make sure your code isn't vulnerable to injection by sanitizing or validating input from $_GET. For example:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID = $_GET['section'];
} else if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
?>
...
<body id="<?php echo $bodyID; ?>">
Alternatively,
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
$bodyID='home';
foreach (array('section', 'home') as $key) {
if (isset($_GET[$key]) && isValidID($_GET[$key])) {
$bodyID = $_GET[$key];
break;
}
}
?>
...
<body id="<?php echo $bodyID; ?>">
In this case, I'd use the first, unrolled version. If you had to check more input keys, use the loop-based approach.
If you decide you want both page & section in the ID, you can try something like:
<?php
function isValidID($x) {
return preg_match('/^[A-Z][-_.A-Za-z0-9]$/i', $x);
}
if (isset($_GET['page']) && isValidID($_GET['page'])) {
$bodyID = $_GET['page'];
} else {
$bodyID = 'home';
}
if (isset($_GET['section']) && isValidID($_GET['section'])) {
$bodyID .= '_' . $_GET['section'];
}
?>
...
<body id="<?php echo $bodyID; ?>">

Categories