if else statement gives array - wordpress

I made this code to below. But it isn't working. Its giving an array.
Can somebody help me with this?
<?php
/**
*$fields['listcheckbox_1574292451270']
*/
$Extra_1 = $fields['listcheckbox_1574292451270'];
if ($Extra_1 == "1_1") {
echo ("Correct");
} elseif ($Extra_1 == "2_2") {
echo ("True");
} else {
echo ("False");
}
?>
I tried to put at the last else echo = "$1_extra"; but that gave an Array.
$fields[listcheckbox_1574292451270] is a checkbox with multiple options "1_1" & "2_2"
If this code isn't any good, can somebody help me with it?
Thanks

shouldn't that first line be:
$1_extra = $fields['listcheckbox_1574292451270'];
I think you missed the ' when accessing that array.
Edit: just noticed a few things:
if echo $1_extra; outputs Array - to see the actual values of it you either need to convert the array to a string or iterate over it. For debugging array values you can use var_dump($1_extra); which provides more information than echo does.
After that you should also see how to access the data inside your array. You can read more about working with arrays in php here: https://www.php.net/manual/en/language.types.array.php

Related

WC Ajax Product Filter - Price Range issue

I'm using WC Ajax Product Filter for filtering options .
Unfortunately the price range is showing :
Min Price: ₹NaN
Max Price: ₹NaN
The actual price is not displaying. Please help me out . I'm new to woocommerce .
The issue found out !
The minimum and maximum price range difference for the products was very less. For example - minimum was $65 and maximum was $66, that was the issue.
I've found a temporary solution, edit the plugin file: wcapf.php :you can find this in the root of the plugin directory: wc-ajax-product-filter
Now search for the code: getPriceRange
This occurs in 2 spots around line #467 and line #773
On both 2 spots you see an if statement below which uses sizeof() function.
There seems to be some sort of issue with this.
I have fixed the issues of NaN by commenting out the entire if and else statement that uses the sizeof() function, see below code for example.
It's not the best solution, but well it works, untill update.. I will post this to plugin creator as well.
#469:
//if (sizeof($unfiltered_price_range) === 2) {
#522:
//}
#781:
//if (sizeof($price_range) > 2) {
#840:
//} else {
// empty array
// return array();
//}
Solved it this way.
Edit widget-price-filter.php file.
It's in the folder /ajax-product-filter/widgets/
Replace the following code in line 125:
<span class="wcapf-slider-value" id="wcapf-noui-slider-value-min"></span> - <span class="wcapf-slider-value" id="wcapf-noui-slider-value-max"></span>
By:
if ($min_val!=0 && $max_val!=0 ) {
echo '<span class="wcapf-slider-value" id="wcapf-noui-slider-value-min"></span> - <span class="wcapf-slider-value" id="wcapf-noui-slider-value-max"></span>';
} else{ echo '-';}?>
It will replace the NaN values bellow the slider by (-), just change the last line if you want another text there.

Using != equals in php to remove unwanted activities rather than include the long list of wanted ones

