Document exact matching - php

Let's say we have a following MongoDB-collection:
{id:1, data:"some_data"}
{id:2, data:"some_data"}
{id:3, data:"some_data"}
{id:4, data:"some_data"}
I also have an php-array with element "6" that is not in the collection
$q = [1,3,6];
If I query the collection like this:
$cursor = $db->col->find(['id' => ['$in' => $q]]);
I'm getting documents with id's 1 and 3
But I'd like to have an empty result because the id 6 do not exist in the collection,
how can I exact match the collection?
Can I do this with just one query?

If id is unique:
var result = db.col.find({id:{$in:q}});
if(result.count() == q.length) {
//handle result
} else {
//handle empty result
}

You can not do this just with one mongodb query, because it does not have such functionality.
Also I don't understand why exactly do you need this, you can achieve it doing something like this:
$q = [1,3,6];
$res = array();
$cursor = $db->col->find(['id' => ['$in' => $q]]);
foreach($cursor as $val){
$res[$val['id']] = $val['data'];
}
foreach($q as $val){
if (!isset($res[$val])) $res[$val] = ''; // to show that there is nothing in the results.
}
P.S. I have not tried this code, so there might be errors.

Related

Laravel 5.8 get tables belonging to a user

I am listing all tables in the DBthat are prefixed with the user->id + _ (eg: 2_my_table) like so:
$account = Auth()->user();
$tables = DB::select("SHOW TABLES LIKE '" . $account->id . "_%'");
This works and returns an array like this:
array:1 [▼
0 => {#577 ▼
+"Tables_in_mydb (2_%)": "2_country_list"
}
]
Why does it add the (2_%) search pattern to the Tables_in_mydb property?
This causes issues later when try and describe the tables like this:
if (count($tables)) {
foreach ($tables as $table) {
$table->columns = DB::select('describe '.$table->Tables_in_mydb);
$table->rows = DB::select('SELECT COUNT(*) AS count FROM '.$table->Tables_in_mydb);
}
}
Fixed like this. But still do not understand the original issue.
$tables = collect(DB::select('show tables'))->filter(function ($val) use (&$account) {
foreach ($val as $key => $tbl) {
$tbl_prefix = $account->id . "_";
$tbl_prefix_length = strlen($tbl_prefix);
if (substr($tbl, 0, $tbl_prefix_length) == $tbl_prefix) {
return $tbl;
}
}
});
You might have to go about it the long way.
By using get_object_vars, you can get the object values into an array. Then use array_values to get just the values:
$table_name_arr = array_values(get_object_vars($table));
This will provide with an array (0 => '2_country_list'), which you can easily get with $table_name_arr[0]
For a one-liner, use
$table_name = array_values(get_object_vars($table))[0];

Multi-dimensional array to JSON

I am trying to print my data and encode it into JSON. I'm extracting the data from my MySQL database for a quiz. The data that I am trying to extract 1 question, 1 category and 4 options to make a set of quiz. I think I nearly got it how to make it work but I failed to find out how. This was the result check in this link.
Paste the JSON to this link so that you can easily format it. From that JSON data, I want to output like this for each question:
"What is the approximate number of islands that comprise the Philippines?",
"Philippine Geography",
[
{
"quiz_choice_id":"5",
"choice":"7000",
"is_correct_choice":"1"
},
{
"quiz_choice_id":"6",
"choice":"6000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"7",
"choice":"8000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"8",
"choice":"9000",
"is_correct_choice":"0"
}
],
This is my code for that:
<?php
/**
* Created by PhpStorm.
* User: Muhammad
* Date: 19/04/2016
* Time: 00:46
*/
require_once('Database_Connect.php');
$questions_query = "SELECT question, quiz_choice_id, choice ,is_correct_choice, category FROM category_question cq, question q, question_choices qc WHERE q.quiz_question_id = qc.quiz_question_id AND q.cat_ques_id = cq.cat_ques_id LIMIT 10";
$questions_query_result = mysqli_query($con, $questions_query) or die("error loading questions");
$questions_results = array();
encode_result($questions_query_result);
function encode_result($result)
{
//$response = array();
$questions = array();
//$choices = array();
while ($r = mysqli_fetch_array($result)) {
//$response[] = $r;
//$questions = array('question' => $r[0]);
$questions[] = $r['question'];
$questions[] = $r[4];
$choices[] = array('quiz_choice_id' => $r[1], 'choice' => $r[2], 'is_correct_choice' => $r[3]);
$questions[] = $choices;
//array_push($questions, $questions['question']);
//array_push($questions_results, $questions);
}
echo json_encode(array('questions'=>$questions));
}
mysqli_close($con);
The design of the database is this:
I can't find a way to make it work because from the database quiz_choice_id,choice, is_correct_choice are in a different table but I combined all into one table as you can see in my SQL statement $questions_query. Please let me know how can I fix this. Thanks in advance.
Do you want the json to look like this?
[ {
"question" : "What is the approximate number of islands that comprise the Philippines?",
"category" : "Philippine Geography",
"choices" : [
{
"quiz_choice_id":"5",
"choice":"7000",
"is_correct_choice":"1"
},
{
"quiz_choice_id":"6",
"choice":"6000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"7",
"choice":"8000",
"is_correct_choice":"0"
},
{
"quiz_choice_id":"8",
"choice":"9000",
"is_correct_choice":"0"
}
]
},{
..... // next question
}]
You'll have to do something like this, but I have no idea what the results of the query is.
$questions = array();
while ($r = mysqli_fetch_array($result)) {
$key = $r['quiz_question_id']; // you need a key here that is unique to the question, so i think this will do looking at the schema you posted, this will group them in that segment of the array.
if( !isset( $questions[$key] ) ){
//if we never had this question, create the top level in the results
$questions[$key] = array(
'question' => $r['question'],
'category' => $r['category'],
'choices' => array()
);
}
//add in the choices for this question
//on the next iteration, the key is the same so we only do this part
//and append that rows choices to the previous data using $key to match the question
$questions[$key]['choices'][] = array('quiz_choice_id' => $r['quiz_choice_id'], 'choice' => $r['choice'], 'is_correct_choice' => $r['is_correct_choice']);
}
Make sure to add quiz_question_id to your query if this is the questions id, unique identifier. Essentially this will group them together, as this will be the same for each row with that question's choices.
I added string keys to the output, I wouldn't mix using string keys and numbered index. I hope I mapped them out right.
Also I haven't tested this at all, so sorry if there are any syntax errors.

