PHP check if not empty and the value of another GET - php

I have a POS and need to check 2 things before I save a sale in MySQL
Code to get and check the information I need
$invoice=mysqli_real_escape_string($db,$_POST['same_invoice']);
$customer=mysqli_real_escape_string($db,$_POST['same_customer']);
If invoice is empty the customer is also empty because is the first prodyct in this invoice so we assign a invoice number and we ask the customer to show identification, the invoice is generated automatic so we input the ID-number of the customer and save to MySQL
Now the problem
If the customer wants to buy 2 products we dont need to give the order a new invoice number neither the ID but we must check both in MySQL
code I am trying but not working
if( $invoice is empty && $customer is also empty)
{ we create new invoice an ask customer for id first time }
if( $invoice is empty && $customer is NOT empty)
{ we create new invoice but do not ask customer for id }
else
{ we use the $invoice and $customer comming from the form }
Any idea how to check that?

You should check that the $_POST values are actually set, first. I would guess that is where your error is coming from.
$invoice = (isset($POST['same_invoice']) ? $_POST['same_invoice'] : null;
$customer = (isset($POST['same_customer']) ? $_POST['customer'] : null;
if(empty($customer) || empty($invoice)) {
//Use values from form
} else {
//Create new invoice
}

Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.
$invoice=mysqli_real_escape_string($db,$_POST['same_invoice']);
$customer=mysqli_real_escape_string($db,$_POST['same_customer']);
if(!empty($invoice)&&!empty($invoice)){
//do whatever you want to do with it.
}
No warning is generated if the variable does not exist. That means
empty() is essentially the concise equivalent to !isset($var) || $var
== false.

Related

How to prevent manually set url in laravel

I create one quiz app. In which students attend the question and click on the next button next question appears. now the issue is that the student is on the second question and the URL like "takeQuizStudent/149?page=2" and he manually changes URL to "takeQuizStudent/149?page=1". so it should not happen. Once clicks on the next question he can't go back to the previous question.
You should check request page number from session in your controller,for example
public function show(Request $request, $id)
{
$value = $request->session()->get('pageNumber'); //2
//$id = 1
if($id < $value){
return response("invalid request",504);
}
//else update pageNumber value and do continue
// Via a request instance...
$request->session()->put('pageNumber', '3');
// Via the global helper...
session(['pageNumber' => '3']);
.....
}
Determining If An Item Exists In The Session
To determine if an item is present in the session, you may use the has method. The has method returns true if the item is present and is not null:
if ($request->session()->has('pageNumber')) {
//
}
To determine if an item is present in the session, even if its value is null, you may use the exists method. The exists method returns true if the item is present:
if ($request->session()->exists('pageNumber')) {
//
}

How to properly display a calculated value of a non-db field in the ListView?

In Suitecrm/SugarCRM 6.5
I need to modify the ListView. Showing the value of a field based on a certain condition.
In the Account module, the name field may be empty based on certain conditions, I require that when this occurs show the value of a custom field. To do this, first I tryied using 'customCode' + smarty as usual in Edit/Detail vies, but in several posts mention that this is not possible in the ListView. The concession is to use logic hooks + a non-db field to store the calculated field. Following this SO answer I have written the following:
Custom field at custom/Extension/modules/Accounts/Ext/Vardefs/custom_field_name_c.php
<?php
$dictionary['Account']['fields']['name_c']['name'] = 'account_name_c';
$dictionary['Account']['fields']['name_c']['vname'] = 'LBL_ACCOUNT_NAME_C';
$dictionary['Account']['fields']['name_c']['type'] = 'varchar';
$dictionary['Account']['fields']['name_c']['len'] = '255';
$dictionary['Account']['fields']['name_c']['source'] = 'non-db';
$dictionary['Account']['fields']['name_c']['dbType'] = 'non-db';
$dictionary['Account']['fields']['name_c']['studio'] = 'visible';
Label at suitecrm/custom/Extension/modules/Accounts/Ext/Language/es_ES.account_name_c.php
<?php
$mod_strings['LBL_ACCOUNT_NAME_C'] = 'Nombre de cuenta';
Logic hook entry at custom/modules/Accounts/logic_hooks.php
$hook_array['process_record'] = Array();
$hook_array['process_record'][] = Array(
2,
'Get proper name',
'custom/modules/Accounts/hooks/ListViewLogicHook.php',
'ListViewLogicHook',
'getProperName'
);
Logic hook at custom/modules/Accounts/hooks/ListViewLogicHook.php
<?php
class ListViewLogicHook
{
public function getProperName(&$bean, $event, $arguments)
{
if (empty($bean->fetched_row['name'])) {
$bean->account_name_c = $bean->fetched_row['person_name_c'];
} else {
$bean->account_name_c = $bean->fetched_row['name'];
}
// Here I also tried, but same result
// $bean->account_name_c = empty($bean->name) ? $bean->person_name_c : $bean->name;
}
}
listviewdefs.php at custom/modules/Accounts/metadata/listviewdefs.php
I added the field
'NAME_C' =>
array (
'width' => '20%',
'label' => 'LBL_ACCOUNT_NAME_C',
'default' => true,
),
After these modifications and repair I hope to see the field full with the right value. But it appears empty. I have verified that the logic hook is properly called, but the name andperson_name_c fields are always empty. In case it is relevant I have verified in Studio that the name fields in Account is typename I do not know what this means when it comes to obtaining its value.
I appreciate your comments
The problem is in the logic hook is due to first that the $bean does not have access to the custom fields, so I have to invoke them using $bean->custom_fields->retrieve(); Also the name field is always empty, I had to use DBManager to get only the name field.
The logic of the final logic hook is the following:
<?php
class ListViewLogicHook
{
public function getProperName($bean, $event, $arguments)
{
// Get access to custom fields from $bean
$bean->custom_fields->retrieve();
// Get access to name property using DBManager because $bean->name return null
$sql = "SELECT name FROM accounts WHERE id = '{$bean->id}'";
$name = $GLOBALS['db']->getOne($sql);
// Assign a value to non-db field
$bean->name_c = empty($name) ? $bean->nombre_persona_c : $name;
}
}
I was not familiar with the method $bean->custom_fields->retrieve() and at the moment I do not know why is empty the name field and I understand others fields remain empty.
I hope this is useful
We achieved that doing an after_retrieve hook, very fast and simple - This is taken from a working example.
public function RemoveCost(&$bean, $event, $arguments)
{
global $current_user;
include_once(‘modules/ACLRoles/ACLRole.php’);
$roles = ACLRole::getUserRoleNames($current_user->id);
if(!array_search("CanSeeCostRoleName",$roles)){
$bean->cost = 0;
$bean->cost_usdollar = 0;
}
}
All you need is to define and add this function to the module logic_hooks.php
You can even tailor down to specific calls:
if (isset($_REQUEST['module'],$_REQUEST['action']) && $_REQUEST['module'] == 'Opportunities' && $_REQUEST['action'] == 'DetailView')
As some times there are views you do want to show the field, for example quicksearch popup.

Wordpress: how can I check if a specific value exists in a custom field without specifying a post ID?

I would like to check, if a given number has been attributed to a custom field.
How can I do that, without specifying the post ID?
details:
I created a custom post type in which I store a client number and wether or not the client has booked a service.
The service is provided by handing the client a little code snippet (iframe, source is the url to said file in their website) so they can display some information.
I know want to do the following: I attach the clientnumber to the given url, like so: www.example-url.org?nr=123
If there is no number after the url, some fail message is displaied (I already managed to do that).
Now I want to check if the client number exists at all in my database. And only then should the booked information be displaied.
I checked the get_post_meta, but I need the post ID. Can I check wether the number exists without knowing the post ID?
Try this snippet,
global $wpdb;
$metas = $wpdb->get_results(
$wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta where meta_key = %s", 'add_your_key_here')
);
echo '<pre>';
print_r( $metas );
echo '</pre>';
Hope this will helps you.
For more information,
How we get_post_meta without post id
You can achieve using custom query in wordpress. For that I create one function which will help you to check if this customer number is exist or not. Please have a look at below method.
$clinetnumber=get_meta_value_using_key($meta_key); // call the method
// check if custom number is exist or not
if(!empty($clinetnumber) && $clinetnumber!=0)
{
echo "Customer number is already registered";
}
else
{
echo "Customer number is not registered";
}
function get_meta_value_using_key($meta_key)
{
global $wpdb;
$query="SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s "; // meta query by key
$result=$wpdb->prepare($query , $meta_key);
$value = $wpdb->get_var($result); // get result of quesry
return $value; // return value
}

Issues with getting row from my database and turning it into string laravel

this is my code
public function GetUsersWhoNeedPayments()
{
$All_DB_Email_Info = DB::table('users')->pluck('email');
foreach($All_DB_Email_Info as $ADEI){
$DisplayUserToReceivePaymentFrom = Auth::user()->where([
['paired_with', '=', $ADEI]
])->get();
var_dump($DisplayUserToReceivePaymentFrom->name);
}
}
So I created the variable $All_DB_Email_Info which selects the email column. my goal is to get the email saved in my DB...every user is paired to make payment to other users...so i am paired to make payment to johndoe#example.com,,i want the check the db for johndoe#example.com and then check for he's ->name, phonenumber->, ->age, etc..but when i reload my page the page says 'Exception in Colection.php line 1498: Property [name] does not exist on this collection instance.' Please help me out

Gravity Forms custom validation filter

I have a function that processes sales via a third-party service, processes the result and returns an array with the Status "Success" or "Invalid." This sales call is made using the gform_after_submission hook applied to the specific form.
What I need to do is store the "Success" or "Invalid" result in the array as a variable that I can later pass to a function to validate or invalidate the credit card field, using gform_validation hook.
I'm declaring the variable in a function, like so:
function foo {
...code to sell product through API...
$status = $checkoutShoppingCartRequest['Result']['Status'];
}
When I print the variable $status within the function, it is showing either Success or Invalid like it should.
Here is other function where I need to use this variable, passed to gform_validation, which fails every time regardless of Success or Invalid result:
function MBvalidate( $validation_result ) {
$form = $validation_result['form'];
if ( $status !== "Success") {
$validation_result['is_valid'] = false;
foreach( $form['fields'] as &$field ) {
if ( $field->id == '34' ) {
$field->failed_validation = true;
$field->validation_message = 'Your credit card could not be processed.';
break;
}
}
}
//Assign modified $form object back to the validation result
$validation_result['form'] = $form;
return $validation_result;
}
add_filter( 'gform_validation_47', 'MBvalidate' );
I have tried passing the variable a number of different ways, via globals and sessions, etc.
I am new to GF development so I am sure I'm missing something. I'd appreciate any direction.
The gform_after_submission action hook runs after gform_validation.
Anyway, assuming you can find a hook that runs earlier, what I would do is store a unique variable for each submitted form using the Transients API's set_transient() and get_transient() functions. For example you can create a hidden field in every form which you populate with a random ID. Use this random ID as a key to store and retrieve the Success/Invalid result.
$status here is a local variable which has never been defined before you try to use it in if-condition. So, it's always null.
Maybe you missed
$status = $validation_result['Result']['Status'];
or something like this before checking the condition.

Categories