I have a buddypress site and I have edited the activity stream to include the activities I want to see however it's more logical to filter out the unwanted so future plugins don't have to be manually included I have tried != but does not work.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ).'&action=bbp_reply_create,last_activity,gmw_location,activity_liked,rtmedia_update,new_avatar,updated_profile,joined_group,new_blog_post,bbp_topic_create,created_group' ) ) : ?>
And here is what I tried.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ).'&action!=friendship_created,new_member' ) ) : ?>
But the attempt simply left the whole stream blank.
I've never used Buddypress, but from what I see you cannot simply use the != operator. You are placing the != in a string that is the input for a function named bp_ajax_querystring(). From the function's name, and the rest of the input, it looks like a set of GET parameters.
GET parameters only allow the setting of attributes (so != has no meaning). Instead, place the ! in front of the expression being evaluated by the if statement:
if ( !bp_has_activities( bp_ajax_querystring( 'activity' ).'&action=bbp_reply_create,last_activity,gmw_location,activity_liked,rtmedia_update,new_avatar,updated_profile,joined_group,new_blog_post,bbp_topic_create,created_group' )

Multiple query are not inserting data Wordpress

I wrote a function where two insert query have. One is executed and inserting data properly. But next one is not executing. And I cant check the value i want to insert if it is set or not. How to do the stuff? EXPERT's have a look kindly. My function is given below:
add_action( 'save_post', 'cs_product_save' );
function cs_product_save( $post_id ){
global $wpdb;
$cs_product_array = $_POST['cs_product'];
$cs_product_count = count($cs_product_array);
$event_start_date = $_POST['event_start_date'];
$event_end_date = $_POST['event_end_date'];
$event_start_time = $_POST['event_start_time'];
$event_end_time = $_POST['event_end_time'];
$event_all_day = $_POST['event_all_day'];
$event_phone = $_POST['event_phone'];
$event_location = $_POST['event_location'];
$event_map = $_POST['event_map'];
$table_cause_product = "wp_cause_woocommerce_product";
$table_event_info = "wp_cause_info";
for( $i=0; $i < $cs_product_count; $i++ ){
$wpdb->insert($table_cause_product,array(
'cause_ID'=>$post_id,
'product_ID'=>$cs_product_array[$i],
'status'=>'1'
),array('%d','%d','%d'));
}
$wpdb->insert($table_event_info,array(
'cause_ID'=>$post_id,
'event_start_date'=>$event_start_date,
'event_end_date'=>$event_end_date,
'event_start_time'=>$event_start_time,
'event_end_time'=>$event_end_time,
'event_all_day'=>$event_all_day,
'event_phone'=>$event_phone,
'event_location'=>$event_location,
'event_map'=>$event_map
),array('%d','%s','%s','%s','%s','%d','%s','%s','%d'));
}
I don't see any problem here with your code. But be sure to double check your code.
The issues my be with the name of your database table names. Are you sure that $table_cause_product and $table_event_info holds the actual name of the tables? I would recommend to use $wpdb->prefix instead of harcoding table names.
In my case I would check the function in several parts.
Check the $_POST actually holds the data I want.
Use $result = $wpdb->insert( $table, $data, $format ); in all cases for debug purpose as the $result would hold the result of the operation. If its is false then I would be sure that there is definitely something wrong with the operation.
Finally I would use a wp_die() (though its not a standard way to do, but it suffices my purpose) so that I can see the dumped variable data.
One major issue you might face with your code that if the post is edited after saving then it might insert another row for same post data. Again if the post is being autosaved, you need some safeguard. I would recommend a where clause here to check the row already exists or not. If exists then you can simply update the row, or else insert the data.
Hope this might help you.

$wpdb query adding extra value during update query

I am very baffled by this problem.
The below is the very simple function to update a column by adding some more value:
public function add_user_to_new_post_sub($email, $sub_post_type) {
global $wpdb;
$add_setting = "|||".$sub_post_type;
//echo $add_setting; exit;
$wpdb->query(
"UPDATE $this->subscriptions_table
SET subscription_settings = concat(subscription_settings, '$add_setting')
WHERE user_key = '$key'"
);
}
For some reason, the $sub_post_type is always added 2 times. As an example, if the subscription_settings column has apple in it, and $sub_post_type = orange, the end result after the query would be apple|||orange|||orange. I don't understand why the extra value being added. I even did a sanity check with echo to make sure I am not passing things twice, and I am not.
Please help, I have been struggling for some time now.
I found my reason; the function was being called again through another if statement. doh

mysql_fetch_row loop?

I set a session variable upon login, I want to find all rows in a table that have the username in the "Createdby" field and I want to list them on a page. I'm using this code:
<?php
$result = mysql_query("SELECT email FROM members WHERE createdby = '" . $_SESSION['myusername'] ."'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
while($row = mysql_fetch_row($result))
{
echo ($row[0]);
}
?>
It works great but it doesn't space them, it echoes out like: data1data2 and not separate like data1, data2. How can I customize the results without messing it up? I tried to add
echo ("<p>".$row[0]."</p>");
But received: 11, I'm kind of new to PHP.
This is simply string concatenation. You're adding strings to other strings.
I'd suggest the following just to get started:
echo $row[0].", ";
I also suggest you read up on PHP more, as this is a basic concept in the language.
You can add spaces between each $row[0] like this:
echo $row[0] . ' ';
The dot followed by a space means that you're concatenating a space.

Resources