code checks one checkbox or the other, but not both - php

So basically, I have a function that is ran when a post is created on wordpress. I've modified this function by adding the following code.
$episodeID = $_POST['item_meta'][137];
$episodeVersion = $_POST['item_meta'][357];
if ($episodeVersion == "Subbed") {
$value = array("Subbed");
update_field('field_48', $value, $episodeID);
}
else if ($episodeVersion == "Dubbed") {
$value = array("Dubbed");
update_field('field_48', $value, $episodeID);
}
This code basically says, if the value of a field in that post is "Subbed" then update another checkbox field by checking the "Subbed" checkbox. If it's "Dubbed" then update the checkbox field by selecting the "Dubbed" checkbox.
This works perfectly fine, however there is no time when both of these checkboxes are checked. If I add a post with the Dubbed, it'll check dubbed, then if I add a post with Subbed, it'll uncheck dubbed and check subbed.
So basically, how can I make it so it doesn't actually uncheck whatever has already been checked. So what should I use to check to see if the checkbox is unchecked or checked? Some type of boolean true / false?

Okay, so I was able to manipulate some of the code and get it to work eventually.
All I had to do was check if the Subbed checkbox was already checked when executing the dubbed if statement, and vice versa.
Here is the updated code
$episodeID = $_POST['item_meta'][137];
$episodeVersion = $_POST['item_meta'][357];
if ($episodeVersion == "Subbed" && !(get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion', $episodeID))) ) {
if (get_field('episode_sdversion') && in_array( 'Dubbed', get_field('episode_sdversion'))) {
$value = array("Subbed", "Dubbed");
} else {
$value = array("Subbed");
}
update_field('field_48', $value, $episodeID);
}
if ($episodeVersion == "Dubbed" && !(get_field('episode_sdversion') && in_array('Dubbed', get_field('episode_sdversion', $episodeID))) ) {
if (get_field('episode_sdversion') && in_array( 'Subbed', get_field('episode_sdversion'))) {
$value = array("Subbed", "Dubbed");
} else {
$value = array("Dubbed");
}
update_field('field_48', $value, $episodeID);
}

Related

Checkbox checked and inserted into database

I'm trying to save the data into the database based on the checkbox. If the checkbox is checked then save, else don't save. My idea is passing the ng-model value to php value then do validation but it doesn't work.
blade.php
<input type="checkbox" name="update_info" ng-model="item.update_info"></label>
php
$value = "{{item.update_info}}"
if ($vendor === NULL && $value === true) {
try {
DB::table('vendors')
->insert([
'vendor_id' => $vendor_id,
'price' => $price
]);
} catch(PDOException $e) {
throw new TransactionException([$e->getMessage()]);
}
}
The value of a checkbox is not a boolean type true, but a string with value 'on';
Try this:
if ($vendor === NULL && $value === 'on') {
You use value in checkbox when it is checked the value will go to the server, for eg:
<input type="checkbox" name="update_info" ng-model="item.update_info" value="1"></label>
So you can validate in the server
$value = Input::get('update_info');
if($value){ ... }
Try this and let me know how it works :)

PHP Display Magento Yes/No Attribut if Value is Yes and display an jpg instead of "yes"

I would like to display Yes/No Attributes in my view.phtml. I tried some codes but none is working.
Firstly I tried this syntax, which I found only and which is working for Attributes with Text-Values:
<?php
if ($_product->getAn_342() != null && $_product->getAn_342() != "") {
echo $this->__('Deliveryinformation');
echo $_product->getAn_342();
}
?>
This does not work for Attributes with yes/no Values. With this syntax there is displayed the Magento-ID of the Attribut but not the Value.
Next I tried this Syntax, which I found here in this forum:
<?php
if ($_product->getAttributeText($_data['An_56']) == "Yes"):
?>
But this does not work as well.
For explanation:
One Attribut i want to display would be "Bestseller". The Attribut has Label "an_bestsller" and code "an_56", which will be the magento-Id.
So what exactly is wrong with that yes/no syntax above? Some help would be great.
You can try to do as follows :
$attr = $product->getResource()->getAttribute("oversize_type");
if ($attr->usesSource()) {
$oversizeLabel = $attr->getSource()->getOptionText($oversizeId);
}
Please change oversize_type to your attribute code
Above is the way of getting the option text of dropdown type.
hope this helps.
You've to check different translatable values: 'N/A' or 'NO':
if ($_product->getAttributeText($_data['An_56']) =! $this->__("N/A") && $_product->getAttributeText($_data['An_56'] != $this->__('NO')):
It is better to make an extension which extends Mage_Catalog_Block_Product_View_Attributes and overwrites the function getAdditionalData:
public function getAdditionalData(array $excludeAttr = array())
{
$data = parent::getAdditionalData($excludeAttr);
foreach ($data as $code => $item) {
if ($item['value'] == Mage::helper('catalog')->__('N/A')
|| $item['value'] == Mage::helper('catalog')->__('No')
//You can add more values to hide
//|| $item['value'] == Mage::helper('catalog')->__('--')
) {
unset($data[$code]);
}
}
return $data;
}

$_SESSION variable getting lost somewhere........or PHP ignoring "IF...ELSEIF"

I am having a problem with getting my PHP script to correctly read and execute my "IF.....ELSEIF" conditions.
In my first file, I have the following code :
if(isset($_POST['submit']) {
$selected_radio = $_POST['selection'];
$_SESSION['my_selection'] = $_POST['selection'];
if (($selected_radio == '25') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
elseif (($selected_radio == '50') {
header("url=http://xxxxxxxxxxxxxxxxx");
}
}
That was the easy part.
If either "radio button" is selected, I have a Javascript function which opens a "new (child) window"
That's also easy.
But, then, comes the hard part : within that new window, the user has to select from another choice of radio buttons :
if(isset($_POST['submit']) {
$selected_radio = $_POST['my_response'];
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
}
Basically, this means : if the user selects "yes" in the current (child) window, then the window closes, and the parent window re-directs to "25.php" or "50.php"..........depending on the value of the $_SESSION['my_selection'] --- which was selected earlier in the parent-window
But, for some reason, it's not working. My code is executing only the FIRST IF-condition :
if (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25'))
{
echo '<script type="text/javascript">window.opener.location =
"/PHP/25.php";setTimeout("window.close();", 1000);</script>';
}
It is completely ignoring the second one.............even if the user had earlier selected "50" in the parent-window.
My first thought was : the SESSION value of the radio-button ---- $_SESSION['my_selection'] --- was not being carried-over into the new (child) window.
But, I used "echo" to verify that this was working properly. The value was indeed being carried-over into the new (child) window.
However, after the child-window closes, and the parent-window is re-directed, I used "echo" again to track any errors........and it showed that : the value of $_SESSION['my_selection'] is always equal to "25" !
In a nutshell : why is the second IF-statement being ignored??
elseif (($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] =
'50')) {
echo '<script type="text/javascript">window.opener.location =
"/PHP/50.php";setTimeout("window.close();", 1000);</script>';
($_POST['my_response'] = 'yes') && ($_SESSION['my_selection'] = '25')
^ ^
A single equals sign is an assignment and as the value you are assigning is truthy the if will always evaluate to true. Use == for a comparison.
You are using = instead of == in nearly all statements:
if (($_POST['my_response'] = 'yes')
should be
if (($_POST['my_response'] == 'yes')
This way, you don't check if $_POST['my_response'] is equal to "yes", but if it is possible to assign "yes" to $_POST['my_response']. As a result, all your if statements are true.

php ! operator not working

I have this code for a few check-boxes, works fine like this
foreach($_POST as $key => $order_type) {
if ('1' == $_POST[$key]) $_POST[$key] = '0';
}
if I negate the if it stops working and I'm sure that some are not == '1'; it just sets them to NULL.
foreach($_POST as $key => $order_type) {
if ('1' != $_POST[$key]) $_POST[$key] = '0';
}
do I miss anything ? tried with !('1' == $_POST[$key]) too.
Thanks
Checkboxes only get sent to the server if they are checked.
I assume that they have a value of 1, so you will be able to find these in the $_POST array. However, there will be none where the value is 0 (unless you specify a value of 0 in the html and check the box...).
To check checkboxes, you need to use isset as the value is really not that important, it is either set (checked) or not and then it simply does not appear.
How about a simple if/else?
if ('1' == $_POST[$key]) {
$_POST[$key] = '0'; }
else {
Do this if it's != ;
}

Which checkbox is selected and assigning a value

I am trying to create logic to see what checkbox is selected (of 5 possible checkboxes) and if it is selected assign it a value of 0. if it is not selected I was to assign it a value of 1. The proceeding code snippet highlight this but throws a parse error in my else statement and I cannot fighure out why.
//Check to see what checkbox is marked for correct answer
//Correct answer variables
$chkBox1 = 'unchecked';
$chkBox2 = 'unchecked';
$chkBox3 = 'unchecked';
$chkBox4 = 'unchecked';
$chkBox5 = 'unchecked';
if (isset($_POST['chkBox1'])) {
if ($chkBox1 == 'chkBox1Selected') {
$chkBox1 = '0';
}
else{
$chkBox1 = '1';
}
}//End of chkBox1Selected logic
You don't understand how checkboxes work. If a checkbox is deselected before posting, it will not be set on post.
Therefore, the only condition that will ever be present in your code is that every value will show as 1, since they cannot be overridden.
Take this snippet and try it out. It dynamically loops for the amount of variables you need and assigns the values based upon the submitted value.
$_POST['chkBox4'] = 'test';
for( $i = 1; $i <= 5; $i++ )
{
$name = 'chkBox' . $i;
$$name = !isset( $_POST[$name] ) ? 0 : $_POST[$name];
}
print $chkBox2 . ' // '. $chkBox4;
http://codepad.org/51RotnCf
Ok I got it to work from a syntax standpoint, however now no matter what is selected it is still assigning a value of 1 to all the checkboxes and not changing the selected checkbox to a value of 0. Here is the new code that is correct from a syntax standpoint but defaults to 1 no matter what:
//Check to see what checkbox is marked for correct answer
//Correct answer variables
$chkBox1 = '1';
$chkBox2 = '1';
$chkBox3 = '1';
$chkBox4 = '1';
$chkBox5 = '1';
if (isset($_POST['chkBox1'])) {
if ($chkBox1 == 'chkBox1Selected') {
$chkBox1 = '0';
}
}//End of chkBox1Selected logic
if (isset($_POST['chkBox2'])) {
if ($chkBox2 == 'chkBox2Selected') {
$chkBox2 = '0';
}
}//End of chkBox2Selected logic
if (isset($_POST['chkBox3'])) {
if ($chkBox3 == 'chkBox3Selected') {
$chkBox3 = '0';
}
}//End of chkBox3Selected logic
if (isset($_POST['chkBox4'])) {
if ($chkBox4 == 'chkBox4Selected') {
$chkBox4 = '0';
}
}//End of chkBox4Selected logic
if (isset($_POST['chkBox5'])) {
if ($chkBox5 == 'chkBox5Selected') {
$chkBox5 = '0';
}
}//End of chkBox5Selected logic

Categories