如何在通过php检索的列中指定特定值

How can I specify a specific value in a column retrieved via php?

本文关键字:检索 php      更新时间:2023-09-26

我想添加一个关于"type"列的if语句。.attr('type')之后是否有允许我为类型指定特定值的内容?

$.get("map_process.php", function (data) {
            $(data).find("marker").each(function () {
                  var name      = $(this).attr('name');
                  var address   = '<p>'+ $(this).attr('address') +'</p>';
                  var type      = $(this).attr('type');

所以$(this).attr('type');正在加载我的表"type"列值中的所有行。例如:
表格
姓名、地址、类型*
名称1,地址1,类型A,
名称2,地址2,类型B,
名称3,地址3,类型A,
etc

如何"访问"类型"列的实际值;例如$(this).attr('type').<something>('TypeA');

这可能吗?

第2版:map_process.php end

// Select all the rows in the markers table
$query = "SELECT * FROM markers";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo 'description="' . parseToXML($row['description']) . '" ';
  echo '/>';
}
// End XML file
echo '</markers>';
?>

您有两个选项:

(1)如果您可以完全控制正在调用的ajax页面,最好的解决方案是:

"将选中的复选框值发送到map_process.php中,然后获取从表中选择值,然后在Map中再次更新标记。"–Krish R

//jQuery
var cbVal = "TYPEA"; //Assign with checkbox value's associated string
$.get("map_process.php?checkboxval=" + cbVal, function (data) 
{ 
    //do something with the data returned
});
//PHP
$query = "SELECT * FROM markers where type = '".$_GET["checkboxval"]."'";

(2)如果您无法控制ajax调用的源(例如来自第三方源),或者您希望每次都返回所有标记,请考虑使用jQueryFilter方法。

//jQuery
var cbVal = "TYPEA"; //Assign with checkbox value's associated string
$(data).filter(function(index,val) 
{
    return $(val).attr('type') === cbVal;
}).each(function () 
{
    //do something with the data returned
});

请参阅此JSFiddle 上选项(2)的示例