如何在PHP中的jQuery/Ajax get中创建IF语句?
如何在jQuery中添加IF语句 我在数据库中有两(2)行具有不同状态的数据 我的index.php是显示数据的主文件 index.php如何在PHP中的jQuery/Ajax get中创建IF语句?,php,jquery,ajax,Php,Jquery,Ajax,如何在jQuery中添加IF语句 我在数据库中有两(2)行具有不同状态的数据 我的index.php是显示数据的主文件 index.php $.get("test.php", {"test": "test"}) .done(function (data) { var displayString = ""; $.each(JSON.parse(data), function (key, value) { displayString +=
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
displayString += "<td>" +
if (value['APPLICATION_STATUS'] == 'APPROVED'){
}
else {
+ '<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
}
displayString += "</td>" +
"</tr>";
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
<?php
require 'connection.php';
$query = "SELECT APPLICATION_STATUS FROM TABLE";
$result = oci_parse($conn, $query);
oci_execute($result);
$arr = array();
while ($row = oci_fetch_array($result)){
$arr[] = $row;
}
echo json_encode($arr);
?>
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
if(value['APPLICATION_STATUS'] == 'APPROVED')
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
else
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"<td>" +
'<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
$.get(“test.php”,{“test”:“test”})
.done(函数(数据){
var displayString=“”;
$.each(JSON.parse(数据)、函数(键、值){
显示字符串+=
"" +
"" +
'' +
"" +
显示字符串+=“”+
如果(值['APPLICATION_STATUS']=='APPROVED'){
}
否则{
+ '' +
}
显示字符串+=“”+
"";
});
$('fTableBody').css(“显示”、“无”);
$(“#tableBody”).html(显示字符串);
$(“#表视图”).dataTable({
dom:'Blfrtip',
是的,
按钮:[
“复制”、“csv”、“excel”、“pdf”、“打印”
],
“长度菜单”:[[10,25,50,-1],[10,25,50,“全部”]]
});
});
我使用jQuery.get从test.php获取数据,下面是我到目前为止得到的数据
test.php
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
displayString += "<td>" +
if (value['APPLICATION_STATUS'] == 'APPROVED'){
}
else {
+ '<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
}
displayString += "</td>" +
"</tr>";
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
<?php
require 'connection.php';
$query = "SELECT APPLICATION_STATUS FROM TABLE";
$result = oci_parse($conn, $query);
oci_execute($result);
$arr = array();
while ($row = oci_fetch_array($result)){
$arr[] = $row;
}
echo json_encode($arr);
?>
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
if(value['APPLICATION_STATUS'] == 'APPROVED')
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
else
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"<td>" +
'<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
我的问题是,如何在.each json解析中添加if语句
当“应用程序状态”为“已批准”时,我想禁用/隐藏edit.php按钮
如果有人能帮忙,我将不胜感激。谢谢我希望这能帮助解决您的问题:
<script>
$.get("test.php", {
"test": "test"
}).done(function(data) {
var displayString = "";
$.each(JSON.parse(data), function(key, value) {
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param=' + value['APPLICATION_STATUS'] + ' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>";
displayString += "<td>";
// make a condition
if (value['APPLICATION_STATUS'] == 'APPROVED') {
displayString += 'hide button';
} else {
displayString += '<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param=' + value['APPLICATION_STATUS'] + ' ">' + '<i class="fa fa-pencil"></i>' + '</a>';
}
displayString += "</td>";
displayString += "</tr>";
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [
[10, 25, 50, -1],
[10, 25, 50, "All"]
]
});
});
$.get(“test.php”{
“测试”:“测试”
}).完成(功能(数据){
var displayString=“”;
$.each(JSON.parse(数据)、函数(键、值){
显示字符串+=
"" +
"" +
'' +
"";
显示字符串+=“”;
//提出条件
如果(值['APPLICATION_STATUS']=='APPROVED'){
displayString+=“隐藏按钮”;
}否则{
显示字符串+='';
}
显示字符串+=“”;
显示字符串+=“”;
});
$('fTableBody').css(“显示”、“无”);
$(“#tableBody”).html(显示字符串);
$(“#表视图”).dataTable({
dom:'Blfrtip',
是的,
按钮:[
“复制”、“csv”、“excel”、“pdf”、“打印”
],
“长度菜单”:[
[10, 25, 50, -1],
[10,25,50,“全部”]
]
});
});
我不确定这是不是正确答案,但它解决了我的问题 index.php
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
displayString += "<td>" +
if (value['APPLICATION_STATUS'] == 'APPROVED'){
}
else {
+ '<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
}
displayString += "</td>" +
"</tr>";
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
<?php
require 'connection.php';
$query = "SELECT APPLICATION_STATUS FROM TABLE";
$result = oci_parse($conn, $query);
oci_execute($result);
$arr = array();
while ($row = oci_fetch_array($result)){
$arr[] = $row;
}
echo json_encode($arr);
?>
$.get("test.php", {"test": "test"})
.done(function (data) {
var displayString = "";
$.each(JSON.parse(data), function (key, value) {
if(value['APPLICATION_STATUS'] == 'APPROVED')
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
else
{
displayString +=
"<tr>" +
"<td>" +
'<a class="btn btn-xs btn-default btn-flat" target="_blank" href="view.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-eye"></i> ' +
'</a>' +
"</td>" +
"<td>" +
'<a class="btn btn-xs btn-warning btn-flat" target="_blank" href="edit.php?param='+ value['APPLICATION_STATUS'] +' ">' +
'<i class="fa fa-pencil"></i> ' +
'</a>' +
"</td>" +
"</tr>";
}
});
$('#fTableBody').css("display", "none");
$("#tableBody").html(displayString);
$("#table-view").dataTable({
dom: 'Blfrtip',
scrollX: true,
buttons: [
'copy', 'csv', 'excel', 'pdf', 'print'
],
"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]]
});
});
$.get(“test.php”,{“test”:“test”})
.done(函数(数据){
var displayString=“”;
$.each(JSON.parse(数据)、函数(键、值){
如果(值['APPLICATION_STATUS']=='APPROVED')
{
显示字符串+=
"" +
"" +
'' +
"" +
"";
}
其他的
{
显示字符串+=
"" +
"" +
'' +
"" +
"" +
'' +
"" +
"";
}
});
$('fTableBody').css(“显示”、“无”);
$(“#tableBody”).html(显示字符串);
$(“#表视图”).dataTable({
dom:'Blfrtip',
是的,
按钮:[
“复制”、“csv”、“excel”、“pdf”、“打印”
],
“长度菜单”:[[10,25,50,-1],[10,25,50,“全部”]]
});
});
谢谢 假设
value['APPLICATION\u STATUS']
返回正确的值,那么如果(value['APPLICATION\u STATUS']=='APPROVED'){}
不适用于您?@CarlBinalla不适用于我。它表示意外的if令牌。它会抛出错误:未捕获的语法错误:赋值中的左侧无效由于您尝试了某项操作,是否可以添加它?@CarlBinalla coding已在上面更新。但仍然存在相同的错误:未捕获的语法错误:assignmentNope中的左侧无效。错误:check.php:460未捕获的语法错误:赋值中的左侧无效正常。我已经找到了最简单的答案。我首先创建if语句,然后在if块上粘贴整个displayString。这解决了我的问题。谢谢