PHP Error -- Use of undefined constant x - assumed 'x' - php

how to get data from table and pass it in controller in json format and get those data in view using angularjs
I want to get data from json encoded variable from controller and show it to view page using angularjs
view page using angularjs
<div class="card" ng-app = "myApp">
<table class="table table-hover" ng-controller = "menuController">
<thead>
<tr>
<th>Name</th>
<th>Parent menu</th>
<th>Order</th>
<th>Status</th>
<th class="text-center">Actions</th>
</tr>
</thead>
<tbody>
<tr ng-repeat = "x in values">
<td>{{x.name}}</td>
<td>{{x.parent_name}}</td>
<td>{{x.orders}}</td>
<td>{{x.status}}</td>
<td class="text-right">
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Edit row">
<i class="fa fa-pencil"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Copy row">
<i class="fa fa-copy"></i>
</button>
<button type="button" class="btn btn-icon-toggle"
data-toggle="tooltip" data-placement="top"
data-original-title="Delete row">
<i class="fa fa-trash-o"></i>
</button>
</td>
</tr>
</tbody>
</table>
</div>
var app = angular.module('myApp');
app.controller('menuController',function ($scope,$http) {
$scope.values = ["Milk", "Bread", "Cheese"];
});
PHP controller code
public function getmenu(){
$records=Menu::all();
return json_encode($records);
}
route code
Route::group(['prefix'=>'admin'],function(){
Route::get('/form','HomeController#form');
});
ErrorException (E_ERROR)
Use of undefined constant x - assumed 'x' (this will throw an Error in a future version of PHP) (View:form.blade.php)

I think it might be because you are serving your view which contains the angular code with Laravel, and in the file form.blade.php you use the angular syntax, which is the same as the blade one.
To solve that, you can try to remove blade word from the view filename, so it would become form.php or (alternative way instead of modifying the filename) each time you have to print something with the JavaScript framework instead of blade, use: #{{ variableToPrint }}.
So for example, part of your loop would become:
<tr ng-repeat="x in values">
<td>#{{x.name}}</td>
<td>#{{x.parent_name}}</td>
<td>#{x.orders}}</td>
<td>#{{x.status}}</td>
<!-- ... -->
You get the error as blade uses the same syntax to evaluate and print values, so if you write: {{ x.name }} blade finds a string literal x which gets interpreted as a constant.
If you prefix the # sign, blade will recognize that as an instruction not to be parsed, but that needs to be left as it is, it would just remove the #, leaving you with the correct code for you JavaScript loop

Related

how to declare variable by using the following symbol "{!!" in Laravel