using array_count_values in 2 dimensional array

So I have array with patch number and seasons next to it, and I'm trying to count how many patches are in a season so I want to count how many $season[x][1] == 1 or 2 etc.
$patches_get = $conn->prepare("SELECT Patch_No FROM info ORDER BY Created DESC");
$patches_get->execute();
$patchesresult = $patches_get->get_result();
while($data1 = $patchesresult->fetch_assoc()){
$patches[]=$data1["Patch_No"];
}
function getseasons($patches){
$seasons = array();
foreach($patches as $patch){
if(substr($patch,0,1)!=1){
$seasons[] = array($patch, substr($patch,0,1));
}
//Checking first number if 1 it is season 1 or 2 or 3
elseif(substr($patch,0,1)==1){
if(substr($patch, 6,3)>151&&substr($patch, 6,3)<155){
$seasons[] = array($patch, 3);
}
elseif(substr($patch, 6,3)>125&&substr($patch, 6,3)<151){
$seasons[] = array($patch, 2);
}
elseif(substr($patch, 6,3)>32&&substr($patch, 6,3)<126){
$seasons[] = array($patch, 1);
}
}
}
return $seasons;
}
$seasons = getseasons($patches);
var_dump($seasons);
$fr_c=array_count_values($seasons);
echo $fr_c['1'];
Here is also a var_dump of how my array would look like http://i.imgur.com/lV1APvV.png
If I"m reading your question right, you essentially want to count the inner array without iterating through the outer? If that's the case, look into array_column()
For example, let's say I have the following array
$myArr = [
['item' => 'value1'],
['item' => 'value2']
]
Calling array column like so
array_column($myArr, 'item'); // returns an array of just the values that you can iterate through.
See here
If this isn't the answer you're looking for, please disregard.

query inside while loop only shows 1 result

