I have two functions:
$shipmethod = $wpdb->get_results( "SELECT * FROM wpck_rg_lead_detail WHERE
lead_id = $user_id and field_number = 111" );
$shipmethodo=$shipmethod[0]->value;
and If statement:
if( isset( $entry['103'] )) {
return number_format (($entry['103'] * 0.9),0, ".", ",") ;
}
I need to add an else if statement with one given value for $shipmethodo, being this one false or not true, so for that I'm using: !$ = 'value', however this is not working:
else if( isset( $entry['103'] ) && !$shipmethodo = 'value') {
return number_format (($entry['103'] * 0.8),0, ".", ",") ;
}
What I need to get is that if $shipmethodo 'value' is different, then condition works.
How can I make it?
Thank you!
In your else if statement:
else if( isset( $entry['103'] ) && !$shipmethodo = 'value') {
It should actually be
else if( isset( $entry['103'] ) && $shipmethodo != 'value') {
!= means "does not equal".
Related
Good day,
I would like to know if there is any function for making the difference between this:
?param=
and this:
?param
Because I would like my script to detect if value is empty (first example), or if it's not given (second example).
I made a test with the following functions but I could not find what I want:
if( isset( $_GET['param'] ) ) {
echo '<div>isset</div>';
if( empty( $_GET['param'] ) ) {
echo '<div>empty</div>';
} else {
echo '<div>not empty</div>';
}
if( is_null( $_GET['param'] ) ) {
echo '<div>null</div>';
} else {
echo '<div>not null</div>';
}
if( defined( $_GET['param'] ) ) {
echo '<div>defined</div>';
} else {
echo '<div>undefined</div>';
}
} else {
echo '<div>not set</div>';
}
Any idea? Thank you in advance.
EDIT
Solution:
( strpos( '?'.$_SERVER['QUERY_STRING'], '?get=' ) !== false ) || ( strpos( '?'.$_SERVER['QUERY_STRING'], '&get=' ) !== false )
You can test with something like if( $_GET['param'] ) which will give true if Param is declared (even if it is an empty string) and false if not declared.
I have a problem with the following code when I make the price variable the function doesn't work but when I hardcode a price it works. (note I am in a testing face and haven't worried about security)
this, down't work:
function add_custom_price( $cart_obj ) {
global $product, $woocommerce ,$wpdb;
if ( isset( $_POST['fra'] ) ){
$GLOBALS['$_fra'] = urldecode( $_POST["fra"] );
} else {
$GLOBALS['$_fra'] = "";
}
if ( isset( $_POST['til'] ) ){
$GLOBALS['$_til'] = urldecode($_POST["til"]);
} else {
$GLOBALS['$_til'] = "";
}
if ( $GLOBALS['$_til'] !="" && $GLOBALS['$_fra'] !="" ){
$t1 = urldecode( $GLOBALS['$_til'] );
$t2 = urldecode( $GLOBALS['$_fra']);
$objArray = $wpdb->get_results("SELECT $t1 FROM test_priser WHERE city = '$t2'");
if ( isset($objArray[0]->$t1) ){
$priser = explode("/",$objArray[0]->$t1);
if ( !isset($priser[1]) ){
$priser[1] = intval($priser[0]);
}
}
echo "priser[1] = ".$priser[1]; //this outputs the expected value
}
if ( is_admin() && ! defined( 'DOING_AJAX' ) ){
return;
}
foreach ( $cart_obj->get_cart() as $key => $value ) {
$value['data']->set_price( $priser[1] );
}
}
When I add this line, and override the variable, it works:
$priser[1] = 1000;
I am quite confused as to what the problem might be.
for anyone else who might stumble upon this. Seems I need to pass the variable price in the session then it works.
you can find the answer here: custom price on woocommerce product
I have been trying to create a custom validation hook for Gravity Forms plugin.
The validation checks that at least one field has been filled in from a set of fields.
Check out the code below, I just can't get it too work. I think it is something to do with the variables for the inputs, even if a field is filled in, the error still shows on each field?
add_filter( 'gform_field_validation_2', function ( $result, $value, $form, $field ) {
if ( $field->type == 'number') {
$a = rgar( $value, $field->id . '10' );
$b = rgar( $value, $field->id . '12' );
$c = rgar( $value, $field->id . '13' );
$d = rgar( $value, $field->id . '14' );
$e = rgar( $value, $field->id . '15' );
$f = rgar( $value, $field->id . '17' );
$g = rgar( $value, $field->id . '18' );
$h = rgar( $value, $field->id . '20' );
$i = rgar( $value, $field->id . '21' );
$j = rgar( $value, $field->id . '22' );
$k = rgar( $value, $field->id . '23' );
if ( !empty($a) || !empty($b) || !empty($c) || !empty($d) || !empty($e) || !empty($f) || !empty($g) || !empty($h) || !empty($i) || !empty($j) || !empty($k) ) {
$result['is_valid'] = true;
$result['message'] ='';
} else {
$result['is_valid'] = false;
$result['message'] = 'Please select a quantity of materials to order';
}
}
return $result;
}, 10, 4 );
I think you maybe should use a field of type "radio buttons".
Anyway, if your form has several "number" fields and you need to validate that at least one of them has been filled, then you should use the gform_validation filter since you're validating the whole form, not just a single field.
TIP: Add a custom css class to each field in the group to identify them. for example "validate-quantity".
add_filter('gform_validation_2', 'quantity_validation', 1, 4);
function quantity_validation($validation_result) {
if ($validation_result['is_valid']) {
$valid=false;
$form = $validation_result['form'];
foreach( $form['fields'] as &$field ) {
if ( strpos( $field->cssClass, 'validate-quantity' ) === false ) {
continue;
}
$field_value = rgpost( "input_{$field['id']}" );
if (!empty($field_value)) {
$valid=true;
break;
}
}
if (!$valid) {
$field["failed_validation"] = true;
$field["validation_message"] = 'Please select a quantity of materials to order';
$validation_result['form'] = $form;
}
}
return $validation_result;
}
So heres a working version (Thanks for the gform_validation hint Francisco R) - Went down a slightly different route, but works perfectly, incase anyone interested in future!
add_filter( 'gform_validation_2', 'custom_validation_2' );
function custom_validation_2( $validation_result ) {
// array of field IDs to be checked
$field_ids = array (10, 12, 13, 14, 15, 17, 18, 20, 21, 23, 22);
// get the form object from the validation result
$form = $validation_result['form'];
// counter to store how many fields have a value > 0 submitted
$number_of_fields = 0;
// loop through all the fields to be sure one has a value > 0
foreach ( $field_ids as $input ) {
// the rgpost string we are going to check
$input_id = 'input_' . intval( $input );
// the value that was submitted
$input_value = rgpost ( $input_id );
if ( $input_value > 0 ) {
// if any field in the array has a value, we can just continue
$number_of_fields++;
} // end if
else {
// no value for this input, so continue without incrementing the counter
continue;
} // end else
} // end foreach
// check the $number_of_fields and if it is 0 return a validation error
if ( $number_of_fields == 0 ){
// set the form validation to false
$validation_result['is_valid'] = false;
// mark all the fields with a validation error
foreach( $form['fields'] as &$field ) {
// add a validation error to *all* the inputs if none were submitted > 0
if ( in_array( $field->id, $field_ids ) ) {
$field->failed_validation = true;
$field->validation_message = 'Please select a quantity of materials to order from one or all of these fields.';
} // end if
} // end foreach
} // end if
// assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
It looks you should change your if clause to:
if ( empty($a) || empty($b) || empty($c) || empty($d) || empty($e) || empty($f) || empty($g) || empty($h) || empty($i) || empty($j) || empty($k) ) {
to validate if at least one option is selected.
To skip not targeted fields add the following code before complex if above:
$target_fields = array('name_1', 'name_2');
if (!in_array($field, $target_fields)) {
$result['is_valid'] = true;
$result['message'] = '';
}
So I'm trying to work out conditionals with Wordpress post meta. I've been using get_post_meta() to display content if user had populated the post meta, but I need to improve the rule and add some additional conditionals to it.
Basically, what I need to do is extend this condition to multiple keys. If the user typed in both post_meta_1 and post_meta_2 some code will run, if not then some other code will run.
This is the code I'm currently using:
if (!((get_post_meta($post->ID, 'post_meta_1', TRUE))=='')) {
// code here
} elseif {
// code here as well
}?>
Here's how far my PHP logic goes:
if (!((get_post_meta($post->ID, array('post_meta_1', 'post_meta_2'), false))=='')) {
// code here
} elseif {
// code here as well
}?>
EDIT
Somehow I managed to get it to work by using this method:
<?php
$post_meta_1 = get_post_meta($post->ID, 'post_meta_1', TRUE);
$post_meta_2 = get_post_meta($post->ID, 'post_meta_2', TRUE);
if ($post_meta_1 && $post_meta_2) : ?>
CODE HERE
<?php endif; ?>
You will need to call get_post_meta() individually for each meta_key that you would like a value for. You can have multiple values stored under a single meta_key which is what the third parameter is for - true returns a single value, false an array of values for that meta_key. The result of get_post_meta() will be === false if there is no value in the database, or you can just check for empty() as well.
$capacity = get_post_meta( $post->ID, 'post_meta_1', true );
$approved_environment = get_post_meta( $post->ID, 'post_meta_2', true );
if ( $capacity && $approved_environment ){
// post_meta_1 AND post_meta_2 are set
}
if ( false !== $capacity && false !== $approved_environment ){
// post_meta_1 AND post_meta_2 are not set
}
if ( false !== $capacity || false !== $approved_environment ){
// post_meta_1 AND/OR post_meta_2 are not set
}
if ( empty( $capacity ) && empty( $approved_environment ) ){
// post_meta_1 AND post_meta_2 are not set or are equal to "", null, or 0
}
if ( empty( $capacity ) || empty( $approved_environment ) ){
// post_meta_1 AND/OR post_meta_2 are not set or are equal to "", null, or 0
}
I'm trying to get more advanced with php and I pick up the book PHP 5 Social Networking by Michael Peacock. While the book seemed to be interesting it didn't however get to involved in the details of the code. The function I'm trying to figure out is,
public function getURLData()
{
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
$data = explode( '/', $urldata );
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
$this->urlBits = $this->array_trim( $data );
}
}
This a part of a larger class and the $_GET['page'] is something like this: relationships/mutual/3. My main question is what is happening in the else section. I think what is happening that it's removing any empty array indexes but I also question that.
Any help would be appreciated.
EDIT: added array_trim function that is also part of the class
private function array_trim( $array )
{
while ( ! empty( $array ) && strlen( reset( $array ) ) === 0)
{
array_shift( $array );
}
while ( !empty( $array ) && strlen( end( $array ) ) === 0)
{
array_pop( $array );
}
return $array;
}
public function getURLData()
{
Gets the 'page', this data can be obtained by $_GET from the url: for instance: http://mysite.com/?page=contact
If 'page' has been set, is assigned to $urldata, else $urldata=''
$urldata = ( isset( $_GET['page'] ) ) ? $_GET['page'] : '' ;
$this->urlPath = $urldata;
if( $urldata == '' )
{
$this->urlBits[] = '';
$this->urlPath = '';
}
else
{
Now is creating an array with all the substrings from $urldata splited by '/'
$data = explode( '/', $urldata );
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the first element is equal to 0, then removes the first element from the array.
while ( !empty( $data ) && strlen( reset( $data ) ) === 0 )
{
//NOTES: php array_shift — Shift an element off the beginning of array
array_shift( $data );
}
If the array $data is not empty (otherwise accessing a non-existent element would raise an exception) or the lenght of the last element is equal to 0, then removes the last element from the array.
while ( !empty( $data ) && strlen( end( $data ) ) === 0)
{
array_pop($data);
}
array_trim is a custom function, not sure what does but probably will do some kind of trimming too
$this->urlBits = $this->array_trim( $data );
}
}