Data table column with if else condition - php

I want to make my one column with if else condition when data table render.
I am using laravel 4.2
my current code is
$result = DB::table('flowers')
->select('flowers.id as id', 'flowers.name as name',
'flowers.price as price',
'flowers.description as description','flowers.quality as quality',
'flowers.picture as picture','flowers.visibility_status as visibility_status');
return Datatables::of($result)
->edit_column('visibility_status', '<button type="button" onclick="activeFunction({{ $id }})" class="btn btn-primary bpad">Make Featured</button>')
->edit_column('picture', '<img src="{{ $picture }}" alt="flower Image" height="150">')
->add_column('edit', '<i class="icon-list-alt"></i>Edit')
->add_column('delete', '<i class="icon-trash"></i>Delete')
->make();
i want visibility_status column implement with if else condition like
if($visibility_status==1)
{
<div id="active454">
<button type="button" onclick="activeFunction(454)" class="btn btn-primary bpad">Make Featured</button></div>
<div id="block454" style="display:none"><button type="button" onclick="blockFunction(454)" class="btn btn-danger bpad">Make Unfeatured</button></div>
}
else
{
<div id="active454" style="display:none">
<button type="button" onclick="activeFunction(454)" class="btn btn-primary bpad">Make Featured</button></div>
<div id="block454" ><button type="button" onclick="blockFunction(454)" class="btn btn-danger bpad">Make Unfeatured</button></div>
}
Is it possible

I am not familiar with laravel but will risk it.
CSS
.hide{display:none;}
PHP Init
$class1 = array('class="hide" ','');
$class2 = array('','class="hide" ');
In PHP HTML generation
<div id="active454" $class1[$visibility_status]>
<div id="block454" $class2[$visibility_status]>
If $visibility_status is ambiguous as far as data type or null use:
$classX[intval($visibility_status)]

Related

How to fix POST data not properly set from ajax request?

