PHP - Search through PHP array through input field? - php

<?php
$allBookings = [
"IMD002" => [
"client" => "James Holden",
"paid" => "yes",
"email" => "james.holden#gmail.com"
],
"IMD003" => [
"client" => "Harold P. Redman",
"paid" => "yes",
"email" => "HaroldPRedman#dayrep.com"
],
"IMD004" => [
"client" => "Marcus C. Nelson",
"paid" => "no",
"email" => "MarcusCNelson#armyspy.com"
]
];
// DONT EDIT ANYTHING BELOW HERE
if(!empty($_POST)) {
$clientName = $booking['client'];
}
else {
$clientName = null;
};
?>
<!-- DONT EDIT ANYTHING BELOW HERE -->
<div class="myApp">
<form method="post">
<input type="text" name="bookingReference" placeholder="Booking Reference">
<input type="submit" value="Find Client">
</form>
<h2 id="result_name">Client name: <em><?php echo $clientName; ?></em></h2>
</div>
How can I showup the credentials when I type into the input field (booking reference) for example "IMD002"? As it should show up the array listed in php under "IMD002". I'm assuming its something to do with a $_POST statement?
Thanks alot

After $allBookings = [ ... ]
Try adding something like :
if(isset($_POST['bookingReference']) &&
isset($allBookings[$_POST['bookingReference']])) {
$booking = $allBookings[$_POST['bookingReference']];
}
else {
$booking = ['client' => 'Invalid Ref/Not found'];
};

To search, Use $_POST['bookingReference'] as index to, $allBookings, your data array;
Try:
$allBookings[$_POST['bookingReference']];
Print the content:
print_r( $allBookings[$_POST['bookingReference']] );

Related

Laravel, store multiple variables from array into DB

Im not sure if the question title is correct but what I wanna do is clear in the example below
I have 3 arrays and I want to store data in one table
the table structure is like this
table name: tour_transports
id
transport_id
transport_track
transport_note
arrays coming from blade
"transport_id" => array:2 [
1 => "1"
5 => "5"
]
"transport_track" => array:3 [
1 => " from airport to the hotel"
3 => null
5 => " be ready at 4:pm"
]
"transport_note" => array:3 [
1 => "from hotel to the beach"
3 => null
5 => " bring ur swiming suite"
]
so as you see the ids are 1 and 5 so I wanna store two rows here
id = 1,transport_id = 1 , transport_track = from airport to the hotel , transport_note = be ready at 4:pm
id = 2,transport_id = 5, transport_track = from hotel to the beach , transport_note = bring ur swiming suite
I tried to do this but can't get it correct
if(!empty($request->transport_id))
{
foreach ($request->transport_id as $key => $transport_id) {
foreach ($request->transport_track as $key => $track) {
foreach ($request->transport_note as $key => $transport_note) {
TourTransport::create([
'transport_id' =>$transport_id,
'transport_track' =>$track[$transport_id],
'transport_note' =>$transport_note[$transport_id]
]);
}
}
}
}
You can use this code to achieve your results:
$data = $request->only('transport_id', 'transport_track', 'transport_note');
foreach ($data['transport_id'] as $key => $transportId) {
TourTransport::create([
'transport_id' => $transportId,
'transport_track' => $data['transport_track'][$key],
'transport_note' => $data['transport_note'][$key],
]);
}
Now $results contains the required data. However the better solution might be to configure your html form to send related data in single array entry, Like:
<input type="text" name="transports[0][transport_id]">
<input type="text" name="transports[0][transport_track]">
<input type="text" name="transports[0][transport_note]">
<input type="text" name="transports[1][transport_id]">
<input type="text" name="transports[1][transport_track]">
<input type="text" name="transports[1][transport_note]">
Which you can also use Javascript/php loops to generate the form inputs. Then you will receive form data in server side in well formed structure like:
"transports" => array:2 [
[
'transport_id' => 1,
'transport_track' => 'from airport to the hotel',
'transport_note' => 'be ready at 4:pm',
],
[
'transport_id' => 5,
'transport_track' => 'from hotel to the beach',
'transport_note' => ' bring ur swiming suite',
],
]

prefill not working in razorpay

