从不同的表中识别JSON对象

时间:2016-11-21 15:03:55

标签: php mysql json xcode

我正在尝试使用xcode中的php脚本从三个不同的表(MySQL)中获取数据。

我知道如何从一个表中获取元素但我不知道如何扩展此方法以便能够使用相同的php脚本和xcode中的NSURLSession从其他两个表中获取数据。

我的一个表(工作)的PHP脚本:

setValue()

现在问题是我想对另外两个表做同样的事情。我试图将我的获取方法合并到一个单独的php文件(比如getElementFunction.php)中并在主文件中调用此方法:

getElementFunction.php

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno(). ') ' . mysqli_connect_error());
}

$sql = "SELECT * FROM table1";
$test = $mysqli->query($sql);
$Nrows = $test->num_rows;
$resultArray = array();

if ($result = mysqli_query($mysqli, $sql)) {
    while ($row = $result->fetch_assoc()) {
        $resultArray[] = $row;
    }
    echo json_encode($resultArray);
}
else {
    echo 'oups.';
}

main_file.php:

<?php
function getElements()
{
    $test = $mysqli->query($sql);
    $Nrows = $test->num_rows;
    $resultArray = array();

    if ($result = mysqli_query($mysqli, $sql)) {
        while ($row = $result->fetch_assoc()) {
            $resultArray[] = $row;
        }
        echo json_encode($resultArray);
    }
    else {
        echo 'oups.';
    }
}
?> 

但我不知道它是否正确以及如何“标记”将返回的不同json对象以便在我的xcode脚本中使用。我想在一个只有一个URL的循环中运行提取过程。

以防万一,我的Objective-C脚本,我需要一种方法来指定从哪个表返回的json对象...

使用NSURLSession的XCODE脚本:

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno(). ') ' . mysqli_connect_error());
}

include 'getElementFunction.php';

$sql = "SELECT * FROM table1";
getElements() // --> how can I flag the json object as being returned from table 1

$sql = "SELECT * FROM table2";
getElements() // --> how can I flag the json object as being returned from table 2

$sql = "SELECT * FROM table3";
getElements() // --> how can I flag the json object as being returned from table 3

如果有人可以帮助我......

谢谢!

1 个答案:

答案 0 :(得分:1)

不确定要清楚明白..但你可以在getElements()中尝试foreach。

我的意思是:

<?php
    $getid     = $_GET['estimate_id'];  // The current estimate number
    $getLineid = $_GET['line_id'];      // The current line item on the estimate
?>

<label for="finish_id">Finishing</label>
<div class="checkbox_group">
    <?php
        if ($select = $db -> prepare("SELECT f.finish_id, f.finish_name, u.estimate_id, u.line_id FROM finishes AS f INNER JOIN finishes_used AS u ON u.finish_id = f.finish_id WHERE u.line_id = ? ORDER BY f.finish_name ASC"))
        {
            $select -> bind_param('s', $getLineID);
            $select -> execute();
            $select -> bind_result($finish_id, $finish_name, $used_estimate_id, $used_line_id);
            while ($select -> fetch())
            {
                echo '<div class="checkbox">';
                echo '<input type="checkbox" name="finish_id[]" id="edit_'.$finish_name.'" value="'.$finish_id.'" ';
                echo '/><label for="edit_'.$finish_name.'">'.$finish_name.'</label>';
                echo '</div>';  
            }
            $select -> close();
        }
    ?>
</div>

这一部分:

function getElements(array $allSQL)
{
    //Final array to json_encode
    $finalResultsArray = array();

    foreach($allSQL as $tableName => $sqlStatement) {
            $arrayResults = array();

            $test = $mysqli->query($sqlStatement);
            $Nrows = $test->num_rows;

            if ($result = mysqli_query($mysqli, $sqlStatement)) {
                while ($row = $result->fetch_assoc()) {
                    $arrayResults[] = $row;
                }
                //echo json_encode($arrayResults);
                $finalResultsArray[$tableName] = $arrayResults;
            }
            else {
                echo 'oups.';
            }

    }

    echo json_encode($finalResultsArray);
}

成为这个:

$sql = "SELECT * FROM table1";
getElements() // --> how can I flag the json object as being returned from table 1

$sql = "SELECT * FROM table2";
getElements() // --> how can I flag the json object as being returned from table 2

$sql = "SELECT * FROM table3";

getElements() // --> how can I flag the json object as being returned from table 3