Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为PHP查询启动读取_Php_Mysql_Join - Fatal编程技术网

为PHP查询启动读取

为PHP查询启动读取,php,mysql,join,Php,Mysql,Join,我是PHP新手,正在为一个项目开发自己的CMS。主要是给自己一个学习语言的项目。CMS正在工作,但我只是在跟踪其中的一些错误。就这样 我试图列出我在每个部分发表的所有文章。 它正确地做到了这一点,但在代码行后面的代码语句中有一个问题 echo '</br></br><br>'.ucfirst($row['section']).' Articles: '; //['section'] is the name 也许有人也可以试着帮我找出一种方法,去掉计数

我是PHP新手,正在为一个项目开发自己的CMS。主要是给自己一个学习语言的项目。CMS正在工作,但我只是在跟踪其中的一些错误。就这样

我试图列出我在每个部分发表的所有文章。 它正确地做到了这一点,但在代码行后面的代码语句中有一个问题

    echo '</br></br><br>'.ucfirst($row['section']).' Articles: '; //['section'] is the name
也许有人也可以试着帮我找出一种方法,去掉计数器的分隔符。我把其他方法弄得一团糟,但这是我能想出的解决问题的唯一逻辑,而且它似乎不是一种非常有效的编码方法。 问题是,当我只看一个部分,它有5篇文章时,它会显示部分名称,然后列出它的5篇文章,5次,而不是只做一次。所以我有一个计数器,如果一个部分被传入,它只会进入循环一次

这是我在这里的第一篇帖子,如果我发布的内容太多,需要进一步细分,那么就说出来吧,这样我觉得多多益善。如果需要,我也可以发布整页代码


谢谢

能否在初始SQL语句中添加where子句

例如,从secionId=$sectionId order by section asc的节中选择*


我想你点击一个部分,这样你的应用程序将带你进入一个页面,显示你选择的部分及其文章。

我认为你的问题在于,你在打印输出时第一次遍历了你的数据。这使得代码非常不灵活。资源效率高,但不灵活

以下是您可能要做的:

查询完节和文章后,创建一个数组来保存节。 在包含相关文章的每个部分中放置一个文章数组 使用foreach循环遍历节数组,然后遍历那些使用foreach的文章 要完成第一项和第二项,请执行以下操作:

$sections = array();
$result = $handle->query($query);
if($result)
 while ($storie = $result->fetch_assoc()) {
  $article = array();
  $article["stuff"] = $storie["stuff"];
  /* replace "stuff" with things you need from your query */
  $sections[$stories['section']][$stories['articleName'] = array('stuff'=>'some value');
 }

foreach($sections as $sectionName=>$articles)
 foreach($articles as $articleName=>$article)
  // print your stuff here
  // at this point you have all your data organised and you
  //   could've gone though it easily before the 2 foreaches
  //   to search for other stuff you needed.
$sections = array();
$result = $handle->query($query);
if($result)
 while ($storie = $result->fetch_assoc()) {
  $article = array();
  $article["stuff"] = $storie["stuff"];
  /* replace "stuff" with things you need from your query */
  $sections[$stories['section']][$stories['articleName'] = array('stuff'=>'some value');
 }

foreach($sections as $sectionName=>$articles)
 foreach($articles as $articleName=>$article)
  // print your stuff here
  // at this point you have all your data organised and you
  //   could've gone though it easily before the 2 foreaches
  //   to search for other stuff you needed.