How to I fix this if/else tree? PHP - php

I am trying to program a sub-system for students on my website, I tried using these if/else statements to determine some outputs if the parameters match with the user information they have. So if a user is a student they can contact other people that are students and cant contact other users that are not students, and vice versa. The code I tried is below. The problem is that the code shows the error message and the message buttons at the same time whether or not the user is or is not a student. TIP: This PHP code is in a .phtml file, hence the amount of PHP opening and closing tags.
<?php if($dc['user']['student'] !== 0){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>
<?php if($dc['user']['student'] !== 1){ ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == false){ ?>
<div class="user-button user-follow-button"><?php echo Dc_GetFollowButton($dc['popover']['user_id']); ?></div>
<div class="user-button message-button"><?php echo Dc_GetMessageButton($dc['popover']['user_id']); ?></div>
<?php } ?>
<?php if(Dc_IsStudent($dc['popover']['user_id']) == true) { ?>
<?php echo $dc['lang']['student_contact_warning']; ?>
<?php } ?>
<?php } ?>

Do yourself a favour and start to use variables for repeated functions, and look up PHP's HereDoc to allow you to simplify your code. I think you'll agree the following is clearer.
The question now is what values you expect in your conditions. Run the following and check that the outputted IsStudent and Student are what you expect.
<?php
$Student=$dc['user']['student'];
$IsStudent=Dc_IsStudent($dc['popover']['user_id']);
$Dc_FollowBut=Dc_GetFollowButton($dc['popover']['user_id']);
$Dc_MessageBut=Dc_GetMessageButton($dc['popover']['user_id']);
echo"IsStudent: $IsStudent<br>";
echo"Student: $Student<br>";
if($Student!=0){
if($IsStudent==true){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
if($Student!=1){
if($IsStudent==false){
echo<<<EOC
<div class="user-button user-follow-button">$Dc_FollowBut</div>
<div class="user-button message-button">$Dc_MessageBut</div>
EOC;
}else{
echo $dc['lang']['student_contact_warning'];
}
}
?>

<?php
if($dc['user']['student'] != 0){
if(Dc_IsStudent($dc['popover']['user_id']) == true){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
} else if($dc['user']['student'] != 1){
if(Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">'. Dc_GetFollowButton($dc['popover']['user_id']) .'</div>';
echo '<div class="user-button message-button">'. Dc_GetMessageButton($dc['popover']['user_id']) .'</div>';
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Try putting it in one block, rather than 2 seperate blocks. If-else blocks are meant like that. I would prefer using cases here, though i tried cleaning this up a bit for you to see whats going on.
I agree with #jbes in the other awnser that you should use variables, to make things a lot easier to read. i didn't do this, but i totally agree on that part.
A typo can be made quickly if you constantly run only if blocks, with else, you can point to a different actions in the same if, without closing the complete block. Now I don't know if $dc['user']['student'] can only be 0 or 1, but else you can toss away the complete else-if statement and just change it to else to make it more cleaner. Also to check if not 0 leaves open for a lot of interpretation, what if the value is 2 or 48644. it would always trigger both blocks, so you may want to swap it around to check if the value equals 1 or 0.
I hope this points you in the right direction

I would first make the PHP code more clear. Then you will see that you didn't close some tags.
<?php
if ($dc['user']['student'] !== 0) {
if (Dc_IsStudent($dc['popover']['user_id']) == true) {
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton ($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
if ($dc['user']['student'] !== 1){
if (Dc_IsStudent($dc['popover']['user_id']) == false){
echo '<div class="user-button user-follow-button">';
echo Dc_GetFollowButton($dc['popover']['user_id']);
echo '</div>' . "\n";
echo '<div class="user-button message-button">';
echo Dc_GetMessageButton($dc['popover']['user_id']);
echo '</div>' . "\n";
} else {
echo $dc['lang']['student_contact_warning'];
}
}
?>
Now we can start analyzing the code.

Related

What is wrong with my if statement?

I had the below code which worked fine until I added an if statement to restrict the loop to only run on certain items in the array. I am now only getting a blank page which suggests there is an error somewhere in my code since adding the if statement, but I can't figure out where.
I'd really appreciate any help on solving this, as well as suggestions on how I could have solved myself (I'm still new to PHP and not sure how to effectively debug this type of issue).
Nb. There is an opening <?php tag not shown in the below snippet.
foreach ($portfolioProjects as $portfolio) {
if ($portfolio['featureContent'] == "Yes") {
?>
<div class="row featurette">
<?php
//for each odd number, change the layout so it looks nice :)
if ($loopCount % 2 == 0) {
echo '<div class="col-md-7">';
} else {
echo '<div class="col-md-7 col-md-push-5">';
}
?>
<h2 class="featurette-heading"><?php echo $portfolio[title]; ?> <span class="text-muted"><?php echo $portfolio[languages]; ?></span>
<?php
//Check array for newTag which will be added to show a tag to the user
if ($portfolio[newTag] == "Yes") {
echo '<span class="label label-success test pull-right"> New!</span>';
}
?></h2>
<p class="lead"><?php echo $portfolio[blurb]; ?></p>
</div><!--end of column1-->
<?php
if ($loopCount % 2 == 0) {
echo '<div class="col-md-5">';
} else {
echo '<div class="col-md-5 col-md-pull-7">';
}
?>
<img class="featurette-image img-responsive center-block" data-src="holder.js/200x200/auto" alt="200x200" src="assetts/200x200.gif" data-holder-rendered="true">
</div>
</div>
<?php
//if statement to stop divider being added if last item in array
$loopCount = $loopCount + 1;
if ($loopCount != $itemsCount) {
echo '<hr class="featurette-divider">';
}
}
}
?>
The easiest way to find out what the problem is, to check your webserver's log file, or turn on error_reporting and display_errors in your php.ini file.
Are newTag and blurb constants or keys in the $portfolio array? If they are keys, you should use apostrophes.
if ($portfolio['newTag'] == "Yes") {
and
<p class="lead"><?php echo $portfolio['blurb']; ?></p>
You forgot to wrap array keys in quotes
`$portfolio['title']
$portfolio['languages']
$portfolio['newTag']
$portfolio['blurb']`

PHP MYSQL <?php echo $row[$variable]; ?>

I have the following code that does what I want it to do, but the ones with the backslashes <?php echo $row[\'t_id\']; ?>, the <?php echo $row[\'t_type\']; ?> and the <?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?> do not output their respective variable.
Instead, their output is literal like
view_t_profile.php?tutor_id=<?php echo $row['t_id']; ?>&t_type=<?php echo $row['t_type']; ?>
The <?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?> doesn't show up on the web page like the previous code, but its in the code italicized and in red text. I'm not a programmer/coder, so if someone can correct my code, I sure would appreciate it. Usually, I can figure it out, but on this one, I cannot.
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF"><?php echo ucfirst$row[\'t_fn\']).\' \'.ucfirst($row[\'t_ln\']); ?></font></strong></center>';
}
?>
You don't use <?php echo ... ?> when you're already in PHP mode and echoing something. Just concatenate the variable.
echo '<center><strong><font color="#3BB9FF">' . ucfirst($row['t_fn']) . ' ' . ucfirst($row['t_ln']) . '</font></strong></center>';
<?php echo ... ?> is used when you're just outputting HTML directly, and you want to insert a bit of PHP. For instance, like this:
if($row['t_type'] == 1)
{ ?>
<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>
<center><strong>Main Contact</strong></center>
<center><strong><font color="#3BB9FF"><?php echo ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']); ?></font></strong></center>
<?php
}
Try this:
<?php
if($row['t_type'] == 1)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Contact</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
if($row['t_type'] == 0)
{
echo '<center><strong><font color="#3BB9FF"><br />View Profile</font></strong></center>';
echo '<center><strong>Main Sponsor</strong></center>';
echo '<center><strong><font color="#3BB9FF">'.ucfirst$row['t_fn']).' '.ucfirst($row['t_ln']).'</font></strong></center>';
}
?>
When you open <?php once, you can't use this tags again, before you close with ?> tag.

Multiple if else

How can i show some default code only if BOTH if statements are false ?
I've got this code -
<?php if( get_sub_field('cta_phone')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_phone');?>"><?php the_sub_field('cta_phone');?></a></span>
<?php else: ?><?php endif; ?>
<?php if( get_sub_field('cta_mobile')): ?>
<a class="phone" href="tel:<?php the_sub_field('cta_mobile');?>"><?php the_sub_field('cta_mobile');?></a></span>
<?php else: ?><?php endif; ?>
<?php else: ?>
<img src="http://my-domain/image.jpg">
<?php endif; ?>
i'm not using the else bit at the end at the moment because i only want that to show if both 'if's are false ?
hope that it makes sense
You can use 1 php tag only to make your syntax much more readable and
avoiding
multiple <?php ?> tags.
you can also use echo to print your html markup using php scripting like this.
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a></span>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a></span>';
} else {
//do what you want to do here, if they are false.
}
Ok thanks , the 'else' works if neither of the if's are true , but if i have one that is true or both are true , it only displays the first entry but shows it twice ?
<?php
if( get_sub_field('cta_phone')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_phone').'">'.the_sub_field('cta_phone').'</a>';
} else if (get_sub_field('cta_mobile')){
echo '<a class="phone" href="tel:'.the_sub_field('cta_mobile').'">'.the_sub_field('cta_mobile').'</a>';
} else {
echo '<img src="http://placehold.it/350x150">';
}
?>

Div not showing in wordpress

<?php
if (is_user_logged_in()) {
echo '<div id="signin-box"> ' . wp_login_form() . ' </div>';
} else {
echo 'hi';
}
?>
Is what I've got. The login form is working, but it is not being wrapped in the div. Without the else/if statement, it works.
Does this work for you?
<?php
if (is_user_logged_in()) {
?>
<div id="signin-box">
<?= wp_login_form(); ?>
</div>
<?php
} else {
echo 'hi';
}
?>
http://codex.wordpress.org/Function_Reference/is_user_logged_in states it returns true if logged in and false if not.
You are probably looking for:
if(!is_user_logged_in()){
echo '<div id="signin-box">' . wp_login_form(array('echo' => false)) . '</div>';
}else{
echo 'hi';
}
Adding ! inverses true to false so if user is not logged in show login form else say hi. Also adding array('echo' => false) will give a return instead of echoing, removing array('echo' => false) you'll have to put wp_login_form() on its own line without a echo:
if(!is_user_logged_in()){
echo '<div id="signin-box">';
wp_login_form();
echo '</div>';
}else{
echo 'hi';
}
You want to add this code it seems work like this please add this code and check it.
<?php
if (is_user_logged_in()) { ?>
<div id="signin-box" class="test"><?php wp_login_form()?> </div>
<?php } else {
echo 'hi';
}
?>
In your code "echo" is not working in login form ..

If statement to not show div / h2

This for each / if statement displays changes, and then right below it it displays the changes made.
I am trying to write an if statement that tells it not to show the h2 and the #change_box if there are no changes.
Help would be greatly appreciated.
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<? foreach ($audit['Events'] as $event):?>
<?if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?= $event['Field']?>
</span>:
<?= $event['Value'] ?>
<?=($event['Previous'])?>
<?endif?>
<?endforeach?>
</div>
<?php
if ($changes) { // You'll have to set this variable
//Echo all of your HTML
else {
//Echo the stuff you'd rather show if they didn't change anything
}
?>
To give you an idea of how I'd write your code
<?php
if ($changes) {
echo '<h2 class="changes">Changes:</h2>';
echo '<div id="change_box">';
foreach ($audit['Events'] as $event) {
if ($event['Type'] != 'Comment') {
echo $event['Field'] . </span> . $event['Value'] . $event['Previous'];
}
}
echo "</div>"
}
?>
You could wrap your code with an if like
if(count($audit['Events'])>0)
{
//Your code
}
Wny do you open and close php tags every time? Would be better to make everything in PHP and echo whenever you want to print HTML code.
<?php
echo "<h2 class=\"changes\"> Changes: </h2>";
echo "<div id=\"change_box\">";
foreach ($audit['Events'] as $event) {
if ( $event['Type'] != 'Comment') {
echo "<span class=\"field\">".$event['Field']."</span>";
echo $event['Value'];
echo "(".$event['Previous'] .")";
}
}
echo "</div>";
?>
please make space after each <? and make sure you enabled short tags
<?php if(is_array($audit['Events']) && $audit['Events']):?>
<h2 class="changes"> Changes: </h2>
<div id="change_box">
<?php foreach ($audit['Events'] as $event):?>
<?php if ( $event['Type'] != 'Comment'):?>
<span class="field">
<?php echo $event['Field'];?>
</span>:
<?php echo $event['Value']; ?>
<?php echo $event['Previous'];?>
<?php endif;?>
<?php endforeach;endif;?>
</div>

Categories