I have a MySQL table like this:
+------+-----------------+---------+------------+
| id | name | refferal| reference |
+------+-----------------+---------+------------+
| 1 | Alex Muller | 1 | null |
| 2 | John Doe | 2 | 1 |
| 3 | Tom Foe | 3 | 1 |
| 4 | Harry Pott | 4 | 3 |
| 5 | Kate Garry | 5 | 3 |
| 6 | Mike Blue | 6 | 4 |
+------+-----------------+---------+------------+
(more data than this...)
I need to turn that data to JSON file with Laravel. Like this:
[{"id":1,"name":"Alex Muller","parent":0},
{"id":2,"name":"John Doe","parent":1},
{"id":3,"name":"Tom Foe","parent":1},
{"id":4,"name":"Harry Pott","parent":3},
{"id":5,"name":"Kate Garry","parent":3},
{"id":6,"name":"Mike Blue","parent":4}]
At the and of this I will get a tree view like this:
TREE
I just made this json file with my own write. And I don't know what to do. I'm waiting your answers. Thank you.
If you want to add extra fields like "parent" in your example you can use map on the collection:
$users = User::where(function($query){
...
})->get()->map(function($user){
return array(
"id" => $user->id,
"name" => $user->name,
"parent" => *INTEGER*
);
})->toJson();
Or if you just want to encode the model attributes, you can use toJson serialization directly with the collection:
$users = User::User::where(function($query){
...
})->get()->toJson();
for further information, refer to the links:
https://laravel.com/docs/collections
https://laravel.com/docs/eloquent-serialization#serializing-to-json
On the model get the keys you need and then with map() change the key reference to parent, on that check if is null the reference to put a 0 then encode the array for json with json_encode.
$array = Model::get(['id', 'name', 'reference'])
->map(function($model){
return [
'id' => $model->id,
'name' => $model->name,
'parent' => is_null($reference->reference)? 0 : $reference->reference,
];
})
->toArray();
Then just make a json with that array:
echo json_encode($array);
Related
beginner here. I'm currently making a matching system where owners will register their entries. Once it is done, I will click the "match" button and it'll generate the match. The logic for matching depends on the weight.
For example, if owner 1 and owner 2 registered an entry/ies that has 1900 weight, they'll
automatically be matched. (As you can see at the 2nd table)
How can I achieve these goals of mine? Thank you guys in advance.
tbl_entry
|id| entryName| lightBand| weight |
|---| --------| ----- |---------|
| 1 | owner1 | 1 | 1900 |
| 2 | owner1 | 2 | 1920 |
| 3 | owner2 | 3 | 1900 |
| 4 | owner3 | 4 | 1910 |
| 5 | owner4 | 5 | 1910 |
tbl_matching
| id |fightID| entryName| lightBand| weight | entryName| lightband1| weight1|
|---- |--------| --------| ----- |---------|-------- |----------|--------|
| 1 | 1 | owner1 | 1 | 1900 | owner2 | 3 | 1900 |
| 2 | 2 | owner3 | 4 | 1910 | owner4 | 5 | 1910 |
| 3 | - | owner2 | - | - | - | - | - |
HTML:
<form action="transaction/match" method="POST">
<input type="submit" name="doMatch"> match
</form>
Controller:
public function match {
$formSubmit = $this->input->post('doMatch');
// do the matching query here
}
(hyphen/dash) <<<< means no correspondent player to match with.
Note: I'm using codeigniter3.
First of all, I never use <input type = "submit"> before, usually I use <input type="text"> and put the submit in button.
Let's say you have
<input type="text" name="entryName">
<input type="text" name="weight">
... etc
you can put this in your Controller (for later, you should use Control -> Model)
$weight = $this->input->post('weight'); //get your weight input
//get the opponent data based on weight input
$result = $this->db->get_where('tbl_entry', ['weight' => $weight])->row_array();
//check if the result is not null
if ($result != null) {
$submit = [
//I assume that your ID is autoincrement
'fightID' => 'fight ID', //you can make a function for fight ID and put the result like $fight_ID
'entryName' => $this->input->post('entryName'),
'lightBand' => $this->input->post('lightBand'),
'weight' => $weight,
];
$data[] = array_merge($submit, $result); //merge the entry and the result into 1 array
$this->db->insert('tbl_matching', $data); //insert it into your table matching
}
else {
//like above, but just change it to '-' instead of input->post
}
I am using Firebase for my project. Firebase database looks like that:
{
myObjects:{
1:{
index: '1',
body: 'foo1'
},
2:{
index: '1',
body: 'foo2'
},
3:{
index: '2',
body: 'foo3'
},
},
objectIndex: 1
}
As above, I have myObjects object and objectIndex variable. I was retrieving myObjects which index is same as objectIndex variable. objectIndex variable increments every 3 days and when it reaches 50 it turns into 0. So it is dynamic and I couldn't store it on the table.
Now I want to convert my Firebase database to MySQL.
MySQL will look like this:
|----|------|-------|
| id | body | index |
|----|------|-------|
| 1 | foo1 | 1 |
|----|------|-------|
| 2 | foo2 | 1 |
|----|------|-------|
| 3 | foo3 | 2 |
|----|------|-------|
Where I can store objectIndex variable?
I can update my table structure according to your suggestions.
Thanks in advance.
You can have a 1-row and 1-column "objectIndex" table where the value can be updated by using an SQL Cron-Job.
You can then build a query with a cartesian product that returns your data as follows:
|----|------|-------|-------------|
| id | body | index | objectIndex |
|----|------|-------|-------------|
| 1 | foo1 | 1 | 25 |
|----|------|-------|-------------|
| 2 | foo2 | 1 | 25 |
|----|------|-------|-------------|
| 3 | foo3 | 2 | 25 |
|----|------|-------|-------------|
It is redundant but gets the job done. The code to retrieve these values can be written as follows:
SELECT id, body, index, objectIndex
FROM objectTable, objectIndexTable
I have a JSON data feed coming into SQL Server 2016. One of the attributes I must parse contains a JSON array. Unfortunately, instead of implementing a key/value design, the source system sends each member of the array with a different attribute name. The attribute names are not known in advance, and are subject to change/volatility.
declare #json nvarchar(max) =
'{
"objects": [
{"foo":"fooValue"},
{"bar":"barValue"},
{"baz":"bazValue"}
]
}';
select * from openjson(json_query(#json, 'strict $.objects'));
As you can see:
element 0 has a "foo" attribute
element 1 has a "bar" attribute
element 2 has a "baz" attribute:
+-----+--------------------+------+
| key | value | type |
+-----+--------------------+------+
| 0 | {"foo":"fooValue"} | 5 |
| 1 | {"bar":"barValue"} | 5 |
| 2 | {"baz":"bazValue"} | 5 |
+-----+--------------------+------+
Ideally, I would like to parse and project the data like so:
+-----+---------------+----------------+------+
| key | attributeName | attributeValue | type |
+-----+---------------+----------------+------+
| 0 | foo | fooValue | 5 |
| 1 | bar | barValue | 5 |
| 2 | baz | bazValue | 5 |
+-----+---------------+----------------+------+
Reminder: The attribute names are not known in advance, and are subject to change/volatility.
select o.[key], v.* --v.[key] as attributeName, v.value as attributeValue
from openjson(json_query(#json, 'strict $.objects')) as o
cross apply openjson(o.[value]) as v;
I'm having trouble describing what I want so I'll try to illustrate it the best I can with arrays.
Array
(
[user1] => Array(
[title] => customtitle1
[prefix] => false
[worlds] => Array(
[119] => 367
[2] => 5
)
[time] => Array (
100
101
102
204
)
[last] => 119
)
[user2] => Array(
[title] => customtitle2
[prefix] => true
[worlds] => Array(
[119] => 367
[2] => 5
)
[time] => Array (
100
101
102
204
)
[last] => 119
)
)
I stored this in txt files but I moved on to SQL databases. How would I store this?
I only display 2 users here but it is more, the "worlds" array gets new values overtime (including new keys, so the length will change). Same goes for the "time" array, but only values.
username | title | prefix |
user1 | bla | true |
user2 | bl2 | false |
I don't know how I would go on implementing the worlds & time arrays. I would like to be able to sort these too.
Keep entities separate is one goal of good data modeling. Use one table to store one type of information, and another table to store another type, and relate them using foreign keys or join tables. Per your example, you might want a structure like this.
users:
| id | username | title | prefix | last_world |
| 1 | user1 | bla | true | 119 |
| 2 | user2 | bl2 | false | 119 |
worlds:
| user_id | worlds_id | other_id |
| 1 | 119 | 367 |
| 1 | 2 | 5 |
| 2 | 119 | 367 |
| 2 | 2 | 5 |
time:
| user_id | time |
| 1 | 100 |
| 1 | 101 |
| 1 | 102 |
| 1 | 204 |
| 2 | 100 |
| 2 | 101 |
| 2 | 102 |
| 2 | 204 |
These tables can be joined with a query like this:
SELECT
u.*,
w.worlds_id,
w.other_id,
t.time
FROM users u
INNER JOIN worlds w
ON u.id = w.user_id
INNER JOIN time t
ON u.id = t.user_id
Constructing your database schema so that data is never redundant (e.g. you update a username one and only one place) and data is never incorrect in one place but correct in another is called database normalization.
Good luck!
I have successfully made a searchable drop down list,in which whenever I type something, data from database appears in drop down list, now I want to store these multiple values which are selected, in the database.
My code in controller
$temp = $model->package_item = $_POST['package_item'];
foreach($temp as $t)
{
$model->package_item=$t;
}
Package_item is the field in which searchable dropdown list is applied, and I am selecting multiple values, now how can I save these values in the database, so that I will have a single id, but against this id, package_item will have multiple values and the rest of the attributes will be repeated. Just like below.
id------package_item------package_description------package_name
1--------cake------------ very cheap-------------get one buy one free
1---------candles---------- very cheap-------------get one buy one free
1----------fireworks--------very cheap-------------get one buy one free
I am using select2 extension and here is the code for that in view file
$this->widget('ext.select2.ESelect2', array(
'name' => 'package_item',
'data' => CHtml::listData(Package::model()->findAll(), 'id', 'package_item'), //the whole available list
'htmlOptions' => array(
'placeholder' => ' search packge item?',
//'options' => $options, //the selected values
'multiple' => 'multiple',
'style'=>'width:530px',
),
));
Thanks in advance
My first answer was terribly misleading, so I deleted it and here and giving it another try.
You need to plan out your database tables. I'm making a suggestion here:
Table Items
itemId[PK]| name | price |...|
1 | cake | 5.00
2 | candles | 2.00
3 | fireworks | 10.00
4 | expensiveStuff | 50.00
Table Packages
packageId[PK] | name | description | price |
1 | full package | super cheap | 12.00
2 | epic package | best value for money | 55.00
Table PackagesItems
packageId[PK][FK]|itemId[PK][FK]|amount
1 | 1 | 1
1 | 2 | 1
1 | 3 | 1
2 | 1 | 2
2 | 2 | 2
2 | 3 | 3
2 | 4 | 1
You would then query your packages like this:
SELECT itemId FROM PackagesItems WHERE packageId = 1 for example
The keyword of this answer is COMPOSITE PRIMARY KEY, which you can see in the third table: there are two primary keys, which are also foreign keys that lead to the other tables.
With SQL JOINS, you could retrieve the information from the other two tables.
If you need more information, please narrow down the scope of your question. What exactly do you want to know?