sort a JSON array with PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to register for youtube API statistics as JSON data to its proper database. I figured out listing whole data so far,
but I'm not able to list and sort the individual data, which stands for "dislikeCount", descending from more to less.
The JSON data I mentioned in "$ Row ['page_content']" section is below. and I want the "dislikeCount" data to be sorted from large to small.
Any help will be appreciated.
Database page_content
{
"kind":"youtube#video",
"etag":"LVLEiwEdUU043rY6JqiUrm7FmYY",
"id":"IMiBrszc40s",
"statistics":{
"viewCount":"6416534",
"likeCount":"26553",
"dislikeCount":"1862",
"favoriteCount":"0",
"commentCount":"987"
}
}
index.php
$query = $db->from('pages')->orderBy('page_id', 'ASC')->all();
if ($query) {
foreach ($query as $row) {
$json = json_decode($row['page_content'], true);
$videoView = $json['statistics']['viewCount'];
$videoLike = $json['statistics']['likeCount'];
$videoComment = $json['statistics']['commentCount'];
echo $videoView;
echo $videoLike;
echo $videoComment;
}
}

From what I understand from your question, you have a database table pages. This table has a JSON column, page_content, which contains the JSON as you've specified. Now you want to sort the pages based on the pages.page_content.statistics.dislikeCount?
If you're using MySQL or PostgreSQL, then you can order the resultset in the SQL query directly. For example, for MySQL (untested, as I don't have mysql running locally right now):
SELECT * FROM pages
ORDER BY page_id, page_content->"$.statistics.dislikeCount"
If SQL is not an option, you can use PHP's usort.

Related

How to get only a text corresponding to the id from MySQL and store it as a variable in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm a begginer to PHP and I want to know how can I fetch some text from the corresponding ID and store it as a variable in PHP.
The table is like
ID----NAME----ACCOUNT----PASSWORD
1----name1----accont1----password2
2----name2----accont2----password2
3----name3----accont3----password3
Now if I want to get the account2 as text and save it in an variable (say acc2) then what should I do. Assuming that I have connection information in connect.php.
Edit: I want to select the account2 using the ID like from ID 2 select account.
Thanks In Advance!!!
Assuming you use MySQL, the table is named users and you are using PDO, this would get what you need:
$stmt = $conn->query("SELECT * FROM users WHERE ID = 2");
$row = $stmt->fetch()
$account = $row['ACCOUNT']

Can I use an if on foreach in such a case? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a little problem, am trying to read values from mysql table. I have done research and could not find a definite or conclusive answer on this. I will describe the problem:
select data from mysql table using a where e.g where gender=female.
count the results and return the count - just to help know how many records were found
compare a value in table, e.g 'taken' and 'available',
if 'taken' = 'available' in the first record, go to next record(and compare again), if not perform a specific operation in this case can be update or insert or anything of that sort.
the first three are ok and the only problem is, part 4. Kindly help. This is a php problem. Looking forward for your help.
As was said, if you're merely going to skip the record then you may as well not retrieve them in the first place (and thus incur the overhead for having to extract them into PHP memory, etc):
SELECT * FROM `your_table` WHERE `gender` = 'female' AND `taken` = `available`;
However, if you have a specific reason to do this, you can merely do the following:
foreach ($hotels as $hotel) {
// skip if the item is not available, logic can be changed if necessary
if ($hotel->taken >= $hotel->available) continue;
// do the other work here...
}
I interpreted your conditions a little here, assuming you wanted to skip people who aren't 'available'. Though it does look like you wanted the opposite, in which case you can switch the logic in the sql from != to = and in php from != to ==.
Updated: To reflect additional comments made.
Create connection to your database using mysqli or by using PDO.
$db = new PDO($mysqlhost,$username,$password);
$statement = $db->prepare($sqlstatement);
$rows = $statement->execute();
foreach($rows->fetch() as $row)
{
if($row['column_name']==something)
{
//do work
}
else
{
//do work
}
}

How to make this MySQL query execute faster? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
SELECT * FROM (`collection_series`)
JOIN `datadatexnumericy` ON
`collection_series`.`series_id` = `datadatexnumericy`.`series_id`
JOIN `saved_users_chart` ON `collection_series`.`collection_id` =
`saved_users_chart`.`chart_id`
WHERE `saved_users_chart`.`chart_id` = '265'
AND `saved_users_chart`.`id` = '6' AND `datadatexnumericy`.`x` >=
'1973-09-30' AND `datadatexnumericy`.`x` <= '2014-06-30' AND
`datadatexnumericy`.`series_id` != '43538'
AND `datadatexnumericy`.`series_id` != '43541'
GROUP BY YEAR(datadatexnumericy.x)
This is my SQL query and result of this query I am getting from AJAX response this query is working fine but I am getting response slow a bit I think the problem is in my SQL query.
I want all matching records from collection_series and datadatexnumericy table and only matching row from saved_users_chart is there any possible way to optimize this query in more efficient way so that I can get ajax response faster.
In my opinion your query is already optimized. You should test the result of the query in an SQL server to see response time of SQL server and then compare with the time you actualy receive the response back from server.
If is not necesary to extract all the columns from each table, then manualy enter the column names instead of *. By doing this the response received from server will be certainly smaller, therefore it will be faster.

Making posts,getting data from mysql table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how i can make example: post.php and when somebody goes to post.php to show all posts and when somebody click on one post to show in URL post.php?id=1(id=1 by id in database? and when it types post.php?id=2 to go to id 2 in database and show all datas from row of table by id 2)
Use the $_GET method to pass the url. At the top of the php file you can access the information posted in the url using the global $_GET['id']. You can check if it is set, and depending on whether or not it is, show information regarding that id from the database.
It might look something like this:
if (isset($_GET["id"])) {
$id = $_GET["id"];
$query = "SELECT * table WHERE id = {$id} LIMIT 1;";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)){
echo $row["id"];
echo $row["name"];
echo $row["someOtherAttribute"];
}
}
Make sure you have your connection and your database set up and whatnot, but thats how you would accomplish this.

Can I create mysql view columns from an array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The array is json_encoded and stored in another table. Now I want to create a mysql View with MERGE type, and json_decode the array into the View's columns. Is this possible? If yes, How?
If you plan to do that stuff NOT in the database and just use PHP it will be easy. You just have to select the encoded array, decode it, parse it to columns (or any valid SQL), store it in an string and perform this SQL string, than you have what you want.
$sql_array = 'select json_array from tbl';
//use mysql-query/fetch/execute whatever to get your data
//use json_encode to get your PHP $columns = array()
$columns = json_encode(...);
$sql_cols = 'select null';
//iterate through your PHP array()
foreach($columns as $column) {
//make the row-value to columns
$sql_cols .= ', '.$column;
}
$sql_cols .= ' from tbl';
//use mysql-query/fetch/execute whatever to get your data
This should do it.

Categories