I learn PHP and in one of examples I found a part of a code I don't understand.
....
if (array_key_exists('submited', $_POST)) {
for ($i = 1; $i <= $_SESSION['counter']; $i++) {
if (!empty($_POST['checkeditem' . $i]))
if ($_POST['checkeditem' . $i] == 'on') {
$rezultat = mysqli_query($conn, "DELETE FROM gas WHERE id=" . $i);
echo "Checked items are deleted";
}
}
} else {
....
In the code above I don't understand this code line:
$_POST['checkeditem' . $i] == 'on'
There is no attribute with the name='on' in entire code, so it is not related to any name attribute. What is value 'on' and is there other values like that one, that are related to $_POST? Could you suggest me what to google, to find more about this? Thank you.
In the code above I don't understand this code line and here on seems checkbox is checked or not?
$_POST['checkeditem' . $i] == 'on'
will compare like
$_POST['checkeditem1'] == 'on'
$_POST['checkeditem2'] == 'on'
till the last iteration of the loop e.g 100th
$_POST['checkeditem100'] == 'on'
Related
i want to check two string in if condition equal or not but it is give me return true whats the problem please check my code.
$if = "(1 == 2)";
if($if){
echo 'hi';
}
see my code above.. it always return hi.. what i was done wrong please help me.
its return only hi.. i have many condition store in if variable but my first condition not fulfill so i want it now.. please suggest me.
my full code is here..
$if = "(1 == 2)";
if($location_search != ''){
$if .= " && ('."$location_search".' == '."$get_city".')";
}
if($location_state != ''){
$if .= " && ('."$location_state".' == '."$get_state".')";
}
if($location_bedrooms != ''){
$if .= " && ('."$location_bedrooms".' == '."$get_bedrooms".')";
}
if($location_bathrooms != ''){
$if .= ' && ('."$location_bathrooms".' == '."$get_bathrooms".')';
}
if($location_type != ''){
$if .= ' && ('."$location_type".' == '."$get_type".')';
}
if($location_status != ''){
$if .= " && ('".$location_status."' == '".$get_status."')";
}
if($if){
echo 'hi';
}
i added this code but always return tru and print hi. please help me.
Collect all of your variables in one array and go through it with foreach checking everyone. If one of variables is empty, assign false to your if variable. Will write code later, when will be next to computer.
Your variable $if is a declared as a string due to the double quotes. How about this
$a = 1;
$b = 2;
if ($a == $b) {
// When $a equals $b
echo "hi";
} else {
// When $a doesn't equal $b
$if is a string, and not null. In PHP this means it's true. I think if you remove the quotes, it should instead evaluate the expression to false.
Remove the double quotes here " (1 == 2)"
You can remove quotes (with quotes it's a normal string), but keep in mind that this is bad approach and should not be used like this.
Working code:
$if = (1 == 2); // evaluates to true
if($if) {
// code
}
I have this condition here:
if($name != '' || $name != 0 || $name != "0"){
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
My problem with this condition if $name is equal to "0" it still adds the $where to my $where variable. how do I fix this ?
i think, you mean something else, try use && instead of ||
Well, if this statement is true:
$name is equal to "0"
Then this condition evaluates to true:
$name != ''
Therefore the entire conditional check evaluates to true.
It sounds like you want to use && instead of ||:
if($name != '' && $name != 0 && $name != "0")
That way the entire condition will evaluate to true only if all three conditions are met, instead of only one condition.
The best solution is to use :
if(!empty($name))
Just using this should solve your issue:
if($name){
//your condition
}
try this
if ( !in_array($name, array('','0',0), true ) ) {
$where .= ' AND readyBuilt.home_title = "' . $name . '"';
}
if($_SESSION['valueofdie1'] != 0 && $_SESSION['valueofdie2'] != 0 && $_SESSION['valueofdie3'] != 0 && $_SESSION['valueofdie4'] != 0 && $_SESSION['valueofdie5'] != 0)
{
if((($_SESSION['valueofdie1'] == $_SESSION['valueofdie2']) && ($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']||$_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie1'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie2'] == $_SESSION['valueofdie3']) && ($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']||$_SESSION['valueofdie5'])) || (($_SESSION['valueofdie2'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5']))
|| (($_SESSION['valueofdie3'] == $_SESSION['valueofdie4']) && ($_SESSION['valueofdie4'] == $_SESSION['valueofdie5'])))
{
if($_POST['choose'] == 'choose 3oaK')
{
$_SESSION['g'] = 5;
$_SESSION['scoretkind'] = $_SESSION['valueofdie1'] + $_SESSION['valueofdie2'] + $_SESSION['valueofdie3'] + $_SESSION['valueofdie4'] + $_SESSION['valueofdie5'];
unset($_SESSION['3oaKBut']);
echo '<input type="hidden" name="choose" value="Clear" onLoad="form.submit();">';
$_POST['sub'] = 'reset';
$_POST['choose'] = '';
}
if(empty($_SESSION['g']))
{
$_SESSION['3oaKBut'] = '<input type="submit" name="choose" value="choose 3oaK">';
echo $_SESSION['3oaKBut'];
}
}
}
if($_SESSION['g'] == 5)
{
echo $_SESSION['scoretkind'];
}
So here is the code we have. We are trying to check if 3 of the 5 die values are equal. If they are equal we echo out a button that allows the user to choose to score his 3 of a kind, which is the total of all of the dice. Everything works except in some cases the 3 of a kind button would echo out when there isnt a 3 of a kind. Halp PLS
I'm sorry I didn't answer your question by actually solving your bug, but I think your code is hard to read and your approach makes it cumbersome to program all the rules.
General advice: Put $_SESSION['valueofdie1'] and the other dice into an array of values. That's much easier to work with. After that, it should be pretty easy to check how many times each value occurs. Even when you keep your approach, you could make variables like $die1, which is already a lot shorter and more readable than $_SESSION['valueofdie1'].
But with an array, you could roughly start like this:
// Put all dice into an array.
$dice = array(
$_SESSION['valueofdie1'],
$_SESSION['valueofdie2'],
etc.... );
// Count how many times each die is rolled.
$diceCount = array();
foreach($dice as $die) {
$count = 0;
if (isset($diceCount[$die])) {
$count = $diceCount[$die];
}
$diceCount[$die] = $count + 1;
}
// Check possible results simply by looking at those counts.
// If one die value is rolled 5 times, it's Yahtzee...
if (array_search(5, $diceCount) !== false) {
echo 'Yahtzee!';
}
if (array_search(4, $diceCount) !== false) {
echo 'Four of a kind';
}
// Full house takes two types.
if (array_search(3, $diceCount) !== false && array_search(2, $diceCount) !== false) {
echo 'Full house';
}
for ($diceCount as $die => $count) {
echo "$count times $die";
}
... etc ...
You'll need to expand this list, and take some other rules into account. After all, a Yahtzee could also count as a Four of a Kind. But by checking all those rules, you can generate a new array of possible combinations, which you can check against the previously chosen options. And the outcome of that determines which options the player can choose.
The user gets several options which adds a different class into something that is read into PHP -
The below works, but it seems very inefficient and uses a lot of code. Is there a way I can cut this down?
<?php
if(get_sub_field('status') == "Upcoming"){
echo '<li class="upcoming">';
}
if(get_sub_field('status') == "Next"){
echo '<li class="next">';
}
if(get_sub_field('status') == "See what happened"){
echo '<li class="happened">';
}
?>
You could make a table that holds the available values for which you want. This way eliminates all of the if statements. Keep in mind thought that this is functionally equivalent to your code above.
$opt_vals = array(
"Upcoming",
"Next",
"See what happened"
);
$val_to_search = get_sub_field("status");
if(($key = array_search($val_to_search, $opt_vals)) !== FALSE) {
echo '<li class="' . strtolower($val_to_search) . '">';
}
Since array_search returns the key with the corresponding value, $key would hold the key, but as #Timur emphasized, a value at corresponding index 0, would evaluate to FALSE, 0, so in order to really check if it has the value we have to use the strict !== operator because 0 == NULL would evaluate to true.
you can do it with if...else if like this.
<?php
$status = get_sub_field('status');
if( $status == 'upcoming')
$class = 'upcoming';
else if( $status == 'Next' )
$class = 'next';
else if( $status == 'See what happened')
$class = 'happened';
?>
<li class="<?php echo $class; ?>"></li>
The only way you could change it is this:
echo '<li class="' . get_sub_field('status') . '">';
Otherwise there is no way around this! You have to use if and elseif or you make a switch statement
When you sill want to look if the value is valid you can make a array like a whitelist:
$valid = array(
"Upcoming",
"Next",
"See what happened"
);
And then you can check it like this:
if($value = array_search(get_sub_field("status"), $valid))
echo '<li class="' . strtolower($value) . '">';
else
echo '<li class="default">'; //if something fails
I have a form validation issue. Below is the logic that happens on submit (part of it at least.) In the for loop, we check an array of possible events that a site visitor can register for. If the user hasn't checked any events (these are checkboxes because a user can register for multiple events), we should enter the second if statement below, but for some reason we're not. I know that none of the post variables are set if nothing is checked and, by setting a session variable equal to the variable $ECEventCnt, I'm able to varify that if nothing is posted, that variable is equal to 0. However, we seem to never get into the second if statement. Any thoughts?
unset($_SESSION["ECEvents"]);
$ECEventsArray = array();
$ECEventCnt = 0;
$_SESSION['debug'] = 'EC';
for ($i=0; $i<count($Val_WhichEventTypes); $i++) {
$key = $Val_WhichEventTypes[$i]["eventKey"];
//echo 'key' . $key;
if (isset($_POST["WhichEvent-" . $key]) && $_POST["WhichEvent-" . $key] == $key) {
$_SESSION['debug'] .= ' we made it to the place.' . $_POST["WhichEvent-" . $key];
$ECEventsArray[$key] = $key ;
if (strlen($ECEventsArray[$key])) $ECEventCnt += 1; // Only advance counter if EC Event is checked (key value is set)
}
}
$_SESSION['ecventcount'] = $ECEventCnt;
if ($ECEventCnt = 0) {
set_step_INvalid(5);
$_SESSION['debug'] .= ' we made it to the 2nd place.';
$cnt += 1;
$ValidationError .= ((strlen($ValidationError)==0) ? "" : ", ") . "<br />Please just select at least one Event Type/Time";
}
$_SESSION["ECEvents"] = $ECEventsArray;
//valid_step52();
}
if ($ECEventCnt = 0) {
should be
if ($ECEventCnt == 0) {
You are assigning to the variable $ECEventCnt, but what you mean to do is compare using it.