how to insert array to a field in db - wordpress - mysql

I want to insert values to database each time when values are submitted.
These are the post values from the form.
$sFloorKey = $_POST['floorname']; // int value : eg - 5
$aRoomName = $_POST['roomname']; //array contaning multiple values
$iRoomType = $_POST['roomtype']; //array contaning multiple values
$iPostID = $_POST['postid']; // int value : eg - 44
The wp query performed ,
$wpdb->insert('bs_room_types', array(
'postid' => $iPostID ,
'sfloorname' => $sFloorKey,
'aroomname' => $aRoomName,
'aroomtypes' => $iRoomType,
));
The $sFloorKey and $iPostID are getting inserted but the array values not.
Please help out here!

As per the help of #MilanChheda its working perfectly now.
Before inserting i have serialized the values.
$iPostID = $_POST['postid'];
$sFloorKey = $_POST['floorname'];
$aRooms = serialize($_POST['roomname']);
$iRoomType = serialize($_POST['roomtype']);
$wpdb->insert('bs_room_types', array(
'postid' => $iPostID,
'sfloorname' => $sFloorKey,
'aroomname' => $aRooms,
'aroomtypes' => $iRoomType,
));

Related

Codeigniter: Know AI ID of query

When a user create an item, on my controller i need to send 2 SQL Query.
The first is easy to do:
$data = array(
'author' => $this->input->post('author'),
'name' => $this->input->post('name'),
);
$this->item_model->insertItem($data);
But on my second query, i need to recover, to find the ID of the query showing just before.
For example:
$data = array(
'user_id' => $_SESSION['user_id'],
'item_id' => ????,
);
$this->item_model->insertItem($data);
Thanks
For fetching the last inserted ID of variable $item_id in the same transaction of your controller, you can get the ID of record in the same session as follows:
$item_id = $this->db->insert_id();

Aloglia records are updating with wrong datatypes

I am facing some issues in algolia records. There are some attributes in me People table (e.g. id(int), user_id(int), name(varchar) ... etc).
When I add new records through Laravel 5.5 to database then everything works fine at algolia but I try to update record then although records are updating perfectly but the datatypes are changing. Like before Updating record user_id is int = 6 after updating the name or something else the user_id is converting to varchar implicitly.
I dont know whats happening.
before updating this record:
After updating the same record:
The insertion code is:
$new_person = People::create([
'user_id' => Auth::user()->id,
'stage_id' => 0,
'source_id' => 0,
'agent_id' => 0,
'custom_field_id' => 0,
'fname' => $request->first_name,
'lname' => $request->last_name,
'phone' => $request->phone,
'email' => $request->email,
'avatar' => "/images/user.png",
'name_slug' => createPeopleSlug($request->first_name , $request->last_name)
]);
The code for update is:
$person = People::find($request->personID);
$person->tags = $tagsString;
$person->save();
I am not expert of Aloglia
This issue will be resolved by adding Laravel casts variable to your Model, simple.
protected $casts = [
'user_id' => 'integer',
'agent_id' => 'integer',
'company_person_id' => 'integer',
];
Cheers!

Yii2 getting columns name from SQL command when result == null

If you get the following code :
$DBConnection =
CreateNewDBConnection(Yii::$app->get('db_cdh'),$aDatabaseName);
$DBConnection->open();
$command = $DBConnection->createCommand($aQuery);
$queryres = $command->queryAll();
If there is result from the query, I get an array, like this
Array
(
[0] => Array
(
[name] => 2.6.084.545
[xdim+2] => 70
)
[1] => Array
(
[name] => 2.5.102.030
[xdim+2] => 60
)
[2] => Array
(
[name] => 2.5.141.560
[xdim+2] => 80
)
)
But if the result of the query is empty, i get an empty array.
How is it possible to get the columns name ?
The reason why I'm asking this, it's because I'm asking queries to multiple DBs and some have results (1 or more lines) and otherd not. The system almost works, but the grid view parse only the first line to find the columns to display. So depending on the order result across the multiple DB, the grid view display the columns or not, depending what come first ....
Any help welcome.
You can take the column names using yii\db\TableSchema and use them afterwards:
$columns = [];
if (empty($queryres)) {
$columns = $DBConnection->getTableSchema('your_table_name')->getColumnNames();
}

