I've found this piece of code which looks to work for some people,
$dataArr['aaData'] = Array();
while($row = $res->fetch_assoc()){
$r = Array();
foreach($row as $key=>$value){
$r[] = "$key $value";
}
$dataArr['aaData'][] = $r;}
header('Content-Type: application/json');
echo json_encode($dataArr);
/* The output will be of the form,
{"aaData": [ [
[
"colname data"
...
],
] */
but I get an error " Table id =datos Invalid json response.
My Datatable columns do not have the same name as my db columns, and I'm not sure how to handle that.
Here is my js :
$(document).ready(function() {
function getCpAndVille(data, type, dataToSet) {
return data.cp + " " + data.ville;
}
$('#datos').dataTable({
"order": [[ 3, "desc" ]],
"bProcessing": true,
"sAjaxSource": 'ajx/datatable_process_search.php',
"aoColumnDefs": [
{ "sName": "Réf.", "aTargets": [ 0 ] },
{ "sName": "Poste", "aTargets": [ 1 ] },
{ "sName": "Type de contrat", "aTargets": [ 2 ] },
{ "sName": "Date de publication", "aTargets": [ 3 ]},
{ "sName": "Lieu", "aTargets": [ 4 ], "mData": getCpAndVille },
{ "sName": "Descriptif", "aTargets": [ 5 ] }
],
Can someone tell me the right php code I should use, please, because it's quite hard to find it, and I don't understand the example given in http://www.datatables.net/examples/data_sources/server_side.html
Thanks in advance.
Console Network --> {"aaData":[["job_id 9261","job_intitule Assistant Achats","job_contrat Int\u00e9rim","job_date_insertion 2015-02-20","cp 06110","job_ville 19"], and so on...
For those who are looking for a serverside script including JOIN table
https://www.daniweb.com/web-development/php/threads/467455/convert-datatables-to-server-side-processing
Related
I would like to create a query with mongodb which would do this thing:
SELECT * FROM Users WHERE Username NOT IN (SELECT LikeTo FROM Like WHERE Username = "John89").
i did something like this:
[
'$lookup'=> [
'from'=> "like",
'let'=> [ 'username'=> '$username' ],
'as'=> "leaders",
'pipeline'=> [
[
'$match'=> [
'$and'=> [
['$expr'=> [ '$not'=> ['$in'=> ['$likeTo','$$username']]]],
['username'=> "$username" ]
]
]
]
]
]
]
but i got PlanExecutor error during aggregation :: caused by :: $in requires an array as a second argument, found: string.
what did i do wrong? is the query right?
thanks!
I would personally split this into 2 queries, but here is how yo achieve this in a single pipeline, we actually want the lookup to match the "bad" users so we can use it to filter, like so:
db.users.aggregate([
{
"$lookup": {
"from": "likes",
let: {
username: "$username"
},
pipeline: [
{
$match: {
$expr: {
$and: [
{
$eq: [
"$likeTo",
"$$username"
]
},
{
"$eq": [
"$username",
"John89"
]
}
]
}
}
}
],
"as": "leaders"
}
},
{
$match: {
"leaders.0": {
$exists: false
}
}
},
{
$project: {
leaders: 0
}
}
])
Mongo Playground
i want to check login users, follow the search user or not, if follow, true if not,false show in output i need help php cod
MY CODE IS
$sql="SELECT * FROM `users` where user_id='$userid' AND session_id='$session_id'";
$result=mysqli_query($conn,$sql);
if($result->num_rows>0)
{
//user_array
$user_query="SELECT * FROM users";
$user_query_result=mysqli_query($conn,$user_query);
if($user_query_result->num_rows>0)
{
$user_array=array();
while ($row_users = $user_query_result->fetch_assoc())
{
$user_id=$row_users['user_id'];
$user_name=$row_users['fast_name']." ".$row_users['last_name'];
$user_pic_path=$row_users['user_pic_path'];
$follower=array();
$followers=array();
$followed_query="SELECT * FROM followers where follower_id=".$user_id;
$followed_result=mysqli_query($conn,$followed_query);
while($followed_row=$followed_result->fetch_assoc())
{
$follower=$followed_row['user_id'];
if($follower==$user_id)
{
$followers=TRUE;
}
//array_push($followers,$followed_row['user_id']);
}
$images=array();
$images_query="SELECT * FROM image where user_id=".$user_id;
$image_result=mysqli_query($conn,$images_query);
while($image_row=$image_result->fetch_assoc())
{
$images[]=$image_row['image_path'];
}
$user[] = array(
/*"user_id"=>$user_id,*/
"user_name"=>$user_name,
"user_pic_path"=>$user_pic_path,
"follower"=>$followers,
"images"=>$images
);
//add in user_array
array_push($user_array,$user);
}
$response['session']=TRUE;
$response['status']=TRUE;
$response['msg']="user data find successfully";
$response['user_detail']=$user;
echo json_encode($response);
}
MY OUTPUT IS
// 20180104175035
// http://localhost/1111.php?userid=2&session_id=953455543
{
"session": true,
"status": true,
"msg": "user data find successfully",
"user_detail": [
{
"user_name": "asad ali",
"user_pic_path": "localhost/uploads/image/9152108abc",
"follower": [
],
"images": [
"localhost/uploads/image/7947861Discover All In One.png"
]
},
{
"user_name": "asim kabeer",
"user_pic_path": "localhost/uploads/image/1952108xyz",
"follower": [
],
"images": [
"localhost/uploads/image/1787860Discover All In One.png",
"localhost/uploads/image/2152108Mart Zone - All in one.png"
]
},
{
"user_name": "saad hussain",
"user_pic_path": "localhost/uploads/image/5152108ytp",
"follower": [
],
"images": [
"localhost/uploads/image/1474008Mart Zone - All in one.png",
"localhost/uploads/image/6547860Discover All In One.png"
]
},
{
"user_name": "nazeer hussain",
"user_pic_path": "localhost/uploads/image/7352108trp",
"follower": [
],
"images": [
"localhost/uploads/image/1547860Discover All In One.png"
]
},
{
"user_name": "asad zahoor",
"user_pic_path": "localhost/images/ftftft",
"follower": [
],
"images": [
"localhost/uploads/image/1081552Mart Zone - All in one.png"
]
},
{
"user_name": "ali hussain",
"user_pic_path": "localhost/images/sduhsud.png",
"follower": [
],
"images": [
"localhost/uploads/image/5547860Discover All In One.png"
]
}
]
why follower does not show the value i want true or false if follow then show true my follower table schema are following
user_id=>1,2,3,4,5,6
follower_user_id=>2,3,5,3,4,1
I'm almost sure, that below condition is failing
if($follower==$user_id)
{
I'm not sure about the data, so I can't comment much. But, I would suggest to debug your SQL one more time and check, if you are getting expected output from SQL
$followed_query="SELECT * FROM followers where follower_id=".$user_id;
$followed_result=mysqli_query($conn,$followed_query);
while($followed_row=$followed_result->fetch_assoc())
{
$follower=$followed_row['user_id'];
if($follower==$user_id)
Side note : - why can't you simply use follower_id=user_id in SQL ?
$followers=array();
if(1==1)
{
$followers=TRUE;
}
$user[] = array("follower"=>$followers,);
print_r ($user);
Output :
[0] => Array
(
[follower] => 1
)
Whereas, below case return empty array, which is actually your case
if(1==2)
{
$followers=TRUE;
}
$user[] = array("follower"=>$followers,);
print_r ($user);
Output :
[0] => Array
(
)
while ($row_followed = $result_followed->fetch_assoc())
{
//array_push($followers,$row_followed['user_id']);
$age=array($userid=>$row_followed['follower_id']);
foreach($age as $x => $value)
{
if($x==$value)
{
$followers=TRUE;
}
elseif($x!==$value)
{
$followers=FALSE;
}
}
}
I have severals doc and each doc contains nested fields and I want to order by position depending on thematic id
Document1 :
{
...
"thematics":[
{
"id": 1,
"position": 100
},
{
"id": 2,
"position": 1
}
]
}
Document2:
{
...
"thematics":[
{
"id": 2,
"position": 3
}
]
}
Document3:
{
...
"thematics":[
{
"id": 1,
"position": 40
}
]
}
For example, I would like to get only documents with thematics which contains id = 2
So I did something like that
$filter = BoolQuery();
...
$filter->addMust(new Query\Term(["thematics.id" => 2]));
And then when I want to apply sort method on position where thematic id = 2 and not something else.
I tried something like that :
case 'atp': // asc thematic position
$sort = [
"_score",
[
"thematics.position" => [
"order" => "asc",
"missing" => 0,
],
],
];
break;
...
$this->setSort($sort); // call parent method setSort(array()) of elastica-ruflin
Response example :
First case expected :
If I want to display all documents from thematic 1 the order must be :
Document3 then Document1.
Second case expected :
If I want to display all documents from thematic 2 the order must be :
Document1 then Document2.
But for now what I've got is :
- First case : Document3, Document1
- Second case : Document2, Document1
I'm guessing that it takes the first thematic's position of document1 to sort in both cases.
Edit :
I tried to change de mapping with a nested type
thematics:
type: nested
properties:
label: { type: string, index: not_analyzed }
slug: { index: not_analyzed }
name: { type: string, index: not_analyzed }
position: { type: integer }
id: { type: integer }
And the query but still not working
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match_all": {}
}
]
}
}
],
"filter": [
{
"bool": {
"must": [
{
"term": {
"is_searchable": true
}
},
{
"nested": {
"path": "thematics",
"query": {
"term": {
"thematics.id": {
"value": 2
}
}
}
}
},
{
"exists": {
"field": "valuation"
}
},
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"exists": {
"field": "valuation.translations.fr.title"
}
}
]
}
},
{
"bool": {
"must": [
{
"exists": {
"field": "valuation.translations.en.title"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"commercial_subcategory.category.id": 33
}
}
]
}
}
]
}
}
]
}
}
]
}
},
"boost_mode": "multiply",
"functions": [
{
"field_value_factor": {
"field": "booster",
"modifier": "square"
}
}
]
}
},
"sort": [
"_score",
{
"thematics.position": {
"order": "asc",
"missing": 0,
"mode": "min",
"nested_filter": {
"term": {
"thematics.id": {
"value": 2
}
}
}
}
}
]
}
Edit2: I get around the problem.
I changed my mapping so each document looks like that :
{
...
"thematics":[
"1": {
"id": 1,
"position": 100
},
"2": {
"id": 2,
"position": 1
}
]
}
And then I apply a Bool Query must / Exists Filter on "thematics.".$thematicId
And finally, my sort method looks like this :
case 'atp': // asc thematic position
$sort = [
"_score",
[
"thematics." . $thematicId . ".position" => [
"order" => "asc",
"missing" => 0,
],
],
];
break;
...
$this->setSort($sort); // call parent method setSort(array()) of elastica-ruflin
I'm using datatables via ajax and display the table like this
var table = $('#data').DataTable( {
"ajax": "initTable.php",
"columns": [
{ "data": "orderid" },
{ "data": "first_name"},
{ "data": "last_name"},
{ "data": "unix" },
{ "data": "final_total" }
]
} );
I've tried
{ "data": "first_name" + "data": "last_name"},
But I get an error and table is not displayed. So how can I change the render to display first name next to last_name in the same cell not in the next cell
[UPDATE]
Tried
"ajax": "initTable.php",
"columns": [
{ "data": "orderid" },
{ "data": "first_name"},
{"data": "last_name"},
{ "data": "unix" },
{ "data": "final_total" }
],
"columnDefs": [
{
"render": function ( data, type, row ) {
return data + row[2];
},
"targets": 1
},
{ "visible": false, "targets": [ 2 ] }
]
(Note: I have to define column rows because I get many columns (about 20) and want to display just 4 or 5)
But I get the first name followed by 'undefined' something like "Andy undefined"
Use the code below:
{
"render": function ( data, type, row ){
return row["first_name"] + " " + row["last_name"];
},
"targets": 1
},
Also there is no need to include last_name column if you're hiding it.
Sample records in the collection,
(doc 1)
[{
"_id": ObjectId("567941aaf0058ed6755ab3dc"),
"hash_count": NumberInt(7),
"time": [
NumberInt(1450787170),
NumberInt(1450787292),
NumberInt(1450787307),
NumberInt(1450787333),
NumberInt(1450787615)
],
"word": "batman"
},
(doc 2)
{
"_id": ObjectId("567941aaf0058ed6755ab3dc"),
"hash_count": NumberInt(7),
"time": [
NumberInt(1450787170),
NumberInt(1450787292),
NumberInt(1450787307),
NumberInt(1450787333),
NumberInt(1450787354),
NumberInt(1450787526),
NumberInt(1450787615)
],
"word": "apple"
}]
Have stored using PHP,
I want to find the number of records in between time (1450787307) and (1450787615)
Answer:
apple=5
batman=3
What should be query for it?
I ran this command
{
aggregate : "hashtags",
pipeline:
[
{$match:{"time":{$gte:NumberInt(1450787307), $lte:NumberInt(1450787615)}}},
{$unwind:"$time"},
{$match:{"time":{$gte:NumberInt(1450787307), $lte:NumberInt(1450787615)}}},
{$group:{"_id":"$word","count":{$sum:1}}}
]
}
which gave this result
Response from server:
{
"result": [
],
"ok": 1
}
Since you are stuck with an older version of mongoDB, you cannot leverage the power of the array aggregation operators introduced in 3.2.
You would have to aggregate as below:
db.collection.aggregate([
{$match:{"time":{$gte:NumberInt(1450787307), $lte:NumberInt(1450787615)}}},
{$unwind:"$time"},
{$match:{"time":{$gte:NumberInt(1450787307), $lte:NumberInt(1450787615)}}},
{$group:{"_id":"$word","count":{$sum:1}}}
])
translated to PHP,
$result = $c->aggregate([
[ '$match' => [ 'time' => [ '$gte' => NumberInt(1450787307),
'$lte' => NumberInt(1450787615) ] ] ],
[ '$unwind' => '$time' ],
[ '$match' => [ 'time' => [ '$gte' => NumberInt(1450787307),
'$lte' => NumberInt(1450787615) ] ] ],
[ '$group' => [ '_id' => '$word', 'count' => [ '$sum' => 1 ] ] ]
]);
In version 3.2, you could use the combination of $filter and $size to acheive the same result and with less expensive operations.
db.collection.aggregate([
{$match:{"time":{$gte:NumberInt(1450787307),
$lte:NumberInt(1450787615)}}},
{$project:{"_id":0,"word":1,
"count":{$size:{$filter:
{"input":"$time",
"as":"t",
"cond":{$and:[
{$gte:["$$t",NumberInt(1450787307)]},
{$lte:["$$t",NumberInt(1450787615)]}]}
}
}
}
}}
])
ok , after trying a lot I have come with this answer and is correct
for
1450787615- lower limit
1450855155- upper limit
db.hashtags.aggregate([
{
"$match": {
"time": {
"$gte": 1450787615, "$lte": 1450855155
}
}
},
{ "$unwind": "$time" },
{
"$match": {
"time": {
"$gte": 1450787615, "$lte": 1450855155
}
}
},
{
"$group": {
"_id": "$word",
"count": {
"$sum": 1
}
}
}
])
answer is like
{
"result" : [
{
"_id" : "batman",
"count" : 3
},
{
"_id" : "dear",
"count" : 1
},
{
"_id" : "ghost",
"count" : 1
}
],
"ok" : 1
}
db.collection.find({time:{$gt: 1450787307, $lt: 1450787615}});
This will first give you a cursor of all docs that fit within your given time range. Once you have that you can iterate through the cursor and print out the name as well as some loop logic to find the number of occurences for each one. I've only lightly worked with mongodb so there may be a more efficient way to do this.
reference:
https://docs.mongodb.org/v3.0/reference/method/db.collection.find/