How to display the time table from json format - php

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);

Related

How Update a data from an object of json type column in Laravel?

I have an object of json type column in my table (properties) in MySQL like:
[
{
"unit": "2",
"floor": "1",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": "3",
"bathrooms": "3",
"flat_name": "1A",
"flat_size": "1200",
"floor_plan": "217",
"price_per_sqft": "5000"
},
{
"unit": "2",
"floor": "1",
"price": "5000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "1B",
"flat_size": "1200",
"floor_plan": "215",
"price_per_sqft": "5000"
},
{
"unit": "1",
"floor": "2",
"price": "6000000",
"toilet": "2",
"balcony": "2",
"bedrooms": "2",
"customer": null,
"bathrooms": "3",
"flat_name": "2A",
"flat_size": "1250",
"floor_plan": "216",
"price_per_sqft": "5300"
}
]
How can I update customer id, where flat_name = 1B from this object in Laravel?
I have made the solution with using loop. Thank you who reply and answer
$objectitem=Property::where("id", 1)->first();
$updatedFlatDetails= $objectitem->flat_details;
foreach ($objectitem->flat_details as $key => $singleflat){
if($singleflat['flat_name']==$item->flat_name){
$updatedFlatDetails[$key]['customer']=(string)$item->customer_id;
}
}
$objectitem->flat_details = $updatedFlatDetails;
$objectitem->save();
Use collection after fetch data from database :
$target= $collection->filter(function ($item) {
return $item->flat_name=="1B";
})->values();
or filter before fetch data use eloquent :
Model::where('flat_name','1B')->get();

how to print a specific json key in php?

Here is my PHP code:
<?php
session_start();
include"smsciniz/vendor/autoload.php";
include"smsciniz/baglan.php";
//$insert = $DB->query("INSERT INTO sc_ulke(ulke_isim) VALUES(?)", array($_POST["ulke_isim"]));
$ulke = $DB->query("SELECT * FROM sc_ulke");
foreach ($ulke as $value) {
$url = file_get_contents("https://5sim.net/v1/guest/products/".trim($value["ulke_isim"])."/any");
$json = json_decode($url,true);
echo $json;
}
?>
And here is my JSON data:
"1688": {
"Category": "activation",
"Qty": 2000,
"Price": 14.83
},
"1xbet": {
"Category": "activation",
"Qty": 11413,
"Price": 19
},
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
"888casino": {
"Category": "activation",
"Qty": 4109,
"Price": 5
},
"99app": {
"Category": "activation",
"Qty": 5437,
"Price": 5
},
I want to print the area I marked but I don't know how to do.
I would be very happy if you could help me with an example, thank you in advance
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
We can iterate and find by particular key and print result using json_encode with JSON_PRETTY_PRINT
function findObjByKey($res, $keyToFind){
foreach ($res as $key => $value) {
if($key == $keyToFind) {
return $value;
}
}
return NULL;
}
$json = json_decode('
{"1688": {
"Category": "activation",
"Qty": 2000,
"Price": 14.83
},
"1xbet": {
"Category": "activation",
"Qty": 11413,
"Price": 19
},
"32red": {
"Category": "activation",
"Qty": 4113,
"Price": 7
},
"888casino": {
"Category": "activation",
"Qty": 4109,
"Price": 5
},
"99app": {
"Category": "activation",
"Qty": 5437,
"Price": 5
}}'
);
$res = findObjByKey($json, '32red');
$prittyPrint= json_encode($res, JSON_PRETTY_PRINT);
echo "<pre>".$prittyPrint."<pre>";
use json_encode($json['32red']) like this you can access any key in json

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},}
});
});

Want to get a result like below. how to write a query for that

{"result": [
{
"room_id": "1",
"floor_id": "1",
"flat_id": "1",
"room_name": "Room1",
"no_of_cots": "4",
"cot_info":[{"cot_id": "1",
"cot_name": "COT1",
"cot_status": "1"},
{"cot_id": "2",
"cot_name": "COT2",
"cot_status": "1"}],
"flat_name": "Sumangali PG",
"floor_name": "GroundFloor"
},
{
"room_id": "2",
"floor_id": "1",
"flat_id": "1",
"room_name": "Room2",
"no_of_cots": "2",
"cot_info":[{"cot_id": "1",
"cot_name": "COT1",
"cot_status": "1"},
{"cot_id": "2",
"cot_name": "COT2",
"cot_status": "1"}],
"flat_name": "Sumangali PG",
"floor_name": "GroundFloor"
} ]
}
OK you can use two page one for proccess the query example should be like that then you can addit to an array then decode the array and make echo like in the example bellew:
//page source
$query ='select room_id,floor_id,flat_id,room_name,no_of_cots,cot_info,cot_id,cot_name,cot_status,flat_name,floor_name
from table1 ,table2,table3,table4 where table1.id=table2.id and table2.id=table3.id and table4.id=table3.id';
$result = mysqli_query($conn,$query);
$array_list=array();
while($row = mysqli_fetch_array($result))
{
$array_list['room_id']=$row;
$array_list['floor_id']=$row;
$array_list['flat_id']=$row;
$array_list['room_name']=$row;
$array_list['cot_info']=$row;
$array_list['cot_name']=$row;
$array_list['cot_id']=$row;
$array_list['cot_status']=$row;
$array_list['flat_name']=$row;
$array_list['floor_name']=$row;
}
$json_data = array(
"result" => $array_list,
);
echo json_encode($json_data);
// page 2
$content='{"result": [ { "room_id": "1", "floor_id": "1", "flat_id": "1", "room_name": "Room1", "no_of_cots": "4", "cot_info":[{"cot_id": "1", "cot_name": "COT1", "cot_status": "1"}, {"cot_id": "2", "cot_name": "COT2", "cot_status": "1"}], "flat_name": "Sumangali PG", "floor_name": "GroundFloor" }, { "room_id": "2", "floor_id": "1", "flat_id": "1", "room_name": "Room2", "no_of_cots": "2", "cot_info":[{"cot_id": "1", "cot_name": "COT1", "cot_status": "1"}, {"cot_id": "2", "cot_name": "COT2", "cot_status": "1"}], "flat_name": "Sumangali PG", "floor_name": "GroundFloor" } ] }';
$json=json_decode($content);
foreach($json->result as $row)
{
print_r($row);
}

