I have tables for example called 'city':
+-----+---------------------+
| id | name |
+-----+---------------------+
| 2 | Amsterdam |
| 3 | The Hague |
| 8 | Barcelona |
| 10 | Rome |
| 11 | Paris |
| 12 | Rotterdam |
I need to select columns information in this format:
Array
(
[0] => Array
(
[Amsterdam] => 2
[The_Hague] => 3
...
)
)
I do not want to select the information like, and mapping that information to needed format:
Array
(
[0] => Array
(
[title] => Amsterdam
[value] => 2
)
[1] => Array
(
[title] => The_Hague
[value] => 3
)
)
Can I do it by using concat, json or something else?
it looks like you are using PHP. If so, you can do this:
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
see https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
Related
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);
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!
Suppose I have table A and B.
Table A contain column id, name
id | name
----|-----------
1 | X
2 | Y
3 | Z
Table B contain column id, tax_type, rate
id | tax_type | rate
----|-----------|--------
1 | services|12
2 | vat |3
3 | others |4
I have created grid view using table B, so the column of grid view are id and name.
But I want column dynamically in grid view by fetching table B values.
Like:
id | name |services | vat | others
----|-------|-----------|-------|--------
1 | X | 12 | 3 | 4
2 | Y | 12 | 3 | 4
3 | Z | 12 | 3 | 4
If the row change in table B then columns are also change in grid view.
Your Gridview columns can have any value. Do the following in your view:
First, get your data from table B:
$taxInfo = TableB::find()->indexBy('tax_type')->all();
Then, in your view in column definitions just add this:
'columns' => [
'id',
'name',
//...
[
'label' => 'Services',
'value' => function ($model) use $taxInfo {
$taxInfoObject = $taxInfo['services'];
return $taxInfoObject->rate;
}
],
[
'label' => 'VAT',
'value' => function ($model) use $taxInfo {
$taxInfoObject = $taxInfo['vat'];
return $taxInfoObject->rate;
}
],
[
'label' => 'Others',
'value' => function ($model) use $taxInfo {
$taxInfoObject = $taxInfo['others'];
return $taxInfoObject->rate;
}
],
]
Note how the $taxInfo variable we defined above is passed to our anonymous functions. (Generally those functions work with $model which, in your case, would contain a specific row of table A).
|touser| fromuser | msg |
| A | B | hi |
| A | B | hello |
| C | D | bye |
| C | E | hey |
when i use following query in mysql workbench it shows the desired result that is all the rows with given name:
select * from db.table1 where touser in ('A');
output:
|touser| fromuser | msg |
| A | B | hi |
| A | B | hello |
but when i pass query from php commands the resultant array contains only first record
<?php
//connection establishing commands
$sql="select * from db.table1 where touser in ('A')";
$result=mysqli_query($link, $sql);
$data=mysqli_fetch_array($result,MYSQLI_NUM);
print_r($data);
//other stuff;
?>
output:
Array ( [0] => A [1] => B [2] => Hi )
am I missing something in the php commands?
You're PHP is simply returning the first row of the MySQL result set.
You'll want to replace $data=mysqli_fetch_array($result,MYSQLI_NUM);
with
while ($data = mysqli_fetch_array($result, MYSQLI_NUM)) {
print_r($data);
}
Which will iterate over each row of the result set. In other words, the mysqli_fetch_array function doesn't fetch the entire result set as an array, it simply returns a single row, and then moves the row "pointer" to the next row.
I have a mySQL table with the following structure
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| make | varchar(55) | NO | MUL | NULL | |
| model | varchar(55) | NO | MUL | NULL | |
| year | varchar(4) | NO | MUL | NULL | |
+-------------+------------------+------+-----+---------+----------------+
and some sample data
('Chevrolet', 'Express', '2001'),
('Chevrolet', 'Express', '2002'),
('Chevrolet', 'Venture', '2001'),
('Chevrolet', 'Venture', '2002'),
('Dodge', 'Dakota', '2000'),
('Dodge', 'Dakota', '2001'),
('Ford', 'Flex', '2009'),
('Ford', 'Flex', '2010'),
What I want is one fast query that can return me all the models, with all the makes and years attached. So the following is an example of something I would like returned.
Array
(
[Chevrolet] => Array
(
[Express] => Array
(
[0] => 2001
[1] => 2002
)
[Venture] => Array
(
[0] => 2001
[1] => 2002
)
)
[Dodge] => Array
(
[Dakota] => Array
(
[0] => 2001
[1] => 2002
)
)
[Ford] => Array
(
[Flex] => Array
(
[0] => 2009
[1] => 2010
)
)
)
What I am currently doing:
I have one query to return all the unique makes:
$sql = "select distinct make from listings order by make asc ";
and once I have every make, I loop through and run the following query, using the make:
$sql = "select distinct model from listings where make = ? order by model asc ";
and I go one more step to grab the years:
$sql = "select distinct year from listings where make=? and model=? order by year desc";
SELECT make, model, year
FROM listings
ORDER BY make, model, year