Creating and using a multi-dimensional array from MySQL - php

Here's the line I'm having issues with:
array('jsonrpc' => '2.0', 'id' => 1, 'method' => 'getExchangeAmount', 'params' => array(array('from' => 'BTC', 'to' => 'LTC', 'amount' => '1'),array('from' => 'BTC', 'to' => 'ETH', 'amount' => '1')))
This is the specific part of the line I'm having issues with:
array(array('from' => 'BTC', 'to' => 'LTC', 'amount' => '1'),array('from' => 'BTC', 'to' => 'ETH', 'amount' => '1'))
Basically I'm using this script to compare prices of cryptocurrencies. The problem is that right now I'm having to enter all of them into the script manually.
However, I have a database table (trade_stats) that I'd prefer to get the details from.
Here's its layout:
id incoming outgoing
1 BTC ETH
2 BCH LTC
3 ETH BCH
What I'm wanting to do is pull the rows from my database and use them in the script, but I can't seem to figure out how to create a multi-dimensional array.
Any help or pointers in the right direction would be great.

I hope this helps you
<?php
$conn=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if (!$conn) {
die("Error Connecting To Database: ".mysqli_connect_error()."<br/>");
}
$sql="
SELECT * FROM trade_stats
";
$result=mysqli_query($conn,$sql);
if (mysqli_num_rows($result) > 0) {
while($row=mysqli_fetch_assoc($result)) {
$from=$row['incoming'];
$to=$row['outgoing'];
//$amount=$row['???'];
$array=array();
array_push($array, array('from' => $from, 'to' => $to, 'amount' => '1'));
print_r($array);
}
}
?>
I didn't set a value for mysqli connect so you can edit it yourself and suit it for your database.
+I don't know which column is for ((amount)) so I set 1 value for it.You can edit it yourself.
If you had any questions or if the code is not working as you wanted tell me I will edit it :)

Related

How to update data in laravel - there is array value in data

There is field affiliated_facility which is in array and i am trying to update my values:
My controller code:
<?php
$affiliated_facility = implode(",", $userProfile['affiliated_facility']);
$userBasicInfoId = UserBasicInfo::where('user_id', $userProfile['id'])->value('id')->update([
'work_phone' => $userProfile['work_phone'],
'fax' => $userProfile['fax'],
'extension' => $userProfile['extension'],
'primary_facility' => $userProfile['primary_facility'],
'employed_by' => $userProfile['employed_by'],
'emergency_phone' => $userProfile['emergency_phone'],
'designation' => $userProfile['designation'],
'department' => $userProfile['department'],
'employment_type' => $userProfile['employment_type'],
'biography' => $userProfile['biography'],
'hiring_date' => $userProfile['hiring_date'],
'from' => $userProfile['from'],
'to' => $userProfile['to'],
'affiliated_facility' => $affiliated_facility]); // this is in array so i used implode in top of this code
if ($userBasicInfoId) {
$userBasicInfo = $this->userBasicInfo->find($userBasicInfoId);
$userBasicInfo->fill($userProfile)->save();
} else {
$userBasicInfo = $this->userBasicInfo->create($request->only($this->userBasicInfo->getModel()->fillable));
}
?>
But when I hit my request it says
Call to a member function update() on integer
There is some mistakes in my code i want to update my record and there is one field coming which is in array
"affiliated_facility": [1,2]
can someone please help me to modify this code i am stuck on this your help will be highly appreciated!
Thankyou in advance
UserBasicInfo::where('user_id', $userProfile['id']) is a Builder instance. you should get Model instance to update it. So, try:
UserBasicInfo::where('user_id', $userProfile['id'])->first()->update([...]);

using wpdb to insert into SQL SERVER database with PhP

