php for each loop find lowest value - php

There are several options for one item. I.e. Item1 --> Option1: black, white, pink Option2: Small, Medium, Large --> each option is either available(1) or not available(0).
So if someone chooses Item1 with color black and size small the status should be returned based on availability.
My code is the following:
foreach ($item['option'] as $option) {
if ($option['availability']=0) { echo 'Not available'; } else { echo 'Available'; } }
So far so good. My problem is that I only want to print the status message only once, because the item with the configuration is either available or unavailable. With the above code I get twice the echo 'Not available' if both options in the example above are unavailable. I only would like to print :Not available' only once even if several options are unavailable. How can that be achieved?
I hope my question/problem is clear. Thank you for your help!

Use '==' because you are comparing the values and you can use break to stop the loop.
foreach ($item['option'] as $option) {
if ($option['availability']==0) {
echo 'Not available';
break;
} else {
echo 'Available';
break;
}
}

You can break; the loop or just exit it, exit;.
(This had to be a answer as i am not able to put it as a comment*

Related

PHP if/then statement

Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.
Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.

if column, row = something, echo something else echo error

How do I use php so it tests if a value in a mysql table is equal to a string? I am making a bans list and theres several categories, mutes, warnings and bans. The problem at the moment is they all display in every category so bans, mutes, warnings display in bans, mutes and warnings. They aren't sorted. I tried to fix it by using
<td>
<?php
if($row['punishmentType'] == 'BAN') {
echo "<img src='https://mcapi.ca/avatar/2d/" . $row['name'] . "/25' style='margin-bottom:5px;margin-right:5px;border-radius:2px;' />" . $row['name'];
}
else {
echo 'ERROR'
}
?>
</td>
But it comes up with ERROR. In the mysql table, theres 3 punishment types, BAN, MUTE and WARNING. Can someone tell me whats wrong with the code above? And how I fix it.
First of all you should check if $row['punishmentType'] has content. Then you can use a switch statement to separate your logic according to the type of punishment.
You could do:
<?php
if(!empty($row['punishmentType'])) {
switch($row['punishmentType']) {
case 'BAN': ...
break;
case 'MUTE': ...
break;
case 'WARNING': ...
break;
}

PHP Auto-highlight cell depending on data from database

I am working on a PHP application and stumbled upon a problem on auto-highlighting.. I want to ask how to auto-highlight a cell of a table, whose data came from a database, after checking if the value is negative, 0, or positive, Red for negative, yellow for 0 and green for positive.
Please note that I have no experience regarding JavaScript or Ajax and rudimentary knowledge only on CSS. Thank you.
If needed, I can post any part of my code here.
It might be helpful for you:
function getSampleStatus($sampleid){
if($sampleid==1){
$color="#007334";
}elseif($sampleid==2){
$color="#3f96e8";
}elseif($sampleid==3){
$color="#ff9900";
}elseif($sampleid==4){
$color="#ff9770";
}
else{
$color="#ff0000";
}
$query='Select status from config where status_id='.$sampleid;
$this->_db->setQuery( $query );
$status =$this->_db->loadResult();
return "<span style='color:".$color.";padding-left:200px;'>".$status."</span>";
}
This function simply adds different colors depending on sample status.
For example:
For disapproved records, color red.
For Approved records, color green.
For Lab completed records, color yellow and so on..
Edit:
For Simplicity, try this:
$yourdatafromdatabase = 3; //which is either -ve, zero or positive
if($yourdatafromdatabase < 0){
$color="#FF0000"; //red for less than zero
}elseif($yourdatafromdatabase== 0){
$color="#FFFF00"; //yellow for zero
}
else{
$color="#00FF00"; //green for positive
}
echo "<span style=\"color: $color\"> <h1> Wow Color! </h1></span>";
?>
Working Demo:>>
just write css in table tag e.g
if ($value <0)
{echo "<td bgcolor=\"#FF0000\">$value</td>";}

While statement repeats infinitely

I have a website where i need to use a while statement, but when i use it, it repeats the echo infinitely. Although it looks like i could make it work without while, that isnt so, this is a simplified version of a final product that will need while.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Why is it echoing either contact webmaster or you didnt select purple an infinite number of times?
First, you probably need to change:
$passlevel == '1';
to
$passlevel = '1';
The first is a comparison equals, not an assignment equals.
Second, if $color is not #800080, then the loop does not terminate and thus repeats forever as nothing in the loop causes the value to change.
I'm not entirely sure of the point of this loop in the first place. It should work perfectly fine without the loop, however you've stated that your code is a simplified version of something more complicated that indeed needs a loop. Perhaps you can elaborate.
You're not providing any way out of the loop. If $_GET['box_1_color'] isn't purple the first time through the loop, it can't possibly become anything else the second time through the loop, so it'll keep being the wrong color each and every time.
I'm not certain what you intended for this loop to accomplish. If you're trying to have the user enter a new value each time, you won't be able to do that with a loop in PHP. You'll have to regenerate the entire page (with an error message, presumably) and ask the visitor to submit the form again.
In the case of "contact webmaster", you need to break out of the loop, either with the break expression or by setting your $passlevel to anything other than zero. A more serious real problem is revealed in #Mike Christensen's answer, though
If $_GET['box_1_color'] is not set, the variable $passlevel will never be changed.
<?php
$passlevel = 0;
while ($passlevel == 0 || $passlevel == 2)
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel = 1;
}
else
{
echo "you didn't select purple.".'try again.';
}
}
else
{
echo "contact webmaster";
$passlevel = 2;
}
}
?>
You need to define another passlevel for failure, to stop the while loop. Also, don't put any quotes around integers.

