This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I have a custom field in a WP post called "profession." If this profession is RN, then I want one piece of text displayed. If the profession is not RN, then I want another piece of text displayed.
I added the following code to execute this function:
<?php if ( $profession = 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
The problem is, the first echo statement is displaying even when the post does not have RN in the profession field. If I purposely break the if variable, then it defaults to the second echo statement across the board. I can't get the post to respond dynamically to one vs the other.
Am I missing something in this code that is causing one or the other to display for all instead of based on the parameter I am trying to set?
You need to compare in your if not assign.
= is used to assign values
== is used for comparison, checks if value is equal
=== is used for strict comparison, checks if value and type are equal.
So your php if statement should look like this
if ( $profession == 'RN' ){
code
}else{
code
}
Please read how to use condition before writing.
http://php.net/manual/en/control-structures.if.php
<?php if ( $profession == 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li><li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
<?php if ( $profession == 'RN' ) {
echo '<li>Minimum 2 years experience</li><li>Current license in this state</li><li>Graduated from accredited Nursing school</li><li>BCLS required</li> <li>BSN and ACLS preferred</li><li>Other requirements to be determined by our client facility</li>';
} else {
echo '<li>Minimum 2 years experience</li><li>Other requirements to be determined by our client facility</li>';
} ?>
Related
I'm setting up a system where if a users group number is present the user will have access to the page. For example I want people in group 7,8, and 14 to have access to the page. This works fine for the users in group 7 and 8 but if the user is group 14 access appears to be denied. Here's what I tried so far I tried to go this two ways but to no avail. Any help would be appreciated.
An if statment with more than two conditions.
if ($_SESSION['userrole'] == 8 || $_SESSION['userrole']== 7 || $_SESSION['userrole']== 14) {
//text
}else{
echo "access denied";
An array that have values to check against
$acc = array("8","7","14");
if (in_array($_SESSION['userrole'],$acc)){
//text
}else{
echo "access denied";
as Syscall mentioned in his comment you are trying to look for the value "2,14"
in array contain values like this array("8","7","14");
this is want work
what you need to do in this case is use explode()
in your scenario, it will look like this
$acc = array("8", "7", "14");
$user_group = explode(',',$_SESSION['userrole']);
foreach ($user_group as $group) {
if (in_array($group, $acc)) {
//text
} else {
//text
}
}
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I wrote a program in python in my raspberry pi which use the gpio 17. My goal is to read the status of this gpio separably to this program, to run a "if" and to display the result in a local website. For this, I use apache2 and PHP (version 7), I'm beginner in this language. This is the program I use :
<?php
$read = shell-exec ('gpio read 0');
$status = intval($read);
if ($status = 1) {
print ("oui");
}
else {
print ("non");
}
?>
This program don't work because if I understand, the value of $read I obtain is a string and I need an Int to use it in my "If". For it, I tried to change this String to an Int thanks to the function intval() (like you can see in the program in the top) but it didn't work. I tried to use also the ord() and the (int) function. The result is always the same. It display "oui".
Is my problem come from the intval() function or is it maybe come from shell-exec() ?
Thanks for your help ;)
I tried to be the clearest possible in my explanation
You have an error at this line, since = is not a comparison operator:
if ($status = 1) {
It should be:
if ($status == 1) {
If you also want to check that 1 and $status are of the same type, use the operator === instead. Here is the PHP documentation for comparison operator.
You will need to use a comparison operator. I have modified your if condition to compare if the values are identical.
<?php
$read = shell-exec ('gpio read 0');
$status = intval($read);
if ($status === 1) { //Use === to check if they are the same type and value
print ("oui");
}
else {
print ("non");
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 7 years ago.
Improve this question
I'm having a little problem with validation. I have here this code snippet that works by this way: if the pizza_size variable is not set in the Query String and the ID of product category is 1 (corresponds to pizzas) or this is set but the pizza_size value is not equal to small, medium or big this needs to return to the page products.php with the message that the size is not selected. I'm trying with:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" || $_GET['pizza_size'] != "medium" || $_GET['pizza_size'] != "big" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
else //Add the product
The problem is that this is not working. Even if the pizza_size corresponds to small, medium or big I receive the error message. But if I use only one comparison, this works great, like:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
But I need to validate with the 3 sizes. How to do this validation with all the three possible variables? Thanks!
if(($category_id == 1)
&& !(isset($_GET['pizza_size']) && in_array($_GET['pizza_size'], array('small', 'medium', 'big')))
) {
redirect_to('products.php?mesage=no-size-informed');
}
else {
// Add the product
}
You have to do it like this:
if(($category_id == 1)
&& (isset($_GET['pizza_size'] && !in_array($_GET['pizza_size'], array('small', 'medium', 'big')) || !isset($_GET['pizza_size']
) {
redirect_to('products.php?mesage=no-size-informed');
}
I'm having a bit of a brain-fart here...lol
I was using a forloop() initially for this.. but then the conditions got a bit more advanced on things I needed to check for, as well as the output based on found/not found..
My problem is ensuring that on consecutive loops.. the 'else' on the (no access) potion only gets echo'd once... but of course on every 'loop' it will check from the top and if not a match output the 'no-access' text.. (on every iteration).. where it should only output once.
Originally there was only a few if() statement/checks in the foreach() loop.. where a simple break; took care of things fine...
but these if()'s turned into if/else.. which means the else will get 'triggered' on the next 'loop'.. how can this be prevented?
$arrwebinars = ("Name1", "Name3");
foreach($arrwebinars as $webinar) {
/* Webinar 1 */
if($webinar == 'Name1') {
if($web01_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
/* Webinar 2 */
if ($webinar == 'Name2') {
if ($web02_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
/* Webinar 3 */
if ($webinar == 'Name3') {
if($web03_title != '') {
echo "available";
} else {
echo "not available";
}
} else {
echo "no access";
}
}
is there some other sort of 'control' I can use to ensure the main if/else only gets executed once?
Sorry , I am having a hard time trying to describe this one, hopefully the code explains it all (and the problem)
thanks.
update:
Current State: reverted back to foreach() loop.
User is only authorized for 2 vids (#5 & #6 we'll say..can be any of the 6 in reality)
Upon first loop/iteration.. vids 1-4 output (no access for you!) (because they dont, only #5 & #6 as stated above)
no. 5 outputs the embed code fine (access)
no. 6 says 'No access for you'.. (even though they do/should have access)
Upon the scond iteration..vids 1-4 are duplicated, and again say "No access for you" (outside of it being a duplication..this is correct).. however, vid #5 NOW says "no access for you" (first loop it outputted fine, second loop it is looking for a new 'match' while not what I want.. in theory this is correct.. but should have a duplicate)
Vid #6 is NOW outputting the embed code (where as on first loop it did not)..
The user has only purchased access to 2 vids.. so the loop only happens twice.
but I get '12' echo's
No matter what I should only get 6 echo's with 1 out put per video (available/not available -or- no access)
I cant seem to wrap my head around this today.. :(
Goal I want achieve:
6 outputs TOTAL
each output (echo) should ONLY have:
available or not available printed (this is the nested conditional check for the blank title)
-or-
no access
there are 6 video to check against.
the user can have purchased access to 1 or up to all 6 vids
first check is: is the webinar name in their purchased array found in the total available 6 vids available
second tier conditional check:
if YES (match found == access)...then check to see if the title is missing (if there output access [video embed code]... if not there output text 'not available yet')
(going back to first conditional check) if there is NO ACCESS (match not found).. output 'no access' text.
This works if I wanted duplicates on the stage/page.. (but I dont!) LOL..
I need to be limited to ONLY 6 outputs,.. as there are only a total of 6 vids available.
I DO NOT WANT:
on loop 1 it outputs access for vid#1 and #2-#6 are no access..
on loop 2 it re-states all of these outputs, has vid#1 no-access now, vid#2 has access and vids #3-#6 are no access...
etc.etc.. and the cycle continues. I'm getting lost on this one!..
thanks!
If you wish to stop considering that "row" when you encounter a "no access" then you could simply continue; after each no-access. or do you wish to check the others even if you encounter "no access" on the first, just to fail silently in that case?
Not sure exactly what you're trying to achieve, but your code could be shortened greatly.
$webinars = array('one', 'two', 'Name3');
$web0_title = 'not empty'; // Just a demo
for ( $i = 0; $i < count($webinars); $i++ )
{
$access = FALSE;
$available = FALSE;
$name = 'Name' . $i;
$title = 'web' . $i . '_title';
if ( $webinars[$i] == $name ) {
if ( ! empty( $$title ) ) {
$available = TRUE;
}
$access = TRUE;
}
// Simple debug, not sure what you want to accomplish
echo 'Webinar: ' . $webinars[$i] . ' <br />';
echo 'Availability: ' . ( $available ? 'Available' : 'Not Available' ) . '<br />';
echo 'Access: ' . ( $access ? 'Access' : 'No Access' ) . '<br /><br />';
}
Edit: Just read your comments so I updated it.
I am running subscriptions on my website.
I have a 1,3,6 and 12 months of subscription, and I would like the user to be able to change that subscription whenever they feel like.
However, I need to calculate the amount of money the user had to pay had he or she signed up for the shorter term, rather the relatively cheap,longer one.
I made this function optimized_subscription_total($active_sub_time,$arr_sub_values) so that it returns that sum of money exactly.
<?php
function optimized_subscription_total($active_sub_time,$arr_sub_values)
{
// This function takes a row from the DB where prices for each period of time usage is listed. there are prices for 1 month, 3 months,6 and 12 months.
// when the user has subscribed for 12 months, and the user asks for a refund, after they used 9 months and 6 days for example, the system treats the refund as if they subscribed for (in months) COST_6 + COST_3 + (COST_1/30)*6
// the result of the function is then subtracted from the amount they actually paid and is considered the refund.
// $arr_sub_values is the associative row from the DB, containing the prices
// $active_sub_time is measured in months and is a double
$result=0;
while(($active_sub_time-12)>=0)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while(($active_sub_time-6)>=0)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while(($active_sub_time-3)>=0)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while(($active_sub_time-1)>=0)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
if($active_sub_time>0)
$result+=($active_sub_time)*($arr_subscription_values['COST_1']);
return $result;
}
$datetime1 = date_create('2009-12-11');
$datetime2 = date_create('2010-11-09');
$interval = date_diff($datetime1, $datetime2);
$num_of_months = ($interval->format('%y'))*12+($interval->format('%m'))+($interval->format('%a'))/30;
echo "<br />";
$v = array('COST_1'=>'3.99','COST_3'=>'9.99','COST_6'=>'15.99','COST_12'=>'25.99');
echo "OPT value for $num_of_months months=" . optimized_subscription_total($num_of_months, $v);
?>
Strangely I get the bug appearing only after 7 to 10 times after refreshing this code.
So I got:
OPT value for 10 months=M.97
as a result here. I think I need to get a float number, no ?
I was expecting the result of the function that should be "OPT value for 10 months=29.97", as it should take COST_6 + COST_3 + COST_1... but I get that weird M.97, and sometimes things like POKHHHG.97
I would change the logic to the following and see if error is still produced. I think this is a little bit more clear and easy to debug. It is the same as your though just explained differently.
while($active_sub_time>=12)
{
$active_sub_time-=12;
$result+=($arr_subscription_values['COST_12']);
}
while($active_sub_time>=6)
{
$active_sub_time-=6;
$result+=($arr_subscription_values['COST_6']);
}
while($active_sub_time>=3)
{
$active_sub_time-=3;
$result+=($arr_subscription_values['COST_3']);
}
while($active_sub_time>=1)
{
$active_sub_time-=1;
$result+=($arr_subscription_values['COST_1']);
}
What I would also do is added debug cod at the top inside function.
echo "<br>$active_sub_time" // debug code include at the top
This will give you an idea, if any garbage being is being sent to the function itself.
Also I would add this test function in all while blocks if the above does not resolve the issue.
if (!is_numeric($result))
{
echo"<br> Bug occurred";break; // print other values if necessary
}