Cakephp query for enum field in database

I have a field in database. It's type is enum and it looks like
enum('NO ANSWER', 'ANSWERED', 'BUSY').
I need to put this values into dropdown. How can I write query in cakephp?
I tried:
$result = TableRegistry::get('Calls')->find('list', ['valueField' => 'disposition'])->distinct('disposition')->toArray();
But it returns
[
(int) 1 => null,
(int) 77 => '',
(int) 64 => 'NO ANSWER',
(int) 65 => 'ANSWERED',
(int) 72 => 'BUSY'
]
but I need something like this:
[
(int) 1 => 'NO ANSWER',
(int) 2 => 'ANSWERED',
(int) 3 => 'BUSY'
]
I need to put this values into dropdown
Unless the enum values are going to change frequently (and if the are, why would you use an enum..) just put the array of data you need somewhere:
$options = [
'NO ANSWER' => 'NO ANSWER',
'ANSWERED' => 'ANSWERED',
'BUSY' => 'BUSY'
];
And then use it:
echo $this->Form->select('field', $options);
Note that the key in $options is what will be submitted, the value is what will be displayed. More info about the select method is in the documentation.
I found this answer somewhere on SO, but I couldn't find it again. You can do this:
$cols = $this->Model->query("show columns from table_name like 'enum_column_name'")
$enum = explode(',', substr(str_replace(array("'", "(", ")"),'',$cols[0]['COLUMNS']['Type']), 4));
$options = array_combine($enum, $enum);
Then in your form, you can use the end of AD7six's answer and add:
echo $this->Form->select('field', $options);
The problem is that the values of enum are defined in the create table, they are not a piece of data available when you query your table's data. How can I get enum possible values in a MySQL database? SO topic describes how to get the values of the enum through a php code. Just make sure that you reassign the keys for the enum values so that the keys start from 1, and not from 0 (0 stands for empty value).

Cakephp find joining two columns

I have a BD that contains people with day and month fields, like so:
person1 (day = 5; month = 3)
person2 (day = 2; month = 12)
I have to perform a find between 2 dates, for example, I need all people between 01/03 and 01/06 (day/month) but I don't know how to perform that.
I tried using separate conditions, like this:
$conditions['People.day >='] = dayA;
$conditions['People.month >='] = monthA;
$conditions['People.day <='] = dayB;
$conditions['People.month <='] = monthB;
But, that's not correct because it finds day and month, I mean, it finds People between monthA and monthB and People between dayA and dayB, instead, what I need is people between dayA/monthA and dayB/monthB
I suppose I must do some kind of a JOIN, but I'm lost here, I looked some information but I don't know where to start.
updated info:
ok, I'm using this
Array
(
[People.month_day BETWEEN ? AND ?] => Array
(
[0] => 01/02
[1] => 28/02
)
)
but I get people like this:
1/1
2/1
10/1
11/1
12/1
13/1
20/1
21/1
1/2
what's wrong? do you need more code? I'm using this to retrieve results:
A paginate:
public $paginate = array('People'=>array(
'limit' => 16,
'order' => 'People.month, People.day ASC'
));
In your People model
public $virtualFields = array(
'month_day' => 'CONCAT(People.month, "-", People.day)'
// 'month_day' => 'CONCAT(People.day, "/", People.month)'
);
then add in your controller
$options = array(
'conditions' => array(
'Post.month_day BETWEEN ? AND ?' => array('01-03','01-06')
// 'Post.month_day BETWEEN ? AND ?' => array('03/01','06/01')
)
);
$posts = $this->Post->find('all',$options);
It would be best to alter your table. Use DATE field and your SQL query would be:
SELECT *
FROM dbname.People
WHERE People.date BETWEEN '1999-03-01' AND '1999-06-01';