How to display the values from multidiamentional array

Here i have one array, from here i want display all values, but don't know how get all values from this array,i am new person of development any one know means please update my code.I spent lot of time but i am not able to find the exact answer
print_r($planMaster);
{
"status": "success",
"message": "Total 3 record(s) found.",
"total_record": 3,
"data": [
{
"planId": "1",
"planName": "Easy Plan",
"createdOn": "2017-10-23 15:17:08",
"createdBy": "Kani",
"updatedOn": "0000-00-00",
"updatedBy": "",
"planDescription": [
{
"descriptionId": "1",
"planId": "1",
"plandescTitle": "Contacts details up to",
"plandescResult": "24"
},
{
"descriptionId": "2",
"planId": "1",
"plandescTitle": "Bonus Contact details up to",
"plandescResult": "2"
},
{
"descriptionId": "3",
"planId": "1",
"plandescTitle": "Area Master Assistance",
"plandescResult": "No"
},
{
"descriptionId": "4",
"planId": "1",
"plandescTitle": "Contact details through sms",
"plandescResult": "Yes"
}
],
"planValue": [
{
"pvId": "2",
"planId": "1",
"startDate": "2017-10-23",
"endDate": "2017-10-30",
"planValue": "1000"
}
]
},
{
"planId": "2",
"planName": "Cool Plan",
"createdOn": "2017-10-23 16:58:32",
"createdBy": "Kani",
"updatedOn": "0000-00-00",
"updatedBy": "",
"planDescription": [
{
"descriptionId": "5",
"planId": "2",
"plandescTitle": "Contact details up to",
"plandescResult": "48"
},
{
"descriptionId": "6",
"planId": "2",
"plandescTitle": "Bonus Contact details up to",
"plandescResult": "4"
},
{
"descriptionId": "7",
"planId": "2",
"plandescTitle": "Area Master Assistance",
"plandescResult": "Yes"
},
{
"descriptionId": "8",
"planId": "2",
"plandescTitle": "Contact details through sms",
"plandescResult": "Yes"
}
],
"planValue": [
{
"pvId": "3",
"planId": "2",
"startDate": "2017-10-23",
"endDate": "2017-10-30",
"planValue": "2000"
}
]
},
{
"planId": "3",
"planName": "Free Plan",
"createdOn": "2017-10-23 17:09:20",
"createdBy": "Kani",
"updatedOn": "0000-00-00",
"updatedBy": "",
"planDescription": [
{
"descriptionId": "9",
"planId": "3",
"plandescTitle": "Contacts details up to",
"plandescResult": "5"
},
{
"descriptionId": "10",
"planId": "3",
"plandescTitle": "Area Master Assistance",
"plandescResult": "No"
},
{
"descriptionId": "11",
"planId": "3",
"plandescTitle": "Contact details through sms",
"plandescResult": "Yes"
}
],
"planValue": [
{
"pvId": "4",
"planId": "3",
"startDate": "2017-10-23",
"endDate": "2017-10-30",
"planValue": "0"
}
]
}
]
}
Expected Answer
Plan Name: Easy Plan
plandescTitle: Contacts details up to
plandescResult: 24
plandescTitle: Bonus Contact details up to
plandescResult": 2
I tried like this but not getting answer
<?php
$planMaster = GetResponse($API_URL.'getplan_Master');
if (!empty($planMaster)) {
foreach ($planMaster['data'] as $result) {
?>
<th><?php echo $result['planName']; ?> 1000/-</th>
<?php
foreach ($planMaster['planDescription'] as $descresult) {
echo $descresult['plandescTitle'];
}
?>
<?php
}
}
?>
Try this. I'll explain it if it works.
<?php
$planMaster = GetResponse($API_URL.'getplan_Master');
if (!empty($planMaster)) {
foreach ($planMaster['data'] as $result) {
?>
<th><?php echo $result['planName']; ?> 1000/-</th>
<?php
foreach ($result['planDescription'] as $descresult) {
echo $descresult['plandescTitle'];
}
?>
<?php
}
}
?>
you need to use json_decode() function to convert json data to php Array
$planMaster = GetResponse($API_URL.'getplan_Master');
$planMasterArray = json_decode($planMaster, true);
if (isset($planMasterArray) && is_array($planMasterArray)) {
foreach ($planMasterArray['data'] as $result) {
?>
<th><?php echo $result['planName']; ?> 1000/-</th>
<?php
foreach ($planMaster['planDescription'] as $descresult) {
echo $descresult['plandescTitle'];
}
?>
<?php
}
}
?>

Categories