trying to write a proper if statement in php

i am having a hard time wrapping my head around how to do this... i have a select menu i want to display as default and a different one(s) if certain items appear in a shopping cart.
so say i sell dogs and cars. in the cart i have selected one of each. i want one select menu to appear if there are ever any cars in the cart. but if there are ever any dogs in the cart or cats for that matter i want a different select with different options to appear. so car_select would be the default if anything is in the cart...(i sell a lot of stuff) but if a dog appears in the cart then dog_select would replace it.
the way it is written now. the default is the last thing and that is what shows.
(see my comments)
so the code:
<?php
foreach($this->rows as $i => $row){
if (strstr('dogs',$row->dates)) {
$mydates = 'dogs';
} else {$mydates='default';}
//echo $row->dates; this spits out the correct values dogcar
}
//echo $mydates; with a dog and a car in the cart this returns default
//below functions i believe but i think the above is faulty
?>
<?php
if ($mydates =='dogs'){
//do something here;} else {
$type = $this->type;
foreach($this->extraFields[$type] as $fieldName => $oneExtraField) {
if ($fieldName != 'pickupdate-rosh'){; ?>
<tr class="hikashop_checkout_<?php echo $fieldName;?>_line">
<td class="key">
<?php echo $this->fieldsClass->getFieldName($oneExtraField);?>
</td>
<td>
<?php
echo $this->fieldsClass->display($oneExtraField,$this->$type->$fieldName,'data['.$type.']['.$fieldName.']');
?>
</td>
</tr>
<?php } ?>
<?php } ?>
<?php } ?>
i would like to always return car_select unless the presence of dogs or cats etc in the cart.
{edit: i just realized that is not the correct function to use to look for a string.}
It looks like you're setting $mydates, then overriding it (potentially). This is because you're setting one variable to one result for all iterations of your foreach loop, and the last iteration will be what the final variable value will be.
So set $mydates to a default and then change it if the condition is right.
$mydates='default';
foreach($this->rows as $i => $row){
if (strstr('dogs',$row->dates)) {
$mydates = 'dogs';
}
//echo $row->dates; this spits out the correct values dogcar
}

Categories