how Get Multiple value in array with same id laravel & Angular - php

Here I am trying to get question. At first understand all system we have quiz making system in that we make quizzes but in there a problem if we put a question in any quiz then we creating other quiz the how can we know this particular question is putted in other quizzes or not if yes then which-which. So, here i am trying to get all quizzes title of particular question id.
here is controller code
$questions = DB::table('questionbank')
->leftJoin('questionbank_quizzes', function($join)
{
$join->on('questionbank_quizzes.questionbank_id','=','questionbank.id');
})
->leftJoin('quizzes', function($join)
{
$join->on('quizzes.id','=','questionbank_quizzes.quize_id');
})
// ->groupBy('questionbank.id')
->where('questionbank.subject_id', '=', $request->subject_id)
->select('questionbank.id', 'questionbank.subject_id', 'topic_id', 'question_type', 'question', 'questionbank.marks', 'difficulty_level', 'status','quizzes.title')
->get(['questionbank.id', 'questionbank.subject_id', 'topic_id', 'question_type', 'question', 'questionbank.marks', 'difficulty_level', 'status', 'quizzes.title']);
return json_encode(array('topics'=>$topics, 'questions'=>$questions, 'subject'=>$subject));
And This is angular code
$scope.subjectChanged = function(selected_number) {
if(selected_number=='')
selected_number = $scope.subject_id;
subject_id = selected_number;
if(subject_id === undefined)
return;
route = '{{URL_QUIZ_GET_QUESTIONS}}';
data= { _method: 'post',
'_token':httpPreConfig.getToken(),
'subject_id': subject_id
};
httpPreConfig.webServiceCallPost(route, data).then(function(result){
result = result.data;
$scope.subjectQuestions = [];
$scope.subject = result.subject;
$scope.subjectQuestions = result.questions;
$scope.contentAvailable = true;
$scope.removeDuplicates();
});
}
$scope.removeDuplicates = function(){
if($scope.savedQuestions.length<=0 || $scope.subjectQuestions.length<=0)
return;
angular.forEach($scope.savedQuestions,function(value,key){
if(value.subject_id != $scope.subjectQuestions[0].subject_id)
return;
res = httpPreConfig.findIndexInData($scope.subjectQuestions, 'id', value.question_id);
if(res >= 0)
{
$scope.subjectQuestions.splice(res, 1);
}
});
}
This is blade view
<div ng-if="subjectQuestions!=''" class="vertical-scroll" >
<h4 class="text-success">Questions #{{ subjectQuestions.length }} </h4>
<table class="table table-hover">
<th >{{getPhrase('subject')}}</th>
<th>{{getPhrase('question')}}</th>
<th>{{getPhrase('Placed In')}}</th>
<th>{{getPhrase('type')}}</th>
<th>{{getPhrase('marks')}}</th>
<th>{{getPhrase('action')}}</th>
<tr ng-repeat="question in subjectQuestions | filter: { difficulty_level:difficulty, question_type:question_type, show_in_front_end:show_in_front_end , topic_id:topic, sub_topic_id:sub_topic } | filter: question_model track by $index ">
<td>#{{subject.subject_title}}</td>
<td title="#{{subjectQuestions[$index].question}}" ng-bind-html="trustAsHtml(question.question)">
</td>
<td> #{{question.title}} </td>
<td>#{{question.question_type | uppercase}}</td>
<td>#{{question.marks}}</td>
<td><a ng-click="addQuestion(question, subject);" class="btn btn-primary" >{{getPhrase('add')}}</a>
</td>
</tr>
</table>
</div>
</div>
</div>
Its result is like this
questions:[
0: {id: "4599", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 26", topic_id: "120"}
1: {id: "4599", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
2: {id: "4600", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 26", topic_id: "120"}
3: {id: "4600", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
4: {id: "4602", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 26", topic_id: "120"}
5: {id: "4602", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
6: {id: "4603", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 26", topic_id: "120"}
7: {id: "4603", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
]
In result you can see with same id title is different but i am stuck here how can i do that. Please help us in this problem.
I want result like this..
questions:[
0: {id: "4599", subject_id: "104", topic_id: "120", question_type: "radio",status: "1" subject_id: "104", title: "MOCK TEST NCERT BOOKS CHEMISTRY 26(<br> or ,)MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
1: {id: "4600", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: ""MOCK TEST NCERT BOOKS CHEMISTRY 26(<br> or ,)MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
2: {id: "4602", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: ""MOCK TEST NCERT BOOKS CHEMISTRY 26(<br> or ,)MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
3: {id: "4603", subject_id: "104", topic_id: "120", question_type: "radio",status: "1", subject_id: "104", title: ""MOCK TEST NCERT BOOKS CHEMISTRY 26(<br> or ,)MOCK TEST NCERT BOOKS CHEMISTRY 23", topic_id: "120"}
]

Related

How can I insert data from get() in laravel?

I want to insert data from get() method in L_Examen_Categorie table but it says :
Cannot insert explicit value for identity column in table
'L_Examen_Categorie' when IDENTITY_INSERT is set to OFF. (SQL: insert
into [L_Examen_Categorie] ([id], [code], [libelle], [coefficient],
[Note], [id_examen]) values (5, 777, 777, 3, 12, 1))"
Here is it my code
$categorie = DB::table('P_Examen_Categorie as p')
->where('p.id_examen', '=', $request->input('id_examen'))
->get();
foreach($categorie as $cat_item)
{
L_Examen_Categorie::insert((array)$cat_item);;
}
Categorie reponse:
[{id: "5", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"},…]
0: {id: "5", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"}
1: {id: "7", code: "39", libelle: "39", coefficient: "3", Note: "12", id_examen: "1"}
2: {id: "9", code: "777", libelle: "39", coefficient: "3", Note: "12", id_examen: "1"}
3: {id: "10", code: "777", libelle: "777", coefficient: "1", Note: "1211", id_examen: "1"}
4: {id: "11", code: "777", libelle: "777", coefficient: "3", Note: "12", id_examen: "1"}
You need to use the "toArray()" method. This method converts your collection object into an array and then inserts data.
$categorie = DB::table('P_Examen_Categorie as p')
->where('p.id_examen', '=', $request->input('id_examen'))
->get()->toArray();
L_Examen_Categorie::insert( $categorie);
But make sure the same column is available in the L_Examen_Categorie tables
Convert objects to associative array before inserting.
$categorie = json_decode($categorie, true);
L_Examen_Categorie::insert($categorie);

Nested JSON data via AJAX with jquery but getting undefined value

Javascript
JSON
Web Inspect
I have got an error message.
Uncaught ReferenceError: products is not defined.
$(document).ready(function(){
$("div").on("click", "a", function(){
var delivery_id = $(this).attr("id");
$.ajax({
url:"http://localhost:8888/dashboard/fetch_edit.php",
method:"POST",
data:{products:products},}
});
});
JSON
{
"id": "2",
"send_id": "10",
"tracking_id": "TI-000000010",
"user_id": "10",
"username": "istiaqahmed",
"email": "istiaqahmed121998#gmail.com",
"phone": "0176430886",
"company_name": "EVALY.COM.BD",
"company_phone": "01747588386",
"company_address": "Dhanmondi 28",
"com_email": "evaly#gmail.com",
"delivery_type": "Standard",
"packing": "regular",
"product_weight": "1",
"preferred_time": "morning",
"delivery_charge": "60",
"customer_name": "Zenith Jhony",
"to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli",
"to_phone": "01776065208",
"to_zone": "Dhaka",
"to_post_code": "1207",
"date": "2020-01-09 23:26:10",
"u_status": "Approve",
"notes": "note",
"products": [
{
"product_id": "1",
"product_name": "shampoo",
"product_quantity": "2",
"product_price": "2333",
"customer_send_id": "10"
},
{
"product_id": "2",
"product_name": "assf",
"product_quantity": "1",
"product_price": "232",
"customer_send_id": "10"
}
]
}
You are trying to reference an attribute which does not exist.
lets say you assign that JSON object o a variable like this:
obj = {
.... the JSON
}
you could then pass the JSON as follows
data:{products:obj.products}
Hope this help you.
var products_obj = '{ "id": "2", "send_id": "10", "tracking_id": "TI-000000010", "user_id": "10", "username": "istiaqahmed", "email": "istiaqahmed121998#gmail.com", "phone": "0176430886", "company_name": "EVALY.COM.BD", "company_phone": "01747588386", "company_address": "Dhanmondi 28", "com_email": "evaly#gmail.com", "delivery_type": "Standard", "packing": "regular", "product_weight": "1", "preferred_time": "morning", "delivery_charge": "60", "customer_name": "Zenith Jhony", "to_address1": "75\/1 Jafrabad Pulpar Pabna House Goli", "to_phone": "01776065208", "to_zone": "Dhaka", "to_post_code": "1207", "date": "2020-01-09 23:26:10", "u_status": "Approve", "notes": "note", "products": [ { "product_id": "1", "product_name": "shampoo", "product_quantity": "2", "product_price": "2333", "customer_send_id": "10" }, { "product_id": "2", "product_name": "assf", "product_quantity": "1", "product_price": "232", "customer_send_id": "10" } ] }';
products_obj = JSON.parse(products_obj);
var products= products_obj.products;
console.log(products);
$(document).ready(function(){
$("div").on("click", "a", function(){
var delivery_id = $(this).attr("id");
$.ajax({
url:"http://localhost:8888/dashboard/fetch_edit.php",
method:"POST",
data:{products:products},}
});
});

Create nested dynamic json with mysql data in php?

I want to display year and month in json format.Below is my json to get data but it not looks that i want please help me to get proper json.
<?php
include 'conn.php';
$year= array();
$sql = mysqli_query($conn,'SELECT SALARY_SLIP_ID,ORG_ID,EMPLOYEE_ID,PAY_MONTH,PAY_YEAR FROM india_salary_slip_details where EMPLOYEE_ID = 31');
while ($row = mysqli_fetch_array($sql ,MYSQLI_ASSOC))
{
$year[] = array(
'PAY_YEAR' => $row['PAY_YEAR'],
'PAY_MONTH' => $row['PAY_MONTH'],
);
}
echo json_encode($year);
?>
json data that i got with this is :
[
{
"PAY_YEAR": "2014",
"PAY_MONTH": "February"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "April"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "May"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "June"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "July"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "November"
},
{
"PAY_YEAR": "2014",
"PAY_MONTH": "December"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "January"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "February"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "March"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "April"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "May"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "June"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "July"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "August"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "September"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "October"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "November"
},
{
"PAY_YEAR": "2015",
"PAY_MONTH": "December"
}
]
I dont want this kind of json.
i want like this image
my static json code is below:
<script type="text/javascript">
$(document).ready(function(){
// tree data
var data = [{
id: "0",
text: "2017",
data: {},
children: [
{id: "1", text: "January", data: {Action: '|'}},
{id: "2", text: "February", data: {Action: '|'}},
{id: "3", text: "March", data: {Action: '|'}},
{id: "4", text: "April", data: {Action: '|'}},
{id: "5", text: "May", data: {Action: '|'}},
{id: "6", text: "June", data: {Action: '|'}},
{id: "7", text: "July", data: {Action: '|'}},
{id: "8", text: "August", data: {Action: '|'}},
{id: "9", text: "September", data: {Action: '|'}},
{id: "10", text: "October", data: {Action: '|'}},
{id: "11", text: "November", data: {Action: '|'}},
{id: "12", text: "December", data: {Action: '|'}}
],
'state': {'opened': false},
}];
// load jstree
$("div#jstree").jstree({
plugins: ["table","dnd","contextmenu"],
core: {
data: data,
check_callback: true
},
// configure tree table
table: {
columns: [
{width: 200, header: "Year"},
{width: 150, value: "Action", header: "Action", format: function(v) {if (v){ return 'Print | Delete' }}},
],
resizable: true,
draggable: true,
contextmenu: true,
width: 400,
height: 300
}
});
});
</script>
please help me to get this kind of data with dynamic json.i tried lot but dont get any idea. I want nested json as you can see.
Try this:
<?php
include 'conn.php';
$year= array();
$sql = mysqli_query($conn,'SELECT SALARY_SLIP_ID,ORG_ID,EMPLOYEE_ID,PAY_MONTH,PAY_YEAR FROM india_salary_slip_details where EMPLOYEE_ID = 31');
while ($row = mysqli_fetch_array($sql ,MYSQLI_ASSOC))
{
if( !isset( $year[ $row['PAY_YEAR'] ] ) ) {
$year[ $row['PAY_YEAR'] ] = array();
}
if( !isset( $year[ $row['PAY_YEAR'] ][ $row['PAY_MONTH'] ] ) ) {
$year[ $row['PAY_YEAR'] ][ $row['PAY_MONTH'] ] = array();
}
}
echo json_encode($year);
?>

PHP file_get_contents failed to open multiple stream

I am working on a php script that is supposed to use the file_get_contents function.
When I use it in my script just once it works fantastic and I don't get any error but if I implement it more then once I get the following error:
"https://engine.sighten.io/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/):
failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST".
Here are the details:
The webhook I receive is something like this:
{
"owned_by_user":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"date_completed":"None",
"date_created":"2017-08-16T12:47:17.700198+00:00",
"date_started":"None",
"milestone":{
"link":"/api/solar/milestone/eb8fad9a-47cf-448a-920d-9a4d35b3ea39",
"uuid":"eb8fad9a-47cf-448a-920d-9a4d35b3ea39",
"natural_id":"API TEST 1 Test Ignore//Cash // Sales Requirements"
},
"natural_id":"Installation Agreement",
"status":"SIGN",
"created_by":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"documents":[
{
"owned_by_user":{
},
"date_created":"2017-08-16T12:49:49.245223+00:00",
"created_by":{
},
"download_url":"/api/ops/download_from_remote/69c6a394-310a-4300-a18b-7544edfa28a9/",
"modified_by":{
},
"uuid":"69c6a394-310a-4300-a18b-7544edfa28a9",
"description":"Sun Us Solar Agreement v3.pdf",
"natural_id":"Sun Us Solar Agreement v3.pdf",
"date_updated":"2017-08-16T12:49:49.245248+00:00",
"owned_by_organization":{
},
"remote_host":"AMZ",
"name":"Sun Us Solar Agreement v3.pdf",
"merged_doc":true
},
{
"owned_by_user":{
},
"date_created":"2017-08-16T12:49:54.873908+00:00",
"created_by":{
},
"download_url":"/api/ops/download_from_remote/7737b88f-0dec-4917-bf73-aa52e3c5e6ab/",
"modified_by":{
},
"uuid":"7737b88f-0dec-4917-bf73-aa52e3c5e6ab",
"description":"Sun Us Solar Agreement v3.pdf",
"natural_id":"Sun Us Solar Agreement v3.pdf",
"date_updated":"2017-08-16T12:49:54.873936+00:00",
"owned_by_organization":{
},
"remote_host":"AMZ",
"name":"Sun Us Solar Agreement v3.pdf",
"merged_doc":true
},
{
"owned_by_user":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"date_created":"2017-08-16T13:03:42.708770+00:00",
"created_by":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"download_url":"/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/",
"modified_by":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"uuid":"1644d83f-675b-42f9-a8cc-b2cb63d9c705",
"description":"Harry Potter and the Sorcerer's Stone.pdf",
"natural_id":"Harry Potter and the Sorcerer's Stone.pdf",
"date_updated":"2017-08-16T13:03:42.708799+00:00",
"owned_by_organization":{
"link":"",
"uuid":"e62239df-7f3a-4ead-b4cd-500541f8b094",
"natural_id":"Sunus Solar"
},
"remote_host":"AMZ",
"name":"Harry Potter and the Sorcerer's Stone.pdf",
"merged_doc":true
}
],
"modified_by":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"uuid":"411143d0-ecde-429d-943f-d4f22f12c87c",
"type":"DOC",
"assigned_to":{
"link":"",
"uuid":"ec5aec48-621e-4cd6-a029-d5da1a9a18d5",
"natural_id":"jbantock#catalystgroupdevelopment.com"
},
"date_updated":"2017-08-16 13:03:42.768407+00:00",
"date_approved":"None",
"owned_by_organization":{
"link":"",
"uuid":"e62239df-7f3a-4ead-b4cd-500541f8b094",
"natural_id":"Sunus Solar"
},
"comments":[
],
"site_id":"ac396b97-b22a-4914-9a0f-dbbda61116eb",
"name":"Installation Agreement" }
The php code part, which makes the download:
foreach($documents as &$document) {
$downloadlink = $document['download_url'];
$downloadname = $document['name'];
$remote_url = 'https://engine.sighten.io' . $downloadlink;
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Authorization: Token cd048c7b0a127d876e5481ccbd0beb1566bebea2"
)
);
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
$download = file_put_contents($downloadname, $file);
}
As you can see the json file contains 2 links/files. The script work well in case of the first file, I can download on my server, but in case of second file I get the above mentioned error. I have checked and the $remote_url variable contains the correct URL of the files.
CLARIFICATION:
When I execute the script I see the following
Files
The problem is that the second file has no content. Please note, that I have executed the script once again, with other files so the json in this case was the following:
{"date_created": "2017-08-16T15:10:19.327922+00:00", "status": "SIGN", "owned_by_user": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "documents": [{"download_url": "/api/ops/download_from_remote/b608171b-4cd3-4b2a-bef0-19635b00ed52/", "merged_doc": true, "owned_by_user": {}, "date_updated": "2017-08-16T15:10:45.039007+00:00", "uuid": "b608171b-4cd3-4b2a-bef0-19635b00ed52", "remote_host": "AMZ", "description": "Sun Us Solar Agreement v3.pdf", "created_by": {}, "date_created": "2017-08-16T15:10:45.038979+00:00", "modified_by": {}, "owned_by_organization": {}, "name": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf"}, {"download_url": "/api/ops/download_from_remote/049957a5-1340-4e47-bfa2-3e4832d8b88f/", "merged_doc": true, "owned_by_user": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "date_updated": "2017-08-16T15:17:21.529021+00:00", "uuid": "049957a5-1340-4e47-bfa2-3e4832d8b88f", "remote_host": "AMZ", "description": "Harry Potter and the Sorcerer's Stone.pdf", "created_by": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "date_created": "2017-08-16T15:17:21.528992+00:00", "modified_by": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "owned_by_organization": {"link": "", "natural_id": "Sunus Solar", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094"}, "name": "Harry Potter and the Sorcerer's Stone.pdf", "natural_id": "Harry Potter and the Sorcerer's Stone.pdf"}], "date_updated": "2017-08-16 15:17:21.584150+00:00", "date_approved": "None", "modified_by": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "type": "DOC", "created_by": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "comments": [], "uuid": "0f94a08f-4f06-444c-babe-f32ed059a88f", "assigned_to": {"link": "", "natural_id": "jbantock#catalystgroupdevelopment.com", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5"}, "owned_by_organization": {"link": "", "natural_id": "Sunus Solar", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094"}, "date_started": "None", "name": "Installation Agreement", "date_completed": "None", "natural_id": "Installation Agreement", "milestone": {"link": "/api/solar/milestone/1b599fcd-448f-4444-a636-1ae931f25461", "natural_id": "API TEST 1 Test Ignore//Cash // Sales Requirements", "uuid": "1b599fcd-448f-4444-a636-1ae931f25461"}, "site_id": "c2504124-55e4-43b5-9a0c-370ec494d887"}
i can't access your request link because i don't have an API key,
When I use it in my script just once it works fantastic and I don't get any error but if I implement it more then once I get an error
Are you sure you got one that doesn't have API limitations (like one request a second)?
file_get_contents and file_put_content sometimes say synchronously they finished their jobs when in fact the hard disk is still writing content, i had a similar issue and i figured out it worked if i called the function twice but with a 4s sleep inbetween
Hope this will help.
This script is working for me. It does not save to file, as discussion above revealed TS cannot read the 3rd file and so it would not be the problem here
at the end of the foreach-loop some vars are dumped to screen for debugging purposes.
<?php
error_reporting(E_ALL); ini_set('display_errors', true);
$d = '{"owned_by_user": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "date_completed": "None", "date_created": "2017-08-16T12:47:17.700198+00:00", "date_started": "None", "milestone": {"link": "/api/solar/milestone/eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "uuid": "eb8fad9a-47cf-448a-920d-9a4d35b3ea39", "natural_id": "API TEST 1 Test Ignore//Cash // Sales Requirements"}, "natural_id": "Installation Agreement", "status": "SIGN", "created_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "documents": [{"owned_by_user": {}, "date_created": "2017-08-16T12:49:49.245223+00:00", "created_by": {}, "download_url": "/api/ops/download_from_remote/69c6a394-310a-4300-a18b-7544edfa28a9/", "modified_by": {}, "uuid": "69c6a394-310a-4300-a18b-7544edfa28a9", "description": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf", "date_updated": "2017-08-16T12:49:49.245248+00:00", "owned_by_organization": {}, "remote_host": "AMZ", "name": "Sun Us Solar Agreement v3.pdf", "merged_doc": true}, {"owned_by_user": {}, "date_created": "2017-08-16T12:49:54.873908+00:00", "created_by": {}, "download_url": "/api/ops/download_from_remote/7737b88f-0dec-4917-bf73-aa52e3c5e6ab/", "modified_by": {}, "uuid": "7737b88f-0dec-4917-bf73-aa52e3c5e6ab", "description": "Sun Us Solar Agreement v3.pdf", "natural_id": "Sun Us Solar Agreement v3.pdf", "date_updated": "2017-08-16T12:49:54.873936+00:00", "owned_by_organization": {}, "remote_host": "AMZ", "name": "Sun Us Solar Agreement v3.pdf", "merged_doc": true}, {"owned_by_user": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "date_created": "2017-08-16T13:03:42.708770+00:00", "created_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "download_url":"/api/ops/download_from_remote/1644d83f-675b-42f9-a8cc-b2cb63d9c705/", "modified_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "uuid": "1644d83f-675b-42f9-a8cc-b2cb63d9c705", "description": "Harry Potter and the Sorcerer\'s Stone.pdf", "natural_id": "Harry Potter and the Sorcerer\'s Stone.pdf", "date_updated": "2017-08-16T13:03:42.708799+00:00", "owned_by_organization": {"link": "", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id": "Sunus Solar"}, "remote_host": "AMZ", "name": "Harry Potter and the Sorcerer\'s Stone.pdf", "merged_doc": true}], "modified_by": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "uuid": "411143d0-ecde-429d-943f-d4f22f12c87c", "type": "DOC", "assigned_to": {"link": "", "uuid": "ec5aec48-621e-4cd6-a029-d5da1a9a18d5", "natural_id": "jbantock#catalystgroupdevelopment.com"}, "date_updated": "2017-08-16 13:03:42.768407+00:00", "date_approved": "None", "owned_by_organization": {"link": "", "uuid": "e62239df-7f3a-4ead-b4cd-500541f8b094", "natural_id": "Sunus Solar"}, "comments": [], "site_id": "ac396b97-b22a-4914-9a0f-dbbda61116eb", "name": "Installation Agreement"}';
$documents = json_decode($d);
$documents = $documents->documents;
foreach($documents as $document) {
$downloadlink = $document->download_url;
$downloadname = $document->name;
$remote_url = 'https://engine.sighten.io' . $downloadlink;
$opts = array('http' => array('method' => "GET", 'header' => "Authorization: Token cd048c7b0a127d876e5481ccbd0beb1566bebea2"
));
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
$size = strlen($file);
if(1) {
echo 'on line: ' . __LINE__ . ' in file ' . __FILE__;
printf('<pre>%s</pre>', print_r($document->download_url, 1));
printf('<pre>%s</pre>', print_r($downloadname, 1));
printf('<pre>%s</pre>', print_r($size, 1));
}
} ?>
Are you sure you send the Auth code along in your real script?

How to display the time table from json format

Here i am doing school management time table creation,already created the time table for class Name (LKG) and section Name (A).Here i will share my DB structure.
time_table (Table Name)
timeTableId classId sectionId subjectId teacherId dayId period
1 LKG A Tamil X Monday 1
2 LKG A Tamil X Monday 3
3 LKG A Hindi X Monday 2
4 LKG A English X Monday 4
5 LKG A Science X Monday 5
6 LKG A Maths X Monday 6
7 LKG A Science X Monday 7
8 LKG A Hindi X Monday 8
9 LKG A Tamil X Tuesday 1
10 LKG A Tamil X Tuesday 3
11 LKG A Hindi X Tuesday 2
12 LKG A English X Tuesday 4
13 LKG A Science X Tuesday 5
14 LKG A Maths X Tuesday 6
15 LKG A Science X Tuesday 7
16 LKG A Hindi X Tuesday 8
Now i want to display the time table Like this
expected Answer
Day Period1 Period2 Period3 Period4 Period5 Period6 Period7 Period8
Monday Tamil Hindi Tamil English Science Maths Science Hindi
Tuesday Tamil Hindi Tamil English Science Maths Science Hindi
My HTML Table
<table id="timeTableList" class="table table-bordered table-striped">
<thead>
<tr>
<th>Day</th>
<th>Peroid 1</th>
<th>Peroid 2</th>
<th>Peroid 3 </th>
<th>Peroid 4</th>
<th>Peroid 5</th>
<th>Peroid 6</th>
<th>Peroid 7</th>
<th>Peroid 8</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
My Controller
public function timetabeleget_validate()
{
date_default_timezone_set('Asia/Kolkata');
$data = array(
"classId" => $_POST['className'],
"sectionId" => $_POST['sectionName'],
);
$login_response= $this->Add_settings->get_timeTable($data);
}
My Model
public function get_timeTable($params)
{
$this->db->select('*');
//$this->db->order_by("timeTableId","desc");
$this->db->where('status !=', '1');
$this->db->where('schoolId =', $this->session->userdata('school_id'));
$this->db->where('classId =', $params['classId']);
$this->db->where('sectionId =', $params['sectionId']);
$result=$this->db->get('time_table')->result();
if(!empty($result)){
$data = array("status" => "success", "monday" => $result);
echo json_encode($data);
}
else{
$data = array("status" => "Error", "description" => "Not Found");
echo json_encode($data);
}
}
My Response
{
"status": "success",
"monday": [
{
"timeTableId": "1",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "5",
"teacherId": "7",
"dayId": "1",
"period": "1",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "3",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "10",
"teacherId": "2",
"dayId": "1",
"period": "2",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "2",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "5",
"teacherId": "7",
"dayId": "1",
"period": "3",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "4",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "6",
"teacherId": "3",
"dayId": "1",
"period": "4",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "5",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "8",
"teacherId": "6",
"dayId": "1",
"period": "5",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "6",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "7",
"teacherId": "3",
"dayId": "1",
"period": "6",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "7",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "8",
"teacherId": "6",
"dayId": "1",
"period": "7",
"academicYear": "2017",
"status": "0"
},
{
"timeTableId": "8",
"schoolId": "4",
"classId": "8",
"sectionId": "18",
"subjectId": "10",
"teacherId": "5",
"dayId": "1",
"period": "8",
"academicYear": "2017",
"status": "0"
}
]
}
Here bassed upon my response i am not able to display the time table format,
My FORM and AJAX
<form action="#" method="POST" id="getTimeTableForm">
<div class="box-body">
<div class="form-group">
<label for="Class Name" class="col-sm-3 control-label">Class Name: <span class="star"> * </span></label>
<div class="col-md-9">
<select class="form-control select2 classId" style="width: 100%;" name="className" onchange="getSection(this.value);" required="" data-msg-required="Please select class name" aria-required="true">
<option value="">Select Class</option>
<?php
foreach ($dbResultClass as $desc) :
?>
<option value="<?php echo $desc->class_id; ?>"><?php echo $desc->class_name; ?></option>
<?php endforeach;?>
</select>
</div>
</div>
<div class="form-group">
<label for="Section Name" class="col-sm-3 control-label">Section Name: <span class="star"> * </span></label>
<div class="col-md-9">
<select class="form-control select2 sectionAppend sectionId" style="width: 100%;" name="sectionName" required="" data-msg-required="Please select section name" aria-required="true" >
<option value="">Select Section</option>
</select>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right" id="btn-submit">Submit</button>
</div>
<!-- /.box-footer -->
</form>
AJAX
$(document).ready(function(){
$("#btn-submit").click(function(e){
e.preventDefault();
if($("#getTimeTableForm").valid()){
$( "#btn-submit" ).prop( "disabled", true );
$.ajax({
type:'POST',
url :"timetabeleget_validate",
data: $('form#getTimeTableForm').serialize(),
success: function(data) {
var res=jQuery.parseJSON(data);
console.log(res);
/*if(res['status']=="success"){
var htmlString='';
$.each( res['monday'], function( key, value ) {
var period = value.period
});
alert(period);
$('#timeTableList tbody').empty().append(htmlString);
}else{
$('#timeTableList tbody').empty().append('No Datas Found');
}*/
},
error:function(exception){
alert('Exeption:'+exception);
}
});
}
});
});
Update following code with your ajax success function
if(res['status']=="success"){
var rw = '<tr><td>Monday</td>';
$.each( res['monday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr><tr><td>Tuesday</td>';
$.each( res['tuesday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr><td>Wednesday</td>';
$.each( res['wednesday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr><td>Thursday</td>';
$.each( res['thursday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr><td>Friday</td>';
$.each( res['friday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr><td>Saturday</td>';
$.each( res['saturday'], function( key, value ) {
rw += '<td>'+value.subjectId+'</td>';
});
rw += '</tr>';
$('#timeTableList tbody').empty().append(rw);
}
Hope it will be work for you. If you need any help let us know... We happy to help you out...
You first have to change the code in the model where you are fetching the data..
Apply the orderBy on period column instead of timeTableId column. So that you can display the subjects in ascending order.
Below code is the part of your ajax call after getting the response.
Here, first, you need to unset the key 'status' from the response object.
delete res.status;
var time_table_template = '<tr>';
$.each(res,function(index, entries){
time_table_template += '<td>'+index+'</td>';
$.each(entries, function(index, data){
time_table_template += '<td>'+data.subjectId+'</td>';
});
});
time_table_template += '</tr>';
$("#timeTableList tbody").append(time_table_template);

Categories