【问题标题】:json_encode() array in while loop for mySQL for calendar用于日历的mySQL的while循环中的json_encode()数组
【发布时间】:2012-06-29 11:42:45
【问题描述】:

对于 JSON,我不知道自己在做什么。所以这可能是一个愚蠢的问题,我可能试图以一种奇怪的方式来做。任何帮助都会很棒。

我有这个 Jquery 日历,我想把它放在我的网站上。它允许 json_encode 日期。这是他们给出的例子。

    $year = date('Y');
$month = date('m');

echo json_encode(array(

    array(
        'id' => 111,
        'title' => "Event1",
        'start' => "$year-$month-10",
        'url' => "http://yahoo.com/"
    ),

    array(
        'id' => 222,
        'title' => "Event2",
        'start' => "$year-$month-20",
        'end' => "$year-$month-22",
        'url' => "http://yahoo.com/"
    )

));

我想使用现有的 mySQL 数据库来填充日历。这是我到目前为止所拥有的。它不起作用,我认为我对此并不聪明。

$dataSQL ="select *
            FROM events
        ";

$dataResult = mysqli_query($dataBase, $dataSQL);

$encode = array();

$p=0;
    while($allRow = mysqli_fetch_array($dataResult))
        {
            $new = array(
                        'id' => "$allRow['id']",
                        'title' => "$allRow['title']",
                        'start' => "$allRow['date']",
                        'url' => "$allRow['url']"
                    );
            array_splice($encode, ($p), 0, $new);

            $p++;
        }


echo json_encode($encode);

提前致谢!

【问题讨论】:

  • 不应该 $dataSQL 变量首先作为 mysqli_query() 的参数吗?
  • 不适用于这样的程序风格

标签: php mysql arrays json loops


【解决方案1】:

如果您只是在数组结构方面遇到问题,那您就有点复杂了。你可以像这样构建一个数组:

$encode = array();

while($allRow = mysqli_fetch_array($dataResult))
       {
            $new = array(
                        'id' => $allRow['id'],
                        'title' => $allRow['title'],
                        'start' => $allRow['date'],
                        'url' => $allRow['url']
                    );
            $encode[] = $new;

        }

echo json_encode($encode);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 1970-01-01
    • 2019-08-13
    • 2011-10-30
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多