在对象PHP中添加一个数组作为JSON

时间:2013-10-19 06:20:31

标签: php objective-c

我有一个iPhone代码如下

FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];
NSString *sqlSelectQuery = @"SELECT * FROM drinks";

// Query result
FMResultSet *results = [database executeQuery:sqlSelectQuery];
while([results next])
{
    Drinks *drink = [[Drinks alloc] init];

    [drink setDrinkId:[results intForColumn:@"id"]];
    [drink setVenueId:[results intForColumn:@"venueId"]];
    [drink setName:[results stringForColumn:@"name"]];
    [drink setType:[results intForColumn:@"venueId"]];

    NSMutableArray *servingSizes = [[NSMutableArray alloc] init];
    NSString *sqlSelectQuery = [NSString stringWithFormat:@"SELECT * FROM servingSizes WHERE drinkId = %d", drink.drinkId];

    FMResultSet *servingResult = [database executeQuery:sqlSelectQuery];
    while([servingResult next])
    {
        Servings *serving = [[Servings alloc] init];

        [serving setDrinkId:[servingResult intForColumn:@"drinkId"]];
        [serving setServingId:[servingResult intForColumn:@"id"]];
        [serving setServingSize:[servingResult intForColumn:@"servingSize"]];
        [serving setPrice:[servingResult intForColumn:@"price"]];

        [servingSizes addObject:serving];
    }

    [drink setServingsArray:servingSizes];
    [drinksArray addObject:drink];
}

本质上,这段代码读取内置数据库并创建一个Object DRINK,并从drink的id获取从表中提供服务的对象数组。

但是,我想在Php服务器端执行以下操作。

这是我的代码

function GetAllDrinks() 
    {   
        try {

            $conn = $this->GetDBConnection();

            $statement = $conn->prepare('SELECT * FROM drinks');
            $statement->execute();

            if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC))) 
            {
                return false;
            }

            foreach($row as $drink)
            {
               $drinkId  = $row->id;                
               $statement = $conn->prepare('SELECT * FROM servingTable WHERE id = :drinkId)');

                $statement->bindParam(':drinkId' , $drinkId, PDO::PARAM_INT);
                $statement->execute();

                if(($serve = $statement->fetchAll(PDO::FETCH_ASSOC))) 
                {
                    // WHAT TO DO HERE
                }
            }

            $conn = null;
            return $row;

        } catch(PDOException $e) {
                throw $e;
        }   
        return $row;
    }

现在我想为所有人创建一个JSON字符串,但我不确定如何在drink对象中添加服务?我希望有人在这里提供帮助,因为我不是PHP的专家

2 个答案:

答案 0 :(得分:0)

// Add &-sign to create a reference to $drinks
foreach($row as &$drink)
{
   $drinkId  = $row->id;                
   $statement = $conn->prepare('SELECT * FROM servingTable WHERE id = :drinkId)');

    $statement->bindParam(':drinkId' , $drinkId, PDO::PARAM_INT);
    $statement->execute();

    if(($serve = $statement->fetchAll(PDO::FETCH_ASSOC))) 
    {
        $drink['servings'] = $serve;
    }
}

现在$row应该有所有饮料,每个饮料都有一个字段servings。获得JSON:

echo json_encode($row);

答案 1 :(得分:0)

请不要在循环中使用prepare(),此方法非常慢,只需准备一次查询。

// Add &-sign to create a reference to $drinks
$statement = $conn->prepare('SELECT * FROM servingTable WHERE id = :drinkId)');
$statement->bindParam(':drinkId' , $drinkId, PDO::PARAM_INT);
foreach($row as &$drink) {
    $drinkId  = $row->id;
    $statement->execute();//According to documentation

    if(($serve = $statement->fetchAll(PDO::FETCH_ASSOC))) 
    {
        $drink['servings'] = $serve;
    }
}

参见http://php.net/manual/en/pdo.prepared-statements.php (我也应该使用它,但它由一个一个查询系统管理,它需要改进)