【问题标题】:Titanium using JSON ParsingTitanium 使用 JSON 解析
【发布时间】:2012-07-31 12:55:09
【问题描述】:

我正在尝试从 PHPMyAdmin 检索数据并尝试将数据显示到选择器更改...。 这是我的代码 var currentWin = Ti.UI.currentWindow;

 var sendit = Ti.Network.createHTTPClient();
 sendit.open('GET', 'http://localhost/mobileapp/productread.php');
 sendit.send();
  sendit.onload = function(){
 var json = JSON.parse(this.responseText);

 var json = json.mobile_product;

 var picker = Ti.UI.createPicker();
  // turn on the selection indicator (off by default)
 picker.selectionIndicator = true;

 var data = [];
  var pos;
 data.push (Ti.UI.createPickerRow({title:''+ json[pos].product_name +      '',custom_item:'b'}));
 }
 picker.add(data);
 currentWin.add(picker);



//var rw = db.execute("SELECT product_name, SUM(product_quantity)FROM mobile_product   GROUP BY product_name");

picker.addEventListener('change', function(e){
 var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://localhost/mobileapp/orderread.php');
sendit.send();


sendit.onload = function(){
var json = JSON.parse(this.responseText);

var json = json.mobile_product;
 var pos;
var product_name = json[e.rowIndex];
 for (pos=0; pos < json.length; pos++) {

alert(''+json[pos].orders + '');
}
}
})
 }

在这里,当我更改选择器时,警报会显示列(订单)的所有值以进行一次更改...如果我将选择器更改为下一个,则警报会再次显示列中的所有数据。 . 例如,如果我在列(顺序)中有 10 个值,并且如果我更改选择器值,则警报出现 10 次,有 10 个值。 我想根据选择器中的变化显示列中的值.... 我已经从 PHPMyAdmin 为选择器检索了数据(产品名称)...当我更改选择器时,我希望显示该特定名称的值...。 我正在使用查询 SELECT SUM(product_quantity) 作为来自 mobile_product GROUP BY product_name 的订单 在 PHP 文件中.. 我的 PHP 文件是

<?php
//cust-mysql-123-04
$link = mysql_connect('localhost', 'root', '');
 if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('mobileapp', $link);
if (!$db_selected) {
die ('Can\'t use  : ' . mysql_error());
}
// Set the default namespace to utf8
mysql_query("SET NAMES 'utf8'");
if($result = mysql_query("SELECT SUM(product_quantity) As orders FROM mobile_product     GROUP BY product_name")) {
//echo mysql_num_rows($result);
  while ($row=mysql_fetch_array($result)) {
   // $json.= "<li>".$row['first_name']."</li>";
    $json[]=array(
    'orders'=>$row['orders']
    );
 }
}
echo json_encode(array( 'mobile_product'  =>   $json ));

mysql_close();
?>

【问题讨论】:

  • 有人可以帮我解决这个问题吗???

标签: json titanium


【解决方案1】:

您必须包含不同的值才能在 SQL 查询中使用。目前,似乎只有一个聚合函数 SUM(),一个静态 GROUP 语句,仅此而已。这将始终返回相同的值。

您还返回了一个不同的列集,然后您可能期望使用 as orders alius。

如果您希望返回不同的集合,则需要组装不同的 SQL SELECT 语句。

考虑以下文件:

  1. This is how to prepare and execute a SQL statement against MySQL in php
  2. This function will clear many issues from the variable you pass to it
  3. Finally, you build the request with this method in Titanium.

  4. You may wish to review the following document regarding forming and consuming a GET request, as well

【讨论】:

    猜你喜欢
    • 2015-11-03
    • 1970-01-01
    • 2012-04-18
    • 2013-02-25
    • 2012-05-10
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多