Check if current user has a especific value on a profile field - php

I have a site with buddypress and I need to check if current user has a specific value on specific field
I was trying this but it's not working
<?php
$data= bp_profile_field_data( array('user_id'=>get_current_user_id( 'ID' )));
if($data=='Funny Guy') {
echo 'yes';
} else {
echo 'no';
}
?>
Any hints why?

Try using bp_get_profile_field_data instead and provide the name of the field you need. So:
$args = array(
'field' => 'Your field name', // Exact field name or field ID.
'user_id' => bp_loggedin_user_id() // ID of logged in user
);
$data = bp_get_profile_field_data( $args );
if ( $data == 'Funny Guy' ) {
echo 'yes';
} else {
echo 'no';
}

Related

If form field is blank then show error message and form does not submitted

I am trying to create a Api.I am trying to submit a form . Form is submitted and response message after form submission is Ok .but I am trying to validate this form if any filed is blank then form does not submitted and show validation message.
<?php
require('../../../../wp-blog-header.php');
header("HTTP/1.1 200 OK");
global $wpdb;
global $serverUrl;
global $current_user;
$id =isset($_REQUEST['store_id']) ? $_REQUEST['store_id'] : '';
$user_id =isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
$unit_data = isset($_REQUEST['unit_data']) ? $_REQUEST['unit_data'] : '';
$product = isset($_REQUEST['product']) ? $_REQUEST['product'] : '';
$checked_by = isset($_REQUEST['checked_by']) ? $_REQUEST['checked_by'] : '';
$username = isset($_POST['username']) ? $_POST['username'] : '';
$option = "signup";
$data = array(
'store_id' =>$id,
'user_id' =>$uid,
'unit_data' =>$unit_data,
'category_name'=>'delivery_form',
'checked_by'=>$checked_by,
'product' =>$product,
'suppliers'=>$suppliers,
'username' =>$username,
);
$insert=$wpdb->insert('diary_user_form_storage', $data);
echo json_encode(
array(
"status" => "1",
'user_id' => $user->ID,
'message' => 'Data submitted',
"token" => $token,
'token' => $token.$device_id.$device_type,
'serverUrl' => $serverUrl,
$data = array(
'store_id' =>$id,
'user_id' =>$uid,
'unit_data' =>$unit_data,
'product' =>$product,
'suppliers'=>$suppliers,
'checked_by'=>$checked_by,
'username' =>$username,
'option'=>$option
)
));
exit();
?>
If you want to stick to PHP, a simple:
if($value == ""){$value_err = "This field is required"};
And then have $value_err in a red span next to the content that needs an input. Also you can use the same practice:
$if(value_err == ""){
//Submit Values to Database
}
else{
//Don't
}
To submit your values or not.
You can also use JavaScript to check it dynamically using an onblur on one of your inputs you require to be validated, and then using
function checkEmpty(id, idErr){
var z = document.getElementById("myForm");
z.addEventListener("blur", checkEmpty, true);
var x = document.getElementById(id);
var y = document.getElementById(idErr);
if(x.value == ""){
//If direct value
y.value = "This field is required.";
//If inside a span as PHP example above
//y.innerHTML = "This field is required.";
}
}

Insert session userdata into a new database in CodeIgniter