I am attempting to convert a php form script I created from an external MySql database to an external SQL server database. I have properly copied the database over and just seem to be having a problem getting the form to use the WPDB object to use insert into the new SQL server database. The same creation script was used for both databases.
The insert statement:
try {
$externalDB->insert('EmployeeApp.dbo.applicant', array(
'first_name' => $first_name,
'last_name' => $last_name,
'middle_name' => $middle_name,
'suffix' => $suffix,
'nick_name' => $nick_name,
'email' => $email,
'idnumber' => $idnumber,
'street_address' => $street_address,
'city' => $city,
'state' => $state,
'zip' => $zip,
'phone' => $phone,
'ged' => $ged,
'over_18' => $over_18,
'previous_app' => $previous_app,
'previous_app_date' =>$previous_app_date,
'previous_emp' => $previous_emp,
'previous_emp_date' => $previous_emp_date,
'us_citizen' => $us_citizen,
'visa' => $visa,
'us_eligible' => $us_eligible,
'date_avail' => $date_avail,
'name_change' => $name_change,
'name_change_info' => $name_change_info,
'discharged' => $discharged,
'military' => $military,
'military_info' => $military_info,
'high_school_name' => $high_school_name,
'high_school_city' => $high_school_city,
'high_school_state' => $high_school_state,
'highest_grade_completed' => $highest_grade_completed,
'additional_skills' => $additional_skills,
'convicted'=> $convicted,
'convicted_name' => $convicted_name,
'convicted_date' => $convicted_date,
'convicted_charge' => $convicted_charge,
'convicted_sentence' => $convicted_sentence,
));
$lastid = $externalDB->insert_id;
The database connection object:
try{
$externalDB = new wpdb(EMP_DB_USER,EMP_DB_PASS, EMP_DB_NAME, EMP_DB_HOST);
}catch(Exception $e){
echo $e->getmessage();
}
$wpdb has been declared as a global at the top of the script and this was working perfectly before I connected to a SQL server database.
I am wondering if I need to not use $externalDB->insert() with SQL or try a different table call such as just 'dbo.applicant' or just 'applicant'. Not sure about the transition and can’t find a lot of documentation on it.
If you look at the code, you will see that the wpdb class connects only to MySQL databases. This is an acknowledged limitation of Wordpress, and although there have been some attempts by third parties to rectify the situation, nothing official has happened yet!

How do I loop through an object and display its data within an associative array

First of all this is my code:
Log::create([
'action' => $event->changes[0], //<- This is my problem
'object_id' => $event->id,
'object_type' => "Account",
'ip_address' => Request::ip(),
'user' => ucfirst(Auth::user()->name),
'time' => Carbon::now()->format('H:i:s'),
'date' => Carbon::now()->format('Y-m-d')
]);
$event->changes is an array which contains many items but i only know how to get 1 specific item at a time using the index [0] or [1] etc.
How do I get all the values to display instead of 1 at a time? Obviously I don't want to create a new log for each single action but I cant figure out how to do this.
As always any help is appreciated thank you.
You need to serialize the $event->changes if you want it within one log entry.
It actually depends on the structure of changes array, but it seems to be an array of strings, so you may for example use the implode(', ', $changes), so the snippet would look the following:
Log::create([
'action' => implode(', ', $event->changes), //<- This is my problem
'object_id' => $event->id,
'object_type' => "Account",
'ip_address' => Request::ip(),
'user' => ucfirst(Auth::user()->name),
'time' => Carbon::now()->format('H:i:s'),
'date' => Carbon::now()->format('Y-m-d')
]);

Codeigniter adding data to database and then process it

I am using codeigniter to built a type of online shop.
I want to create a process order function in order to verify the details of the order the clients puts in.
I am stuck though because on the last page i have the data to submit and when i click i go to main/process_order where i insert the data in the table and then use curl to comunicate with another server.
My question is: when i hit submit and then stop on the process_order page if i reload it 1000 times, the table will be filled with the same 1000 lines, so this can be a security issue. Also if i make a function to add the data to db and then redirect to process_order it will be another issue because i still need my data that was posted.
What's the best way to solve this. I hope i made it as clear as i can.
Code:
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
$this->db->insert('orders_temp', $data);
Maybe something like this can help. Do a check on the userid and the time before you insert the data. You would need make sure to do a search on database fields that are unique (unless the time field is the full time)
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
if (<!--CONDITIONAL STATEMENT-->)
{
$this->db->insert('orders_temp', $data);
}

Troubleshooting dynamic dropdowns in SugarCRM 6.5

I had asked a question here a while back about setting up database populated dropdowns for SugarCRM. I received a really good answer and, after more php studies and a dev instance running, I decided to give it a shot. The instructions I followed can be found here. After I run the repair and rebuild, I would expect to see the custom field in my Fields list under the module in studio, but have not been able to find it. The module is named Makers (a1_makers as a database table). For good orders sake, there were no errors when I repaired/rebuilt after saving the files. Per the instructions, I first created a php file with a custom function to query the database (custom/Extension/application/Ext/Utils/getMakers.php):
<?php
function getMakers() {
static $makers = null;
if (!$makers){
global $db;
$query = "SELECT id, name FROM a1_maker";
$result = $db->query($query, false);
$accounts = array();
$accounts[''] = '';
while (($row = $db->fetchByAssoc($result)) !=null) {
$accounts[$row['id']] = $row['name'];
}
}
return $makers;
}
?>
Then, I set 'function' field in Vardefs to point to the function (custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php):
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'len' => '100',
'comment' => 'List of makers populated from the database',
);
?>
Unfortunately, there are no errors and the repair/rebuild runs fine. I am just unable to see the custom field when I go into studio. Can anyone please help point out what I may be doing wrong?
I would recommend checking existence of newly created field 'list_of_makers' in cache/modules/Maker/Makervardefs.php file. If new field definition exists in that file, try add 'studio' => 'visible' to custom/Extension/modules/Maker/Ext/Vardefs/makers_template.php to get something like this:
<?php
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
);
Try to edit your custom/modules/Maker/metadata/editviewdefs.php manually and insert field definition by hand in proper place if everything above won't work.
$dictionary['Maker']['fields']['list_of_makers'] = array (
'name' => 'list_of_makers',
'vname' => 'LBL_MKRLST'
'function' => 'getMakers',
'type' => 'enum',
'studio' => 'visible'
'len' => '100',
'comment' => 'List of makers populated from the database',
'studio' => array(
'listview' => true,
'detailview' => true,
'editview' => true
),
);

Categories