$data = [
"key" => $api_key,
"amount" => $amount,
"name" => "DJ Tiesto",
"description" => "Tron Legacy",
"image" => "logo.png",
"prefill" => [
"name" => "Daft Punk",
"email" => "customer#merchant.com",
"contact" => "9999999999",
],
"notes" => [
"address" => "Hello World",
"merchant_order_id" => "12312321",
],
"theme" => [
"color" => "#F37254"
],
"order_id" => $razorpayOrderId,
];
I am trying to integrate razorpay with php everything is ok but in prefill, I get a default value. how can I change this value to custom value?
In prefill you will get the value which you are given in the prefill object. Just like you passsed $amount and $razorpayOrderId you can use $name and $email etc which might be the value you get from textbox or something like that. Store those names and email in a variable and pass that into prefill.
you can easily change the default value by fetching those values from database or pass those values through ulr parameters as i have done in php.
get order id through url parameter or post request
$orderid = $_GET['orderid'];
run php mysql query to fetch data from database based on order id(i
have used 2 table one for payment details and another one for
userdetails)
$query = "SELECT ud.Name, ud.lname,
ud.MobileId,ud.EmailID,opd.pay_amount, opd.receipt, opd.transaction_id
FROM online_payment_details as opd
INNER JOIN userdetails as ud
ON opd.user_id=ud.UserID
WHERE order_id='$orderid';";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($result);
$name = $row['Name'].' '.$row['lname'];
$razorpayOrderId = $orderid;
$amount_pay = $row['pay_amount'];
$receipt = $row['receipt'];
$transaction_id = $row['transaction_id'];
$displayAmount = $amount_pay ;
$corporateName = '';
$email = $row['EmailID'];
$contact = $row['MobileId'];
now in your data array add above variables in data array
$data = [
"key" => $keyId,
"amount" => $amount_pay,
"name" => $name,
"description" => "",
"image" => "",
"prefill" => [
"name" => $corporateName,
"email" => $email,
"contact" => $contact,
],
"notes" => [
"address" => "Hello World",
"merchant_order_id" => $transaction_id,
],
"theme" => [
"color" => "#F37254"
],
"order_id" => $razorpayOrderId,
"receipt" => $receipt,
];
and now in checkout/automatic page in block fetch values from
data array and store in data attribute as below
<script
src="https://checkout.razorpay.com/v1/checkout.js"
data-key="<?php echo $data['key']?>"
data-amount="<?php echo $data['amount']?>"
data-currency="INR"
data-name="<?php echo $data['name']?>"
data-image="<?php echo $data['image']?>"
data-description="<?php echo $data['description']?>"
data-prefill.name="<?php echo $data['prefill']['name']?>"
data-prefill.email="<?php echo $data['prefill']['email']?>"
data-prefill.contact="<?php echo $data['prefill']['contact']?>"
data-notes.shopping_order_id="<?php echo $data['receipt']?>"
data-order_id="<?php echo $data['order_id']?>"
<?php if ($displayCurrency !== 'INR') { ?> data-display_amount="<?php echo $data['display_amount']?>" <?php } ?>
<?php if ($displayCurrency !== 'INR') { ?> data-display_currency="<?php echo $data['display_currency']?>" <?php } ?>
>
</script>

PHP Creating a short array from a list