How would I go about inserting the session userdate id into my new table using the form I have in my view so that I may use the id as a foreign key?
Controller:
function validate_credentials() {
$this->load->model('member_model');
$query = $this->member_model->validate();
if ($query) { // if user credentials validate the user session start
$data = array(
'username' => $query->username,
'id' => $query->id,
'first_name'=>$query->first_name,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('members/members_area');
} else {
$this->index();
echo 'Incorrect Password or Username';
}
}
View:
echo form_open('banks/create_bank');
echo form_input('bank_name', set_value('bank_name', 'Account Name'));
echo form_input('interest', set_value('interest', 'Interest'));
echo form_input('start_amount', set_value('start_amount', 'Starting Balance'));
echo form_input('length', set_value('length', 'Length'));
echo form_submit('submit', 'Create Account')
echo validation_errors('<p class="error"/>');
Try this :
echo form_open('banks/create_bank');
echo form_input('bank_name', set_value('bank_name', 'Account Name'));
echo form_input('interest', set_value('interest', 'Interest'));
echo form_input('start_amount', set_value('start_amount', 'Starting Balance'));
echo form_input('length', set_value('length', 'Length'));
**echo form_input('id', set_value('id', $this->session->userdata('id'));**
echo form_submit('submit', 'Create Account')
echo validation_errors('<p class="error"/>');
Note : where 'id' in first parameter of form_input function can be replace by any name you want to pass when form will be submit.
In the method used in the controller banks where you handle the posted data from the from.
add something like this:
$data = array('bank_name' => $this->input->post('bank_name'), // add more post data
'id' => $this->session->userdata('id') ); //this will take the
//value 'id' from session
and pass it to your respective model.

How do I implement search filter query using mongodb?

i have few search filter user like the following image. User can select any one two or both
these are my filters language,format and status I have written a query but its not working
$lang_id =2;
$format = ''; //user not selected
$status = ''; //user not selected
$request = $collection->find(array
( '$and' => array(
array(
'language' => $lang_id ,
),
array(
'format' => $format,
),
array(
'status' => $status,
)
)
));
I have check with or also then also its not working
if filters are empty no need to find the empty field but if it is not empty need to find the field .
Please give me a solution I am new in mongodb
Thank you
As a solution to above mentioned issue please try executing following code snippet
$lang_id = 2;
$format = ''; //user not selected
$status = ''; //user not selected
$filter=array();
if(!empty($lang_id))
{
$filter['language']=$lang_id;
}
if(!empty($format))
{
$filter['format']=$format;
}
if(!empty($status))
{
$filter['status']=$status;
}
$request = $collection->find($filter);

Having issues with the from

I have an issue with the form. For some reason it does not populate the table I named About
Table info (About):
user_id,
aboutme_id,
about,
Table info (Users):
user_id,
...,
...,
...,
etc.
Form:
$this->addElement('textarea', 'description', array(
'label' => 'Body Content',
'required' => true,
'allowEmpty' => false,
));
Verification
public function onProcess()
{
// In this case, the step was placed before the account step.
// Register a hook to this method for onUserCreateAfter
if( !$this->_registry->user ) {
// Register temporary hook
Engine_Hooks_Dispatcher::getInstance()->addEvent('onUserCreateAfter', array(
'callback' => array($this, 'onProcess'),
));
return;
}
$user = $this->_registry->user;
$data = $this->getSession()->data;
$form = $this->getForm();
if( !$this->_skip && !$this->getSession()->skip ) {
if( $form->isValid($data) ) {
$values = $form->getValues();
$table = Engine_Api::_()->getDbtable('about', 'user');
$rName = $table->info('name');
$userTable = Engine_Api::_()->getDbtable('users', 'user')->info('name');
$select = $select
->setIntegrityCheck(false)
->from($userTable)
->joinLeft($userTable, "$userTable.user_id = $rName.user_id")
->where($userTable, "$userTable.user_id = $rName.user_id");
}
}
}
Any idea what is going on with this picture?
Try to test database table operations in a sample empty action of some controller using echo "<pre>"; print_r(); echo "</pre>"; die(); to debug results. You can also use $select->__toString() to get raw sql of constructed query.
Also take a look at /temporary/log/main.log for some notices/errors.

jQuery Auto Complete Return Data - Showing the ID aswell as label

I am using Codeigniter to create an Autocomplete for user names:
Using the parameter : search-user?term=s I get the following data back as Json :
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"}
However when I am running the auto select, On the search for say "St" it brings back by name in the options, But also the ID as well.
How do I prevent this from happening? I suspect my loop could be the issue.
My PHP Function is as follows :
function search_user()
{
$term = $this->input->get('term');
$user_search = $this->vendor_model->search_user($term);
$user['response'] = 'false';
if(count($user_search) > 0)
{
$user['response'] = 'true';
$user = array();
foreach($user_search as $user)
{
$user = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
echo json_encode($user);
}
}
}
{"id":"1","value":"Stuart Blackett"}{"id":"2","value":"Simon Wilton"} isn't valid JSON.
Try not echoing each $user's information separately - instead, build a new array of users and json_encode() that array. Example:
foreach($user_search as $user) {
$users[] = array(
'id' => $user->user_id,
'value' => $user->user_firstname .' '. $user->user_surname
);
}
echo json_encode($users);

Categories