I have a problem in getting the POST data from a page using ajax. In the jquery code the data is running smoothly and it will display when i alert the data. In the ajax request code the data from jquery has been successfully pass into showpercent.php file. Now the problem about showpercent.php, the data POST index percentage_id is unidentified. How can i fix this problem in getting the value of POST?
Below is the table list with button when the data is coming from.
<table>
<tr>
<td>
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="'.$row['hidden_id'] .'" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
</td>
</tr>
</table>
Below is the ajax request sending the data into showpercent.php file. When I alert the percentage_id from button click the data will show in the alert and the ajax was successfully pass into specific php file which is showpercent.php.
<script>
$(document).ready(function(){
$(".show-percentage").click(function(){
var percentage_id = $(this).data('percentage_id');
alert("Ajax Landing ID "+landing_id);
$.ajax({
url: 'ajax/showpercent.php',
type: 'POST',
cache: false,
data: { percentage_id : percentage_id },
success: function(data) {
alert(data);
$('#add-percentage').modal('show');
readRecords();
},
error: function(request, status, error){
alert("Error!! "+error);
}
});
// READ recods when the button is click
readRecords();
});
function readRecords() {
$.get("ajax/showpercent.php", {}, function (data, status) {
$(".display_percentage").html(data);
});
}
});
</script>
Below is the modal having a tab will display the data from ajax request. The class display_percentage will display the current data from showpercentage.
<div id="add-percentage" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Fish Landing Percentage</h4>
</div>
<div class="modal-body">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#menu1">Add Percentage</a></li>
</ul>
<div class="tab-content">
<div id="menu1" class="tab-pane fade in active">
<br>
<div class="display_percentage"></div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="primary" class="btn btn-primary" onclick="AddPercentage()"> Add Percentage </button>
<button type="button" id="danger" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
showpercent.php
This file will read by readRecords() function to display into the modal with tab class display_percentage when the button click is triggered.
This is now the problem comes, when the data was successfully pass from ajax request the data POST in the showpercent.php is not properly set and cannot proceed to the mysql process because the POST is not set.
<?php
include 'db.php';
$data = '
<table">
<thead>
<tr class="success">
<th ><center>No.</center></th>
<th ><center>Percentage ID</center></th>
<th ><center>Landing ID</center></th>
<th><center>Percentage</center></th>
<th><center>Date Added</center></th>
</tr>
</thead>';
if(isset($_POST['percentage_id'])){
$landing_id = $_POST['percentage_id'];
$query = mysqli_query($conn, "SELECT
percentage.percent_id,
percentage.landing_id,
percentage.percentage,
percentage.date_recorded
FROM
percentage
WHERE percentage.landing_id = '$landing_id'");
$number = 1;
while($row = mysqli_fetch_array($query)) {
$data .= '
<tr>
<td><center>' . $number . '</center></td>
<td><center>' . $row['percent_id'] . '</center></td>
<td><center>' . $row['landing_id'] . '</center></td>
<td><center>' . $row['percentage'] . '%</center></td>
<td><center>' . date("M. d, Y", strtotime($row['date_recorded'])) . '</center></td>
</tr>';
$number++;
}
}else{
echo 'Percentage id is not set!';
}
$data .= '
</table>';
echo $data;
?>
But in the console the ajax passing data will run smoothly.
I wish anybody will help me to fix this problem.
I read your code your showpercent.php, javascript and html and soo far and notice this
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="'.$row['hidden_id'] .'" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
and check this
data-percentage_id="'.$row['hidden_id'] .'"
you're doing it wrong this is not how you put php value into html value, this will return undefined in javascript if you try to get its value so
This is why
var percentage_id = $(this).data('percentage_id');
returns undefined in javascript and php
So replace it with this
data-percentage_id="<?php echo $row['hidden_id']; ?>"
So replace your button into like this
<button class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="<? echo $row['hidden_id']; ?>" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
thanks to #Keval Mangukiya
try:
add this in your html
<button id="percentage_id" class="btn btn-info show-percentage" title="Click to add view percentages!" data-percentage_id="<?=$row['hidden_id'] ?>" data-toggle="show-percentage"><i class="glyphicon glyphicon-time"></i></button>
set the value of your percentage_id before getting it
add to your JavaScript with
$("#percentage_id]").data('percentage_id',loading_id); //setter
var percentage_id = $(this).data('percentage_id'); //getter

Yajra Datatables and Entrust package

I'm working with role based permission using Zizaco Entrust package with yajra datatables.
when i'm giving permission to some users i have to touch datatables also.
This is my code ,
Controller.php
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', '#permission('user-edit')
View#endrole
Edit')
->make(true);
when i use permission inside datatables it is getting error , any one having idea to solve this ??same question in
yajra datatables and entrust role permission laravel
Entrust::can() checks if the user is logged in, and then if user has the permission. If the user is not logged the return will also be false.
Check below code:
Datatables::of(User::where('company_id',$company_id)->get())
->addColumn('action', function($company){
$action = '';
if (!Entrust::can('user-edit')) {
$action = 'View';
}
$action .= 'Edit';
return $action;
})
->make(true);
Made correction into code for {{}} and quotes issue.
You can also follow this technique
->addColumn('actions', function($admin) {
return view('admin.user.partials.admin_action', compact('admin'))->render();
})
And in your blade admin_action, you can write your view code as follows
#permission("admin-edit")
<a href="#" title="Edit"
class="btn-sm btn-primary editData"
data-id="{{ $admin->id }}"
data-fname="{{ $admin->f_name }}"
data-lname="{{ $admin->l_name }}"
data-email="{{ $admin->email }}"
data-redeem_manager="{{ $admin->redeem_manager }}"
data-mobile="{{ $admin->mobile }}"
data-role_id="{{ $admin->role_id }}"
><i class="fa fa-edit"></i> </a>
#endpermission
Entrust::can() not working with me causes an error.
but \Auth::user()->can('user-edit') works well with me
return datatables()->of($data)
->addColumn('actions', function ($data) {
$button = '<a class="btn btn-sm btn-info" href="' . route('users.show', $data->id) . '" >Show <i class="fa fa-eye"></i></a>';
if (\Auth::user()->can('user-edit')) {
$button .= ' <a class="btn btn-sm btn-primary" href="' . route('users.edit', $data->id) . '" >Edit <i class="fa fa-edit"></i></a>';
}
if (\Auth::user()->can('user-delete')) {
$button .= ' <a id="' . $data->id . '" class="delete btn btn-sm btn-danger" href="#" >Delete <i class="fa fa-trash"></i></a>';
}
return $button;
})
->rawColumns(['actions'])
->make(true);

laravel datatable CPU USAGE

This is my Controller:
public function anyData()
{
$myID = Auth::guard('cashier')->user()->id;
$players = User::where('parent_id','=', $myID)
->where('delete', '=', 1)
->orderBy(DB::raw('LENGTH(name),name'))
->get(['id', 'name', 'score', 'alarm', 'bonus_from_percentage']);
return Datatables::of($players)
->addColumn('actions4', function ($players) {
return
'
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#actions-modal" class="open-playerID btn btn-warning" data-toggle="modal"
data-target="#actions-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.SETTINGS').'"><span class="glyphicon glyphicon-cog"></span></a>
';
})
->addColumn('actions3', function ($players) {
return
'
<button type="button" class="'.changeClass($players->alarm).'"
value="OK" onclick="Enable('.$players->id.','.$players->alarm.')" id="enable"/><span class="glyphicon glyphicon-ok-circle"></span></button>
';
})
->addColumn('actions2', function ($players) {
return
'
<a href="#" data-toggle="modal" data-id="'.$players->id.'" data-target="#changeplayerpassword-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.CHANGEPLAYERPASS').'" class="playeridchangepass btn btn-warning">
<span class="glyphicon glyphicon-edit"></span></a>
<a href="#" data-id="'.$players->id.'" data-name="'.$players->name.'" data-target="#delete-modal" class="open-delete btn btn-warning" data-toggle="modal"
data-target="#delete-modal" data-toggle="tooltip" data-placement="top" title="'.trans('cashier.DELETE').'" ><span class="glyphicon glyphicon-remove"></span></a>
';
})
->addColumn('actions', function ($players) {
return
'<div class="btn-group text-center">
<button onclick="changeValueIn(this);" class="btn btn-primary btn-sm" style="width: 50px;" data-score-id="' . AppHelper::ToEuroC($players->score) . '" data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#In">IN</button>
<button onclick="changeValue(this)"class="btn btn-danger btn-sm" data-score-id="' . AppHelper::ToEuroC($players->score) . '"data-player-id="' . $players->id . '" data-name-id="' . $players->name . '" data-toggle="modal" data-target="#Out">OUT</button>
</div>
';
})
->addColumn('score', function ($players) {
return AppHelper::ToEuroC($players->score);
})
->addColumn('bonus_from_percentage', function ($players) {
return AppHelper::ToEuroC($players->bonus_from_percentage);
})
->addColumn('players', function ($players) {
return ($players->name) . ' ' .dikOn($players->id);
})
->make(true);
}
This script i run to refresh table each 5 seconds:
// REFRESH TABLE
function refreshptable() {
if (table != null)
table.ajax.reload(null, false);
setTimeout(refreshplayers
, 5000);
}
wen i upload on server my laravel project (i have active around 300 Users online)
CPU load going 200+
On my old PHP code (No Laravel) CPU load is 10-15
its something wrong with datatables or is laravel ?
i check the request is same no loops and all querys is same the only difference is laravel..
on TOP -C laravel use 3% my old code use 0.3% ..

PHP if else condition giving 500 internal server error

I am using some code to show editor a link to edit specific pages
but it is causing 500 internal server error for users who are not-logged-in
I am a starter and need immediate help
My code is
<?php
global $current_user, $wpdb;
$role = $wpdb->prefix . 'capabilities';
$current_user->role = array_keys($current_user->$role);
$ncaps = count($current_user->role);
$role = $current_user->role[$ncaps - 1];
if ($role == "editor") {
echo '<div class="user-tips">
<h4><u>Edit Equity Market</u></h4>
<h5><button class="btn btn-primary btn-sm">Edit Intraday Tips</button></h5>
<h5><button class="btn btn-primary btn-sm">Edit Positional Tips</button></h5>
<h4><u>Edit Commodity Market</u></h4>
<h5><button class="btn btn-primary btn-sm">Edit Commodity Tips</button></h5>
</div>';
}
else {
echo "";
}
?>
Thanks in advance
Try using function is_user_logged_in():
<?php
if ( is_user_logged_in() && current_user_can('edit_others_pages') ) {
echo '<div class="user-tips">
<h4><u>Edit Equity Market</u></h4>
<h5><button class="btn btn-primary btn-sm">Edit Intraday Tips</button></h5>
<h5><button class="btn btn-primary btn-sm">Edit Positional Tips</button></h5>
<h4><u>Edit Commodity Market</u></h4>
<h5><button class="btn btn-primary btn-sm">Edit Commodity Tips</button></h5>
</div>';
}
?>
EDIT: I also improved the check for roles a little. See Roles and Capabilities for others.

How to pass field id from database in checkbox using ignited datatables.?

i am trying to pass an id in the checkbox while generating datatables,My codes are as follows
Controller
public function list_view() {
$this->datatables->select('c.id,,c.image,c.first_name,c.email_primary,c.contact_mobile,c.contact_home,cn.country_name,c.unique_id,c.title')
->from('contact c')
->join('country cn','cn.id=c.country_id','left');
$this->datatables->add_column('action', '<input id="list-chk_" class="top-label" type="checkbox" value=c.id />', 'id');
$this->datatables->add_column('sync', '<span class="list-sync" ></span>', 'c.id');
$this->datatables->add_column('email', '<span class="list-mail" ></span>', 'c.id');
echo $this->datatables->generate('json');
}
I just want to pass c.id along with the checkbox,as I need to have further manipulation using that id in datatables,
Thank You .
You can just pass the variable as {{ c.id }}
Example:
$result = DB::table('users')
->select('users.id as id','users.first_name', 'users.last_name', 'users.email', 'users.username', 'users.activated');
return Datatables::of($result)
->add_column('actions', '<a aid="{{ $id }}" style="cursor: pointer;" class="edit_admin" data-toggle="modal" data-target="#edit_admin_modal" title="Edit Admin" ><i class="fa fa-pencil"></i></a> <a style="cursor: pointer;" aid="{{ $id }}" class="delete_admin" data-toggle="modal" data-target="#delete_admin_modal" title="Remove Admin" ><i class="fa fa-times-circle"></i></a> <a aid="{{ $id }}" style="cursor: pointer;" class="enable_disable_admin" title="Enable/Disable Admin" ><i class="fa fa-check-circle-o"></i></a>')
->remove_column('id')
->make();

Categories