I am working in a Drupal template. I want to know if two variables exist, and if they do not then do or show something else. I know how to do this for only one variable, but what is the correct syntax to look for two? My code is below.
<?php if (!empty($right) && !empty($left)): ?>
<div id="content-main">
<?php endif; ?>
I also tried it this way.
<?php if (!empty($right)&&($left)): ?>
<div id="content-main">
<?php endif; ?>
and this way.
<?php if (!isset($right)&&($left)): ?>
<div id="content-main">
<?php endif; ?>
None of them work, how do I fix this?
empty() doesn't check variable initialization, it only checks if it contains a defined set of values that are considered empty ( "", 0, for example).
Your third example is in the right direction, but needs a small tweak:
<?php if (!isset($right)&&!isset($left)): ?>
<div id="content-main">
<?php endif; ?>
The second conditional check after && requires its own isset() as well.
hope this helps.
Related
I have an if statement which is throwing a syntax error, unexpected T_VARIABLE error and I have no idea why.
The code:
<?php if($contentTop || $contentLeft || $contentCenter || $contentRight): ?>
<div>a bunch of html</div>
<?php endif; ?>
The variable contains eiter an empty string or a bunch of html.
I have tried changing the || to or but it doesn't seem to change anything. neither does opening and closing the if statement with curly brackets. It only seems to work when i only have one variable in the statement. I have also tried using !empty($contentTop) to check the variables.
The server is running PHP version 5.3.10-1ubuntu3.26 but as far as I can see, that should not change anything regarding if statements
EDIT:
On request of the full markup:
<?php
$contentTop = get_field('section2new_content');
$contentLeft = get_field('section2new_left');
$contentCenter = get_field('section2new_center');
$contentRight = get_field('section2new_right');
if( $contentTop || $contentLeft || $contentCenter || $contentRight ): ?>
<div class="bbh-inner-section teaser-3-col" id="section2temp">
<?php if($contentTop): ?>
<div class="grid-container top">
<div class="row">
<div class="col-sm-12">
<?php echo $contentTop; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if($contentLeft || $contentRight || $contentCenter): ?>
<div class="grid-container bottom">
<div class="row">
<div class="col-sm-4 left">
<?php echo $contentLeft; ?>
</div>
<div class="col-sm-4 center">
<?php echo $contentCenter; ?>
</div>
<div class="col-sm-4 right">
<?php echo $contentRight; ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
As i noticed myself i have second if / || statement further down, which seems to work fine. It's also not that I mistakenly inserted a wrong type of space or whateever, I have manually rewritten the code multiple times
I tried #Poiz answer which worked. Being a bit suspicious i tried my own original code again, which now also worked. Human error once again. Maybe it was some illegal version of a space or tab.
Would you be open to trying something a little odd? Anyways, it is essentially the same thing you did except that we turned the if Statements into Ternary Operations. This is because like You said; you've tried almost all combinations of if without success, except when only one Condition is supplied.... The Idea here is simply to crunch down the Line that does the main Comparison to a single, simple Boolean Value, which can then be used within the Subsequent if. Perhaps this works for you since your Magical if works only in the Case you have but One Condition to test for...
<?php $state = ($contentTop || $contentLeft || $contentCenter || $contentRight) ? true : false; ?>
<?php if($state): ?>
<div>a bunch of html</div>
<?php endif; ?>
I was referring the page.tpl.php(Drupal 7 theme) for understanding the code. I found the following code,
<?php if ($site_name || $site_slogan): ?>
<!-- !Site name and Slogan -->
<div<?php print $hgroup_attributes; ?>>
<?php if ($site_name): ?>
<h1<?php print $site_name_attributes; ?>><?php print $site_name; ?></h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<h2<?php print $site_slogan_attributes; ?>><?php print $site_slogan; ?></h2>
<?php endif; ?>
</div>
<?php endif; ?>
Can you see the code in third line, <div<?php print $hgroup_attributes; ?>> WHY the php code is inside the first div tag of html? Same thing in later part of code also, as you can see h1 and h2 code. So, what is this convention of combining the html and php in so complicated way? and how should I read that?
Combining HTML and PHP code in Drupal templates is actually a very strong feature. In this case, $hgroup_attributes will probably contain some classes that style the div. Printing it in the template results in something like
<div class="SOME_CLASSES"> ... </div>
If you're further interested in the variable $hgroup_attributes, you can inspect by pasting <?php dpm($hgroup_attributes); ?> in your template file after you've installed the Devel module.
I need help writing a php echo that checks if two cells are the same in two tables.
The cells are:
posts>id and reblogs>newid
I have the following code to start with:
<?php if(!empty($reblogDetails->newid)): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
which I borrowed from another snippet but isn't working.
can anyone help?
Your question is not completely clear, but I am assuming you want to show the table with an anchor tag only if the posts->id is equal to the reblogDetails->newid? If that is the case the code you would want would be more like the below.
<?php if(!empty($reblogDetails->newid) && $posts->id == $reblogDetails->newid): ?>
<table><tr><td>
<?php echo $newid;?>
</td><tr></table>
<?php else: ?>
original upload
<?php endif ?>
That being said I doubt your anchor tag is doing what you want. I would anticipate it creating an anchor that looks something like
12
I'm very newbie regarding PHP, I'm trying to configure my wordpress Loop.
I figured out how to display my customfield in my template, I manually added a title before my taxonomy and custom fields, but I'd like it doesn't show if the taxonomy is empty.
Here is the code:
<div class="customfields">
<h2 class="widgettitle">My Title</h2>
<?php echo do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
I would very much appreciate your help!
Thanks a ton,
T.
So code should be
<?php $taxonomy = do_shortcode('[tax id="agences" before="Agences : " separator=", " after=""]'); ?>
<div class="customfields">
<?php if(!empty($taxonomy)) : ?>
<h2 class="widgettitle">My Title</h2>
<?php echo $taxonomy; ?>
<?php endif; ?>
<?php echo do_shortcode('[custom_fields_block]'); ?>
</div>
If you want to show content in HTML only if certain variables contain a value then you can create a simple if statement that uses PHP's isset function to determine whether or not the variables are set, and if true place some HTML on the page. You can read more about isset here.
can somebody help me with this if statement?
My form appears every-time I load the page but it disappears after I submit the form!
Maybe it's the "endif" syntax that confuses me but I can't get this done properly...
here is the code:
<?php
if ($this->input->post('submit') && $this->input->post('categories')):
foreach($tagedImages as $image):
?>
<div class="thumb">
<?php echo'<a href="/toKoritsi/uploads/'.$image.'"/> <img src="/toKoritsi/uploads/thumbs/'.$image.'"/></a>' ?>
</div>
<?php
endforeach;
elseif(isset($photosQuery) && count($photosQuery)):
foreach($photosQuery->result_array() as $photo):
?>
<div class="thumb">
<?php echo'<a href="/toKoritsi/uploads/'.$photo['name'].'"/> <img src="/toKoritsi/uploads/thumbs/'.$photo['name'].'"/></a>' ?>
</div>
<?php
endforeach;
endif;
$options = array(
'bracelet' => 'Bracelets',
'ring' => 'Rings',
'bag' => 'Bags',
'earing' => 'Earings'
);
echo form_open("toKoritsi/gallery");
echo form_dropdown('categories', $options, 'bracelet');
echo form_submit('submit', 'Choose');
echo form_close();
?>
thanks in advance!
Should foreach($tagedImages as $image) be foreach($taggedImages as $image)?
The formatting of your code is very ugly. If you are intent on getting nicely-formatted HTML output, perhaps you should consider using Tidy, or a template engine that offers that functionality.
I donĀ“t know what the url structure of your site looks like, but are you sure you are submitting the form to the right place?
According to the CI user guide, it gets submitted to something like /index.php/toKoritsi/gallery but that depends on your config preferences.
You definitely need to format your code properly.
I'd also recommend trying to consolidate your PHP into blocks, and keep it separate from your HTML.
You can do all the conditionals and echos WITHIN some of those HTML elements:
<div>
<?php /*...*/ ?>
</div>
as opposed to what you're doing now:
<?php /*...*/ ?>
<div>
<?php /*...*/ ?>
</div>
<?php /*...*/ ?>
In addition, things like these blocks make no sense:
<?php endforeach;?>
<?php endif;?>
Even if you had to break your code into small chunks, wouldn't it be easier to say:
<?php
endforeach;
endif;
?>
EDIT: This answer was provided before the question source was reformatted and cleaned up. I was unable to even address the original issue until I could better see what was going on.
This might sound kind of dumb, but are you sure that your gallery controller is behaving correctly? If you're submitting and there's an error in one of your CodeIgniter functions, it could be messing up the rendering after the submit.