I am using a ternary operator. Here is my code:
<td>
{!! $cooking->deleted_at ?
'<button type="button" data-id="{{$cooking->id}}"
data-target="#restore-CF">Enable</button>'
:'<button type="button" data-id="{{$cooking->id}}"
data-target="#exampleModal-3">Disable</button>'!!}
</td>
When I inspect by using the chrome developer tool and inspect over the following code data-id="{{$cooking->id}}" it shows <?php echo e($cooking->id); ?>,
but by inspecting I am suppose to get the value of a number i.e 12.
How to fix this error?
Remove quotes and use blade if statements
#if($cooking->trashed())
<button type="button" data-id="{{$cooking->id}}"
data-target="#restore-CF">Enable</button>
#else
<button type="button" data-id="{{$cooking->id}}"
data-target="#exampleModal-3">Disable</button>
#endif
Your qustions makes no sense
You should give a condition to check in ternary.
For your easyness, you may write
#if($cooking->deleted_at == 'your_condition_to_check_with')
<div>Your Statement</div>
#else
<div>Your Another Statement</div>
#endif
or, I think you may like to use php isset().
#if(isset($cooking->deleted_at))
Your question is very unclear though
I think it's because you are writing php and html at the same time and you are not separating the two, to separating php from html, include the php tags
<td>
<?php {!! $cooking->deleted_at ?
' ?><button type="button" data-id="<?php {{$cooking->id}} ?>"
data-target="#restore-CF">Enable</button><?php'
:'?><button type="button" data-id="<?php {{$cooking->id}} ?>"
data-target="#exampleModal-3">Disable</button><?php' !!}?> </td>
I think if you remove the mustaches on the data-id attribute it would work because you are already in a {!! !!} or better still my advice is to use blade syntax by wrapping it in an #if and #else like so
#if($cooking->deleted_at)
<button type="button" data-id="{{$cooking->id}}" data-target="#restore-CF">Enable</button>
#else
<button type="button" data-id="{{$cooking->id}}" data-target="#exampleModal-3">Disable</button>
#endif
I think this is more readable. I hope this helps.

Dynamic buttons href tests with phpunit

I'm initiating my tests using phpunit so, I have doubts about how to test things dynamically. I created a table dynamically as the image bellow
Here are my view:
<div class="panel-body">
#if(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
<table class="table">
<th>Unity Name</th>
<th>Phone Number</th>
<th>Actions</th>
<tbody>
#foreach($companies as $company)
<tr>
<td>{{$company->name}}</td>
<td>{{$company->owner_number}}</td>
<td>
{{Form::open(['method' => 'DELETE', 'url'=>'/admin/company/'.$company->id, 'style' => 'display:inline'])}}
<button type="submit" class="btn btn-default fa fa-trash-o"></button>
{{Form::close()}}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
So, how can I test the href tag if I haven't previously the tag id?
I did some tests like these one in Symfony projects.
In dev environment, I insert some data in a dev database. After this step, I launch functionnal tests. In these tests, I analyze tag's content. Here is an example from your data :
$client = static::createClient();
$crawler = $client->request('GET', '/yourUrl');
$node = $crawler->filter('#show-company-1');
self::assertNotEmpty($node, "Node #show-company-1 does not exists");
self::assertEmpty( $node->html(), "#show-company-1 content is not empty");
self::assertContains('/admin/company/1', $node->attr('href'), "a#show-company-1.href has not a good value");
Before request, you can add some logic to determine the id of your company.

How to Get Selected Record from Loop in Laravel

According to the loop in the fertilizer I'm just showing the value of one of them! It must be equal to the amount of data ID code because I want it to update the Value Form I, how do I do?
And if there is another way to say thanks.
In principle I would like to use AJAX Edition
#foreach($category as $cat)
<tr>
<td id="destroy_"><a data-delete id="destroy" data-id="{{$cat->id}}" href="#"><span
class="glyphicon glyphicon-remove" aria-hidden="true"></span></a></td>
<td id="update_"><a href="#" data-toggle="modal" data-target="#myModal"><span
class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a></td>
<td>{{$cat->category}}
</td>
<th id="dataId" class="text-right">{{$cat->id}}</th>
</tr>
#endforeach

Jquery Datatables and Jeditable - Data is not displaying

I am using Jquery DataTables and jEditable. I have a correct JSON response as follows:
[{"country_id":"18","country":"Aruba","country_enabled":"1"},{"country_id":"19","country":"Afghanistan","country_enabled":null},{"country_id":"22","country":"Angola","country_enabled":"1"},{"country_id":"23","country":"Anguilla","country_enabled":null},{"country_id":"24","country":"\u00c5land Islands","country_enabled":null},{"country_id":"25","country":"Albania","country_enabled":null},{"country_id":"26","country":"Andorra","country_enabled":null},{"country_id":"27","country":"United Arab Emirates","country_enabled":null},{"country_id":"29","country":"Argentina","country_enabled":null},{"country_id":"30","country":"Armenia","country_enabled":null},{"country_id":"31","country":"American Samoa","country_enabled":null},{"country_id":"32","country":"Antarctica","country_enabled":null},{"country_id":"33","country":"French Southern Territories","country_enabled":null},{"country_id":"34","country":"Antigua and Barbuda","country_enabled":null},{"country_id":"35","country":"Australia","country_enabled":null},{"country_id":"36","country":"Austria","country_enabled":null},{"country_id":"37","country":"Azerbaijan","country_enabled":null},{"country_id":"38","country":"Burundi","country_enabled":null},{"country_id":"39","country":"Belgium","country_enabled":null},{"country_id":"40","country":"Benin","country_enabled":null},{"country_id":"41","country":"Bonaire, Sint Eustatius and Saba","country_enabled":null},{"country_id":"42","country":"Burkina Faso","country_enabled":null},{"country_id":"43","country":"Bangladesh","country_enabled":null},{"country_id":"44","country":"Bulgaria","country_enabled":null},{"country_id":"45","country":"zoo","country_enabled":null},{"country_id":"46","country":"Xylaphone","country_enabled":null}]
The above is taken from Chrome's developer tools and from the XHR window and, therefore, I know the response looks correct and the data is being received.
Here is my HTML to display the data:
<div class="content">
<div id="pad-wrapper" class="form-page">
<div class="row">
<div class="col-md-12">
<h2>List of Countries</h2>
</div>
<div class="bs-example">
</br>
<form>
<div align = "left">
<button type="button" class="btn btn-success" onclick="window.location='<?php echo site_url("admin/country_add");?>'">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Country
</button>
<button type="button" class="btn btn-danger" onclick="window.location='<?php echo site_url("admin/country_delete");?>'">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span> Delete Countries
</button>
</div>
<!-- start table listing -->
<table id="myDataTable">
<thead>
<tr>
<th>country_id</th>
<th>country</th>
<th>country_enabled</th>
</thead>
<tbody>
</tbody>
</table>
<button id="btnAddNewRow">Add</button>
<button id="btnDeleteRow">Delete</button>
</div>
</div>
</div>
The datatable appears in the view but just says, "loading..." and no data is ever displayed.
I have renamed the column headers the same as my database but it still does not display the data.
There is an error in the console as follows:
In the client-side processing mode data provided via Ajax should have the following structure, see ajax option for more details.
{
"data": [
// row 1 data source,
// row 2 data source,
// etc
]
}
The solution is to correct your JSON data to look like:
{
"data": [
{"country_id":"18","country":"Aruba","country_enabled":"1"}
]
}
Alternative solution is to set ajax.dataSrc to empty string to indicate that you're returning plain array, see the sample code below:
$('#example').dataTable({
"ajax": {
"url": "data.json",
"dataSrc": ""
}
});

tablesorter cannot read property '0' of undefined with data

Hi I have a table dynamicaly filled (via php and jquery) but the tablesorter does not work
Scripts initialization at the end of the page
<!-- JavaScript -->
<script src="js/bootstrap.js"></script>
<!-- Page Specific Plugins -->
<script src="js/tablesorter/jquery.tablesorter.js"></script>
<script src="js/tablesorter/tables.js"></script>
<script>
function dynamicRefresh()
{
//generic
$(".pm-count").load("php-scripts/dynamic/pm-system/countU.php");
$(".pm-ddmenu").load("php-scripts/dynamic/pm-system/menu.php");
$(".com-count").load("php-scripts/dynamic/com-system/countU.php");
$(".com-ddmenu").load("php-scripts/dynamic/com-system/menu.php");
//Page Specific
var lpage = page - 1;
$("#new_msg").load("php-scripts/data/setlist/table.php", {'page':lpage});
//Refresh Rate
setTimeout("dynamicRefresh()",60000);
}
window.onload=dynamicRefresh;
</script>
Table Code
<table class="table table-hover table-striped tablesorter">
<thead>
<tr>
<th>Ordem # <i class="fa fa-sort"></i></th>
<th>Nome <i class="fa fa-sort"></i></th>
<th>Banda <i class="fa fa-sort"></i></th>
<th>Original/Cover <i class="fa fa-sort"></i></th>
<th>Acção <i class="fa fa-sort"></i></th>
</tr>
</thead>
<tbody id="new_msg">
</tbody>
</table>
I only click to sort the table when the table is filled with data I know if the table has no data this error occurrs.
Can it be because of the plugin initialization?
The plugin is loaded faster then the table data.
UPDATE #1
After close look at the debug console I see this
Built headers:,1ms jquery.tablesorter.js:147
[th.header, th.header, th.header, th.header, th.header, prevObject: x.fn.x.init[1], context: table.table.table-hover.table-striped.tablesorter, selector: "thead th", jquery: "1.10.2", constructor: function…]
I think it's safe to assume that when the plugin is initialized it builds the headers but also loads the data to faster sorting, if this is correct is there a function to "update" the initialization process?
UPDATE 2
After reading some more I found that I just need to add a trigger
in this case
$('.tablesorter').trigger('update');
It's working fine now

Categories