PHP elseif statement inside of a foreach loop [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
Hello there I have a php foreach loop running that is all working except one part. I am pulling in data from a local json file. The beginning foreach call looks like this:
<?php foreach($boatslips as $boatslip): ?>
One of the json fields is: "rent_sale_or_both" : "rent" (but that field can be rent, sale, or both,)
Lower down in the code after some other fields are successfully rendered and inside the foreach loop still i'm trying the below code to render out whether a boat slip is for rent, sale or both; and wanting to then be able to style 'rent' 'sale' or 'both' with css eventually.
<?php
if ($boatslip->rent_sale_or_both == rent) {
echo "rent";
}elseif ($boatslip->rent_sale_or_both == sale) {
echo "sale";
}else {
echo "both";
}
?>
Then some more html after the elseif.
At the end of the for each loop it is closed off properly with <?php endforeach; ?>
Obviously this is probably not correct? I think I'm making it harder than it is?
Any thoughts?

You might be missing a couple of quotation marks.
$boatslip->rent_sale_or_both == "rent"
$boatslip->rent_sale_or_both == "sale"

Related

If a variable is something and another variable is something else do, PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm doing some PHP traning, and wanted to make my own copy of "Rock, Paper and Scissors" and then I'd like to compare the results from the user and the computer, and I tried with this code:"
if($choice = 'Rock' and $pcSelect == 'Scissors'){
echo "You win!";
}
Plz help me,
Magn0053
You are assigning 'Rock' to $choice with =. You need to test equality with == like in the second conditional.
Use && instead of and in your if
if($choice == 'Rock' && $pcSelect == 'Scissors'){
echo "You win!";
}

PHP if statement breaks webpage [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I didn't see an answer in a quick search, so I decided make a new one, I am on the checkout_shipping.php in OScommerce 2.3.4 and I am adding an if statement so that the value of $0.00 is the text "free" here is my code, it is incomplete because it is crashing the page, here is the code that I modified.
<?php
if ('cost' > 0) {
?>
<td><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'],
(isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
<php
} ?>
fixed, it was syntax error; also cost wasn't a value, so I changed it to $i
Actually you are comparing 0 to a string, what you need to do is compare zero to a variable like so:
<?php
$count = 1;
if($count > 0) {
//Do your logic here
}
The first issue is your use of <php instead of <?php. But also the syntax of if ('cost' > 0) { makes no sense at all. So assuming cost is actually a variable named $cost, then this should work:
if ($cost > 0) {
?>
<td><?php echo $currencies->format(tep_add_tax($quotes[$i]['methods'][$j]['cost'],
(isset($quotes[$i]['tax']) ? $quotes[$i]['tax'] : 0))); ?></td>
<?php
} ?>

php - inline font style for print statement? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I know its possible to apply inline styles to echo statements but can this be done with print statements as well?
In my pagination I have links to previous and next records using the greater and less than symbols <> and a running count of the current record against the total number of records. e.g.
<
1/2
or
>
2/2
I have styled them in css but want to decrease the size of the count only. If I make changes to the css the font size for the previous and next links and count all change, I only want to target the count.
<div class="nextcard"><?php if($nextlink != ""){ print ($nextlink."<br/>".$next."/".$count); } ?></div>
I have tried :
<div class="nextcard"><?php if($nextlink != ""){ print 'style=font:50px' ($nextlink."<br/>".$next."/".$count); } ?></div>
But get syntax errors.
One approach would be to add a class rather than inline styles. However, you have forgotten to append the string correctly.
Here is what I would personally do: (Note I replaced print with echo, TBH, there would be no different)
<div class="nextcard">
<?php
if($nextlink != "")
echo '<span class="mark">'.($nextlink."<br/>".$next."/".$count).'</span>';
?>
</div>
If you still want to style it inline, you should simply do:
<div class="nextcard">
<?php
if($nextlink != "")
echo '<span style="font-size:50px;">'.($nextlink."<br/>".$next."/".$count).'</span>';
?>
</div>

PHP if x then image, else other image (so close) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It's a basic question, but I've googled all sorts of variations of it & nothing that quite answers me. I want to display one of two images. One if the criteria is met & one if it is not met. This code works. If criteria is met, it does display the image. But there is no alternative
<? if(stripslashes($getfeedbackQryRow['CompanyID'])=='344'){?><img src='img1.png' alt='Todays Bite'><? }?>
Then I put this together, but it's still wrong
<?PHP
if ($getfeedbackQryRow['CompanyID']) == '344') {
print ("<IMG SRC =/img1.png>");
}
else {
print ("<IMG SRC =img2.png>");
}
?>
This one looks like it should be right, but it throws an error... It's probably a syntax issue. Does anyone know what I'm doing wrong?
You have an extra ")"
if ($getfeedbackQryRow['CompanyID']) == '344') {
Remove the ")" here ['CompanyID'])
if ($getfeedbackQryRow['CompanyID']) == '344') {
// ^------- this is causing your error
You should do something simple like this:
$image_name = $getfeedbackQryRow['CompanyId'] == '344' ? 'img1.png' : 'img2.png';
echo '<img src="' . $image_name . '">';

Making two PHP echo statements do some math [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
First of all sorry for the most likely simple question. I have two echo statements from woocommerce that I need to subtract from each other.
the first is
<?php echo $woocommerce->cart->get_discounts_before_tax(); ?>
This gives me the amount of the coupon discount.
The second is
<?php echo $woocommerce->cart->get_cart_subtotal(); ?>
This gives me the total off all things in the cart.
I need to subtract the coupon discount from the cart subtotal. So I set this up
<?php
$first_number = $woocommerce->cart->get_cart_subtotal ;
$second_number = $woocommerce->cart->get_discounts_before_tax ;
$sum_total = $first_number - $second_number;
print ($sum_total);
?>
but obviously the syntax is not right and I just get 0. Please help me with some simple math!
I guess you've missed ()
$first_number = $woocommerce->cart->get_cart_subtotal();
$second_number = $woocommerce->cart->get_discounts_before_tax();
Without parentheses, you're trying to access a property; but not calling the existent method itself.
Obviously one or both of the function returns are non-integers or 0. Find the return of each and change it to the appropriate property or function. Try this public property for the first number.
$first_number = $woocommerce->cart->subtotal;

Categories