How to Parse Json In angular Js - php

Hi I am trying to parse json in angular js but didn't get answer pease help me
<div ng-app="" ng-controller="customersController">
<table>
<tr ng-repeat="x in names">
<td>{{ x.id }}</td>
<td>{{ x.mkrDate }}</td>
</tr>
</table>
</div>
<script>
function customersController($scope,$http) {
$http.get("http://www.w3schools.com/website/Customers_MySQL.php")
.success(function(response) {$scope.names = response;});
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
O/P:-
{
"id":"09851021211557572",
"mkrDate":1392023642000,
"mkrId":null,
"mkrName":null,
"timestamp":1410172522974,
"editorId":null,
"editorName":null,
"firstName":"Nandan",
"lastName":"Umarji",
"gender":null,
"dob":null,
"emailAdd":"nandan#mactores.com",
"mobileNo":"9819693822",
"userName":"nandan#mactores.com",
"password":"252117652",
"confirmationCode":"vkt95bf5c",
"photoUrl":null,
"accountType":"Company",
"bussinessName":"Mactores",
"companyUrl":"http://www.mactores.com",
"websiteUrls":"http://www.mactores.com ",
"countryCode":"+91",
"forgetPasswordCodeExpired":false,
"forgetPasswordCode":"vkt558e1e",
"address":{
"building":null,
"street":"Mumbai",
"area":null,
"location":null,
"landmark":null,
"city":"Mumbai",
"state":"Maharashtra",
"country":"India",
"pincode":"400001"
},
"request":true,
"confirm":false,
"deleted":false
}

It will work if you change the code to:
<table>
<tr>
<td>{{ names.id }}</td>
<td>{{ names.mkrDate }}</td>
</tr>
</table>
Or you can add brackets ( [ ] ) to the beginning and end of the json.

try to use <pre>{{ {'name':'value'} | json }}</pre>
https://docs.angularjs.org/api/ng/filter/json

Related

Laravel data not being displayed

I am trying to read data from a local collections from my mongodb database. Name of database Intuit name of collection post.
Code inside PostController:
<?php
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller{
public function home() {
$posts = Post::with(['user'])->get();
return view('home', ['posts' => $posts->toArray()]);
}}
Code inside Post:
class Post extends Model{
use HasFactory;
protected $fillable = [
'STATUS',
'DDC_CODE',
'TRADE_NAME',
'SCIENTIFIC_CODE',
'SCIENTIFIC_NAME',
'INGREDIENT_STRENGTH',
'DOSAGE_FORM_PACKAGE',
'ROUTE_OF_ADMIN',
'PACKAGE_PRICE',
'GRANULAR_UNIT',
'MANUFACTURER',
'REGISTERED_OWNER',
'UPDATED_DATE',
'SOURCE'
];}
All of the above are the titles of the columns in the table.
Next home.blade.php:
<!DOCTPE html>
<html>
<head>
<title>View Records</title>
</head>
<body>
<table border = "1">
<tr>
<td>Id</td>
<td>STATUS</td>
<td>DDC_CODE</td>
<td>TRADE_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>SCIENTIFIC_NAME</td>
<td>SCIENTIFIC_CODE</td>
<td>INGREDIENT_STRENGTH</td>
<td>DOSAGE_FORM_PACKAGE</td>
<td>ROUTE_OF_ADMIN</td>
<td>PACKAGE_PRICE</td>
<td>GRANULAR_UNIT</td>
<td>MANUFACTURER</td>
<td>UPDATED_DATE</td>
<td>SOURCE</td>
</tr>
#foreach ($posts as $post)
<tr>
<td>{{ $post->_id }}</td>
<td>{{ $post->STATUS }}</td>
<td>{{ $post->DDC_CODE }}</td>
<td>{{ $post->TRADE_NAME }}</td>
<td>{{ $post->SCIENTIFIC_CODE }}</td>
<td>{{ $post->SCIENTIFIC_NAME }}</td>
<td>{{ $post->INGREDIENT_STRENGTH }}</td>
<td>{{ $post->DOSAGE_FORM_PACKAGE }}</td>
<td>{{ $post->ROUTE_OF_ADMIN }}</td>
<td>{{ $post->PACKAGE_PRICE }}</td>
<td>{{ $post->GRANULAR_UNIT }}</td>
<td>{{ $post->MANUFACTURER }}</td>
<td>{{ $post->REGISTERED_OWNER }}</td>
<td>{{ $post->UPDATED_DATE }}</td>
<td>{{ $post->SOURCE }}</td>
</tr>
#endforeach
</table>
</body>
</html>
The route handler is as follows
Route::get('view','App\Http\Controllers\PostController#home');
all of this seems to be fine but when I load up the application I see nothing but the column titles not sure what I am missing, I believe it is not working because the query in PostController is incorrect and need to change it as it is taken from another projec. Any and all help and suggestions are welcome.
Instead of
Post::with(['user'])->get();
use
Post::with(['user'])->all();
subject to user relationship is properly defined in your Post model
You are sending data as array, But you are trying to get them as object in your blade view file.
Instead of getting data like this:
$post->STATUS
Try using like this:
$post['STATUS']
Don't use ->toArray()
Pass data as below & variables will get displayed
return view('home', ['posts' => $posts)]);