i'm making a while loop in php and it all goes well but the problem is that
I don't only want to get the id of the user but also some other stuff that is inside another table, so when I go ahead and make a query inside this while loop and select everything from that second table (where the id is equal to the id of the result from the first query), it only returns 1 result...
So this is the code that I currently have:
public function getFriends($id)
{
global $params;
$get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
array(
"{$this->DB['data']['friends']['one']}" => $id
)
);
if($get)
{
while($key = $get->fetch())
{
$query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
WHERE {$this->DB['data']['users']['id']} = :id",
array(
"id" => $key->{$this->DB['data']['friends']['two']}
)
);
while($row = $query->fetch())
{
$params["user_friends"][] = [
"id" => $key->{$this->DB['data']['friends']['two']},
"name" => $row->{$this->DB['data']['users']['username']},
"look" => $row->{$this->DB['data']['users']['figure']}
];
}
}
}
else
{
$params["update_error"] = $params["lang_no_friends"];
}
}
Thanks in advance!
Please help me out!
In the absence of answers, I don't know what db framework you are using behind the scenese...PDO, mysqli_, or (hopefully not) mysql_. But, in any case, the problem might be that your second query stops the first from continuing. I would use PDO->fetchAll() to get them all...but you say you can't do that...so, looping the first and loading those results into an array is the first thing I would do to see if this is the problem:
public function getFriends($id)
{
global $params;
$get = $this->db->select("{$this->DB['data']['friends']['tbl']}", "*",
array(
"{$this->DB['data']['friends']['one']}" => $id
)
);
$firstResults = array();
if( $get ) {
while( $key = $get->fetch() ) {
$firstResults[] = $key;
}
}
else
{
$params["update_error"] = $params["lang_no_friends"];
}
foreach( $firstResults AS $key )
{
$query = $this->db->query("SELECT * FROM {$this->DB['data']['users']['tbl']}
WHERE {$this->DB['data']['users']['id']} = :id",
array(
"id" => $key->{$this->DB['data']['friends']['two']}
)
);
while($row = $query->fetch())
{
$params["user_friends"][] = [
"id" => $key->{$this->DB['data']['friends']['two']},
"name" => $row->{$this->DB['data']['users']['username']},
"look" => $row->{$this->DB['data']['users']['figure']}
];
}
}
}
If this doesn't work, then we need more data...e.g. what is the query generated? When you run it manually does it return more than one result? If you get rid of the inner-query, does this fix it? etc.
The first step when diagnosing PHP and Mysql issues is to add lines to your code that tell you what each line is doing (declare each time a loop is entered; when each mysql query is run, spit out the query string) so you can narrow down where the problem is. Often this makes you feel stupid in retrospect: "Duh, this query didn't return anything because I formatted the record ID wrong" and so forth.
The code snippet you've provided above isn't super helpful to me. I'm a troubleshooter (not a parser) so I need diagnostic data (not straight code) to be of any more help than this.

Create multidimensional array from database query

I have a database that stores accounts and subscriptions. An account can contain accounts and subscriptions and have more than three parent levels.
A row from the database query result looks something like this:
$array = (0 => array("level" => "2", "accountno" => "123124234", "accountname" => "Hansel", "parentaccountno" => "000213123", "subscription" => "5423213213", "username" => "Gretchen");
(Level 1 means that it is top level and it will have PARENTACCOUNTNO = null).
From this I am trying to create a multidimensional array which looks something like this:
accounts:
000213123
accounts:
123124234
name: Hansel
subscriptions:
5423213213
username: Gretchen
This is the code I have so far, which works really well on the first two levels. When there are are three levels or more things become more complicated, since each account only knows about its own parent:
$hierarchy = array();
foreach ($decode as $row) {
$accountno = $row["ACCOUNTNO"];
$msisdn = $row["MSISDN"];
if ($row["PARENTACCOUNTNO"] == null)
$base_array_key = $hierarchy["accounts"];
$base_array_key[$accountno] = array("name" => $row["ACCOUNTNAME"], "accounts" => array(), "subscriptions" => array());
$hierarchy["accounts"][$accountno]["accounts"]["321323123213"] = "blaha";
if ($row["MSISDN"] != null)
$hierarchy["accounts"][$accountno]["subscriptions"][$msisdn] = array("enduser" => $row["ENDUSER"]);
}
The solution I have in mind right now is to pass the base key from each iteration to the next iteration, and then if for example the previous level was two levels above this one just use some sort of string replace to remove the last two parts of the key.
Any better ideas? I am quite sure this is a shitty way of doing what I am trying to accomplish.
Do all your results look like that ? So then, where's are the parent accounts ? Or am i missing something ? I would simple rewrite the query to get a more "linear" result, like:
parentaccountno | accountno | accountname | bla
-----------------------------------------------
000213123 | 123124234 | Hansel | abc
When every result row looks like this, you can simple create your final array like that:
foreach ($decode as $row) {
$my_array = [ $row["parentaccountno"] ][ $row["accountno"] ]["accountname"] = $row["accountname"];
$my_array = [ $row["parentaccountno"] ][ $row["accountno"] ]["bla"] = $row["bla"];
}
Create a temporary associative array:
$all_records = array();
Populate it with the results you find in the database:
while($result = get_results_from_db()) {
$accountno = $result[accountno];
$all_records[$accountno] = $result;
}
Now you have an array indexed by account numbers so it's easy to find any record if you know it's account number. So you iterate through the array to associate children to parents:
$top_level_records = array();
foreach($all_records AS $record) {
$parentaccountno = $record[parentaccountno];
if(!empty($parentaccountno)) {
$parent = &$all_records[$record[parentaccountno]];
if(empty($parent['accounts']))
$parent['accounts'] = array()
$parent['accounts'][] = $record;
}
else {
$top_level_records[] = $record;
}
}
I appreciate the answers, but I think the problem is my database query which should contain records of all the parents of an account and not just the parent, using something like SYS_CONNECT_BY_PATH in Oracle SQL.

Categories