I want to dynamically create a short array with a list of objects. This is for a POST request with the Guzzle client. That's why I need it in a short array.
example of a Guzzle post request:
$res = $this->client->request($methode, $request_url, [
'form_params' => [
'param' => 'value'
]
]);
Problem case:
I have got a List: Params of Objects: Param.
Param has three attributes id, name, link_id.
Let's say the List has three Object.
param(1, email, 1)
param(2, username, 1)
param(3, password, 1)
I want to dynamically create from the list an array with the short array syntax.
Example(Pseudo):
for each params as param
[
'form_params' => [
param->name => 'value'
]
]
the result of this code will be like this
[
'form_params' => [
'email' => 'value',
'username' => 'value',
'password' => 'value'
]
]
Code example:
$params = array(
"param" => array (
"id" => "1",
"name" => "username",
"link_id" => "1",
)
);
$value = '';
$shortarray = '';
foreach($params as $key => $param){
$shortarray .= $param->name . '=>' . $value . ',';
}
$postParams = ['form_params' => [ . $shortarray . ]];
I really could use some help. Thank you in advance.
If you want to just show short array syntax than this solution may helps you to resolve you problem.
https://stackoverflow.com/a/35207172/4781882
I am not quite sure why you require it be a "short array", but here's a solution...
I assume this is a web form, yes?
So let's assume you name your forms in a way that is usable, like:
<form method="POST" action="yourform.html">
Line 1 <input name="email1"> | <input name="user1"> | <input name="password1">
<br/>-------<br/>
Line 2 <input name="email2"> | <input name="user2"> | <input name="password2">
<br/>-------<br/>
Line 3 <input name="email3"> | <input name="user3"> | <input name="password3">
</form>
Then...
for($i = 1; $i <= 3; $i++ {
$postparms[] = ['email' => $_POST['email'.$i], 'user' => $_POST['user'.$i], 'password' => $_POST['password'.$i] ];
}
print_r($postparms);

how to change select values when select an option from other drop down box in codeigniter

I have two drop down boxes its values are fetched from utility libray
public $email_hooks = array(
"create_user" => array(
"name" => "New user creation",
"keys" => array(
"site_url" => "Site URL",
"current_date" => "Current Date",
"user_name" => "User name",
"name" => "Name of the user",
"password" => "Password of user",
"user_email" => "Email address of new user"
),
"content_type" => array("html","text"),
"to" => array("User", "Site Admin"),
"cc" => array("Site Admin"),
"bcc" => array("Site Admin")
),
"register_user" => array(
"name" => "New user registration",
"keys" => array(
"site_url" => "Site URL",
"current_date" => "Current Date",
"user_name" => "User name",
"name" => "Name of the user",
"activation_url" => "Url for activation registered account",
"user_email" => "Email address of the user"
),
"content_type" => array("html","text"),
"to" => array("User", "Site Admin","new user"),
"cc" => array("Site Admin"),
"bcc" => array("Site Admin")
));
following are view code
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Name</label>
<div class="col-lg-4">
<select id ="choose" class="form-control" name="name">
<option value="">Select One</option>
<?php
foreach($email_hooks as $key=>$val)
{
echo '<option value="'.$key.'">'.$val['name'].'</option>';
}
?>
</select>
</div>
<div class="col-lg-6" id="email_tmpl"></div>
</div>
<div class="form-group col-lg-12">
<label class="control-label col-lg-2">Notification to</label>
<div class="col-lg-10">
<select id ="notification" class="form-control" name="notification">
<option value="">Select One</option>
</select>
</div>
</div>
When select first drop down box value,i want to change 2nd dropdown values ,ie "to" => array("User", "Site Admin","new user").these array values change according with the first selection box.
Write a javascript/jquery function and get the value from your first drop-down through java script or jquery. Then use onchange to call the function and then append the array accordingly to the second drop-down menu. Hope this works...
Your first Dropdown:
<select id ="choose" class="form-control" name="name" onchange="your_javascript_function()">
Use javascript function and use jquery append function and each function in it:
function your_javascript_function
{$.each(your_array, function() {
$('#notification').append( $('<option value="' + this.value_from_array + '">' + this.value_from_array + '</option>' ));
});
}

How to generate a pass from 2 different json_encodes

I use this method to generate a pass: public function setJSON($JSON) {
if(json_decode($JSON) !== false) {
$this->JSON = $JSON;
return true;
}
$this->sError = 'This is not a JSON string.';
return false;
}
I call this method to generate pass by: $pass->setJSON('{
"passTypeIdentifier": "'.$passTypeID.'",
"formatVersion": 1,
..............
"barcode": {
"altText" : "'.$alt.'",
"format" : "PKBarcodeFormatPDF417",
"message": "Member-Card",
"messageEncoding": "iso-8859-1",
"changeMessage" : "This pass now has altText %# !"
},
"locations" : [
{
"longitude" : 104.89529371261597,
"latitude" : 11.576150037278605,
"relevantText": "CamMob (dis. 1%)"
},
.....................
]
}');
But now I don't write those locations statically and I get those data from database by : $query5 = mysql_query("select * from company");
while ($row5 = mysql_fetch_array($query5)){
$companyName = $row5['relevantTextName'];
$discount = $row5['relevantTextDiscount'];
$long = $row5['longitute'];
$lat = $row5['latitute'];
$link = $row5['link'];
$location['locations'][] = array("longitute" => $long, "latitute" => $lat, "relevantText" => $companyName." (" . $discount. "%)" );
}
$jsonString = json_encode($location) ;
error_log("Locations: ".$jsonString,0);
$pass->setJSON($jsonString);
$pass->setJSON('{
"passTypeIdentifier": "'.$passTypeID.'",
"barcode": {
"altText" : "'.$alt.'",
"format" : "PKBarcodeFormatPDF417",
"message": "Member-Card",
"messageEncoding": "iso-8859-1",
"changeMessage" : "This pass now has altText %# !"
}
}');</pre>
There is no error when I test for updating pass, but I cannot see the pass on lock screen like before. Thus it means that I not yet include those locations to the pass. What should I change ? Is it the problem of method setJSON ? If I do like this, how can I combine these 2 jsons to become together ?
Rather than constructing the JSON directly, construct an array first, then transform it into JSON.
$pass_data = array("passTypeIdentifier" => $passTypeID,
"formatVersion" => 1,
"barcode" => array (
"altText" => $alt,
"format" => "PKBarcodeFormatPDF417",
"message" => "Member-Card",
"messageEncoding" => "utf-8", // use UTF8 in case you want to encode Khmer or other non ASCII
"changeMessage" => "This pass now has altText %# !"
),
"locations" => $locationsArray,
"organizationName" => "Digi club card",
"description" => "Membership card",
"logoText" => "Digiclub",
"foregroundColor" => "rgb(0,0,0)",
"backgroundColor" => "rgb(211,211,211)",
"generic" => array (
"headerFields" => array(
array ( "key" => "Status",
"label" => " ",
"value" => "Membership card"
),
),
"primaryFields" => array(
array ( "key" => "Name",
"value" => $memberName,
),
),
"secondaryFields" => array(
// Secondary Field data
),
"auxiliaryFields" => array(
// Auxiliary Field data
),
"backFields" => array(
// Backfiels Field data
),
)
);
Then transform the array into JSON:
$pass->setJSON(json_encode($pass_data));

Categories