How to pass an array to js button click in phalcon volt template engine

I am passing an associative array from my controller to my view and then iterating it on the template engine
<tbody class="text-center">
{% for inv in inv_list %}
<tr>
<td><input type="checkbox" id="vehicle1" name="vehicle1" value="{{ inv['supplier_invoice_number'] }}"></td>
<td>{{ inv['contract_id'] }}</td>
<td>{{ inv['supplier_invoice_number'] }}</td>
<td>{{ inv['final_payable_amount'] }}</td>
<td>{{ inv['markup_value'] }}</td>
<td>{{ inv['markup_type'] }}</td>
<td>{{ inv['buyer_price'] }}</td>
<td><button class="btn btn-success" onclick="generateInvoice('{{ inv }}')">Invoice</button></td>
<td><button class="btn btn-success">Invoice Email</button></td>
</tr>
{% endfor %}
</tbody>
Like above .I want to pass the value of the object in the onclick method ,but its giving an error
Array to string conversion
.How to solve this .Please help me out
This Error Array to string conversionmeans tat you try to print an array. In your case invis an array. I think that you need only to pass the id like onclick="generateInvoice('{{ inv['contract_id'] }}')" or a other unique value from your inc array.

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id

Can anyone help me complete this? I'm a beginner and this is my first time using Laravel and PHP.
<?php
#foreach ($peminjaman as $value)
<tbody>
<tr>
<td>{{ ++$i }}</td>
<td>{{ $value->no_peminjaman }}</td>
<td>{{ $value->no_bmn }}</td>
<td>{{ $value->no_seri }}</td>
<td>{{ $value->kelengkapan }}</td>
</tr>
</tbody>
#endforeach
You need to add links(), so that the links for pagination can be generated in your view.Just have a look at the documentation here and you're good to go:https://laravel.com/docs/5.7/pagination
Hope this helps.
When you do :
$peminjaman = Model::paginate(10);
The $peminjaman is an instance of Illuminate\Pagination\LengthAwarePaginator.
In your case it seems like somewhere you have $peminjaman->id which as a result throwing the error you mentioned.
There is no property called id present on the LengthAwarePaginator paginator instance.

Can't pass data in view (laravel 4)

I am trying to pass an array from controller in view but it doesn't show error because it rezognise the variable but the data are not there, meanwhile when i try dd() in controller, i have data there.
Below is my code:
//Controller
public function showMessage(){
$id = Input::get('recordid');
foreach ($id as $chat_id) {
$history = Chat::where('chat_id','=', $chat_id)->get();
return View::make('chat.chatview', compact($history));
}
}
//View
#extends ('master')
#section ('content')
<div class="container">
<h1 style="color: white;">Chat History</h1>
<?php if (isset($history)) { ?>
#foreach($history as $row)
<table>
<tr>
<td><?php echo $history ;?></td>
</tr>
</table>
#endforeach
<?php } ?>
</div>
#stop
//route
Route::get('chathistory', function(){
return View::make('chat/chatview');});
The array that I get is from another view with the code below:
//view
#foreach($chat as $row)
<tr>
<td>{{ $row->chat_id }}</td>
<td>{{ $row->fullname }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->transcript_text }}</td>
<td>{{ $row->duration }}</td>
<td>
<input type="submit" value="History" class="btn pull-left" name="status[]" id="status[]" onclick="javascript:changeStatus('{{$row->chat_id}}','{{$arr}}')"/>
</td>
</tr>
<input name="recordid[]" id="recordid[]" type="hidden">
<?php $arr++; ?>
#endforeach
<script type="text/javascript">
function changeStatus(id, arr){
$('[id^=recordid]').eq(arr).val(id);
}
</script>
//Route
Route::get('individual', function(){
$chat = Chat::all();
return View::make('chat/individual')->with('chat', $chat);});
Route::post('individual','HomeController#showMessage');
Change the following line:
return View::make('chat.chatview', compact($history));
to
return View::make('chat.chatview', ['history' => $history]);
or
return View::make('chat.chatview')->with(compact('history'));
and try again.
Change here
return View::make('chat.chatview')->with('history',$history);

