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

时间:2014-10-18 05:34:57

标签: javascript php jquery

我想添加关于'类型'的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');正在加载表格中的所有行&#39; type&#39;列值。例如:
表格
姓名,地址,类型*
Name1,Address1,TypeA,
Name2,Address2,TypeB,
Name3,Address3,TypeA,

我如何才能获得访问权限?实际上&#39;类型的价值是什么?柱;例如。 $(this).attr('type').<something>('TypeA');

这可能吗?

Edit2:map_process.php结束

// 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 个答案:

答案 0 :(得分:0)

您有两个选项:

(1)如果您完全控制所调用的ajax页面,最佳解决方案是:

  

“将选中的复选框值发送到map_process.php并获取   从表中选择值并在Map中再次更新标记。“ -   克里希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调用的来源(例如来自第三方来源),或者您希望每次都返回所有标记,请考虑使用jQuery Filter方法。

//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
});

请参阅this JSFiddle

上选项(2)的示例