- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 PHP 和 MySQL 来显示正确的 JSON 时遇到问题。尝试了很多我能找到的选项,但都无济于事。到目前为止,我设法获得了正确的 JSON 结构,但内容不断循环使用以前的数据。如何解决内容双循环问题?
使用 2 个表 - recipecategory
和 recipelibrary
。
我的数据库图像:
PHP 代码:
<?php
require("db.php");
//fetch category table rows
$sqlCat = "SELECT categoryName, uId FROM recipecategory";
$resultCat = mysqli_query($conn, $sqlCat) or die ("Error in Selecting" .mysqli_error($conn));
//create an array
$recipeArray = array();
$recipeList = array();
//Loop category table
while($rowCat = mysqli_fetch_assoc($resultCat)){
//Add row to json - $recipeArray
$recipeArray["title"] = $rowCat["categoryName"];
//Store category name to $catName
$catName = $rowCat["categoryName"];
//Sql statement to fetch contents for every categories
$sql = "SELECT A.*, B.categoryName FROM recipelibrary AS A INNER JOIN recipecategory AS B ON A.categorId = B.uId WHERE B.categoryName ='". $catName."'";
$result = mysqli_query($conn, $sql) or die ("Error in Selecting" .mysqli_error($conn));
//Loop contents row and add to json - $recipeArray
while($row = mysqli_fetch_assoc($result)){
$recipeArray["Section"][] = $row;
}
//Add $recipeArray to $recipeList
$recipeList["data"][] = $recipeArray;
}
//display Json
echo json_encode($recipeList);
?>
JSON 结果:(由于 Stack Overflow 中的字符限制,仅是我的 JSON 结果的一部分):
"data": [
{
"title": "Western",
"Section": [
{
"id": "1",
"uid": "7UtP7q",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_1",
"recipeDesc": "Suspendisse id metus et elit rhoncus molestie. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut egestas, neque a sollicitudin suscipit, felis orci molestie orci, quis consectetur urna magna nec mi. Fusce a commodo augue. Integer maximus porttitor elit. Morbi molestie libero a nibh accumsan tincidunt.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "2",
"uid": "3nV9jp",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_2",
"recipeDesc": "Praesent consectetur ex vel est pharetra, sollicitudin convallis nulla dignissim. Duis facilisis accumsan erat id suscipit. Nam sodales tortor nec eros vulputate tristique consectetur sit amet elit. In et ultricies turpis.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "4",
"uid": "EzfzGJ",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_4",
"recipeDesc": "Cras id finibus ante. Sed aliquet vel tellus sit amet luctus. Donec ante augue, suscipit eget blandit quis, accumsan ac est. Fusce eu lorem commodo, maximus nisl non, ornare odio.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "7",
"uid": "0ex4YA",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_7",
"recipeDesc": "Morbi vestibulum tellus sed commodo vehicula. Phasellus elementum, sem ut sagittis fringilla, dolor ex facilisis massa, eget accumsan felis ligula sed dolor. Sed interdum lectus nec nisi ullamcorper, et congue tellus faucibus.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
}
]
},
{
"title": "Chinese",
"Section": [
{
"id": "1",
"uid": "7UtP7q",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_1",
"recipeDesc": "Suspendisse id metus et elit rhoncus molestie. Interdum et malesuada fames ac ante ipsum primis in faucibus. Ut egestas, neque a sollicitudin suscipit, felis orci molestie orci, quis consectetur urna magna nec mi. Fusce a commodo augue. Integer maximus porttitor elit. Morbi molestie libero a nibh accumsan tincidunt.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "2",
"uid": "3nV9jp",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_2",
"recipeDesc": "Praesent consectetur ex vel est pharetra, sollicitudin convallis nulla dignissim. Duis facilisis accumsan erat id suscipit. Nam sodales tortor nec eros vulputate tristique consectetur sit amet elit. In et ultricies turpis.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "4",
"uid": "EzfzGJ",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_4",
"recipeDesc": "Cras id finibus ante. Sed aliquet vel tellus sit amet luctus. Donec ante augue, suscipit eget blandit quis, accumsan ac est. Fusce eu lorem commodo, maximus nisl non, ornare odio.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "7",
"uid": "0ex4YA",
"categorId": "sy0Sbt",
"recipeName": "recipeTitle_7",
"recipeDesc": "Morbi vestibulum tellus sed commodo vehicula. Phasellus elementum, sem ut sagittis fringilla, dolor ex facilisis massa, eget accumsan felis ligula sed dolor. Sed interdum lectus nec nisi ullamcorper, et congue tellus faucibus.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Western"
},
{
"id": "3",
"uid": "nEdCe3",
"categorId": "wNXTav",
"recipeName": "recipeTitle_3",
"recipeDesc": "Etiam id elementum sapien. Cras bibendum dolor felis, nec egestas diam posuere id. Vivamus feugiat urna et libero cursus fermentum.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Chinese"
},
{
"id": "5",
"uid": "8NMelb",
"categorId": "wNXTav",
"recipeName": "recipeTitle_5",
"recipeDesc": "Curabitur est massa, fermentum vitae orci a, pulvinar hendrerit neque. Curabitur finibus ante nec dignissim tempor. Proin eget purus augue. Pellentesque quis eros id turpis tristique condimentum a gravida quam.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Chinese"
},
{
"id": "10",
"uid": "vOOL8r",
"categorId": "wNXTav",
"recipeName": "recipeTitle_10",
"recipeDesc": "Pellentesque blandit facilisis pharetra. Suspendisse libero lectus, malesuada non enim in, convallis cursus erat. Nunc ac congue odio. Integer nibh nibh, luctus et ornare eget, aliquam sit amet velit.",
"recipeIngredients": "150g (5oz) bulgur wheat|3 tbsp olive oil|1 lemon, zested and juiced|1 small shallot, finely chopped|1 tsp ground cumin|100g (3 1/2oz) kale, very finely chopped|1 x 230g pack sweet vine ripened tomatoes, chopped|1/2 cucumber, diced|handful chopped mint|handful chopped flat-leaf parsley|100g (3 1/2oz) raisins|1 x 225g pack lighter halloumi, sliced into 12",
"recipeDirection": "Bring a pan of water to the boil, add the bulgur wheat, cover, then cook for 15 minutes. Drain excess liquid and cool.|In a bowl, whisk 2 tbsp olive oil, the lemon juice and zest, the shallot and the cumin. Stir into the bulgur wheat, reserving some to drizzle.|Stir through the kale, tomatoes, cucumber, herbs and raisins, mix well, then season with black pepper.|Heat a frying pan over a medium heat and add the remaining oil. Fry the halloumi for 1-2 minutes on each side, until crisp and golden. To serve, top the tabbouleh with the halloumi and drizzle with the remaining dressing.",
"userId": null,
"categoryName": "Chinese"
}
]
},
最佳答案
我终于解决了。我的数据库表没有问题,问题出在PHP上。这有两个问题。首先是数组的变量,其次是在 while 循环中如何将数据存储到数组中,
第一个问题
我这样声明了我的数组,这没有任何问题,但是如果我使用函数或方法来使用这个数组,我需要将其设为全局。这样,我就可以访问任何 PHP 中的变量。
$recipeArray = array();
$recipeList = array();
创建全局变量的正确方法
引用:w3schools
$recipeArray = array();
//call for function
categoryContent()
function categoryContent() {
$GLOBALS['recipeArray'][] = $rowContent;
}
第二个问题
第一个问题解决后,它仍然重复前一个类别的数据,因此简单的解决方案是使用 unset()
函数。
引用:w3resource
while($rowCat = mysqli_fetch_array($resultCat)){
$GLOBALS['recipeArray']["title"] = $rowCat['categoryName'];
categoryContent($rowCat['categoryName']);
$recipeList["data"][]=$recipeArray;
}
通过在 while 循环内 $GLOBALS['recipeArray']["title"] = $rowCat['categoryName'] 之前添加
,我得到了我正在寻找的最终 JSON 结果。 unset($GLOBALS['recipeArray']);
;
while($rowCat = mysqli_fetch_array($resultCat)){
unset($GLOBALS['recipeArray']);
$GLOBALS['recipeArray']["title"] = $rowCat['categoryName'];
categoryContent($rowCat['categoryName']);
$recipeList["data"][]=$recipeArray;
}
关于php - 在 PHP 中使用 JSON 显示类别和内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251512/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!