how to write php code to convert latitude longitude value to address in laravel 5.2

i am fetching values from the database and displaying it into a table using php laravel. Latitude logitude value am fetching and displaying in my table. there is another column address, so in that column i need to display the address by converting corresponding lat long value from the database. Can anyone tell how i can write the code for that?
my view blade is giving below
#extends('app')
#section('content')
</br></br></br></br></br></br></br></br>
<div class="templatemo-content-wrapper">
<div class="templatemo-content">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Vehicle Report</li>
</ol>
<h1>Vehicle Report</h1>
<p></p>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table id="example" class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Date</th>
<th>Time</th>
<th>Status</th>
<th>Lat/Long</th>
<th>Speed</th>
<th>Altitude</th>
<th>Odometer</th>
<th>Fuel</th>
<th>Address</th>
</tr>
</thead>
<tbody>
#foreach($devices as $device)
<tr>
<td>{{ date('Y/m/d H:i:s',($device->timestamp)) }}</td>
<td>{{ date('H:i:s',($device->timestamp)) }}</td>
<td>{{--}}{{ $device->statusCode }}--}}
#if($device->statusCode == '61715')
Stop
#elseif($device->statusCode=='62465')
Ignition_On
#elseif($device->statusCode=='61713')
Start
#elseif($device->statusCode=='61714')
InMotion
#elseif($device->statusCode=='62467')
Ignition_Off
#else
Speeding
#endif
</td>
<td>{{ round($device->latitude,5).'/'.round($device->longitude,5) }}</td>
<td>{{ $device->speed }}</td>
<td>{{ round($device->altitude) }}</td>
<td>{{ round($device->odometer) }}</td>
<td>{{ ($device->fuelLevel*100).'%' }}</td>
<td>{{ ?????????? }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</br>
</br></br></br></br>
Controller page is
class ReportController extends Controller
{
public $type = 'Device';
public function getAdd()
{
$vehicles = DB::table('device')->get();
return view('reports.vehicleDetail')->with('vehicles', $vehicles);
}
public function get(Request $request)
{
$account = Account::select('accountID')->where('accountID','=','gts')->get();
foreach ($account as $acc) {
$abc = $acc->accountID;
}
try {
$device_id = $request['deviceID'];
$from = strtotime($request['Fdate']);
$to = strtotime($request['Tdate']);
$devices=DB::table('device as b')
->join('eventdata as a', 'a.deviceID', '=', 'b.deviceID')
->where('a.deviceID', '=', $device_id)
->where('a.accountID', '=', $abc)
->where('a.creationTime', '>=', $from)
->where('a.creationTime', '<=', $to)
->select('a.accountID', 'a.deviceID', 'b.description', 'a.timestamp','a.statusCode',
'a.latitude', 'a.longitude', 'a.speedKPH as speed', 'a.heading', 'a.altitude', 'a.address', 'a.distanceKM as distance', 'a.odometerKM as odometer', 'a.IbatVolts', 'a.EbatVolts', 'a.ITempr', 'a.fuelLevel', 'a.inputState', 'a.IgnRuntime', 'GPSFixType', 'a.GPSPDOP', 'a.isTollRoad')->get();
// $devices = DB::table('eventdata')->get();
return view('reports.vehicleReport')->with('devices', $devices);
} catch (ModelNotFoundException $err) {
//Show error page
}
}
}
Thanks in advance.
You need to use Reverese Geocoding. Google maps API would be the best choice.
<?php
if(isset($_POST['latitude'])){
$lat=$_POST['latitude'];
$long=$_POST['longitude'];
$address=file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?latlng=$lat,$long&key=YOUR_API_KEY");
$json_data=json_decode($address);
$full_address=$json_data->results[0]->formatted_address;
}
?>

Categories