Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/68.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嵌套foreach循环_Php_Mysql_Wordpress_Foreach - Fatal编程技术网

用于显示表的PHP嵌套foreach循环

用于显示表的PHP嵌套foreach循环,php,mysql,wordpress,foreach,Php,Mysql,Wordpress,Foreach,我希望生成一个如下所示的HTML表 |nick_name| bookmarked_sites | ------------------------------- | admin | http://test1.com | | | http://test2.com | | | http://test3.com | ------------------------------- | John | http://mysite.com | --------

我希望生成一个如下所示的HTML表

|nick_name| bookmarked_sites  |
-------------------------------
| admin   | http://test1.com  |
|         | http://test2.com  |
|         | http://test3.com  |
-------------------------------
| John    | http://mysite.com |
-------------------------------
我正在使用$wpdb查询数据库,它正在构建一个信息数组,如下所示:

$userquery = $wpdb->get_results("SELECT * FROM bookmarks");
print_r($userquery);

Array ( 
[0] => stdClass Object ( 
    [id] => 1 [user_id] => 1 [post_id] => 1654,1532,1672,1610,1676 ) 
[1] => stdClass Object ( 
    [id] => 3 [user_id] => 6 [post_id] => 1680,1654 ) )
我开始构建我的第一个
foreach
来提取
user\u id
,然后我有一个嵌套的
foreach
来提取该用户的
post\u id
。但我现在意识到,我不能轻易地根据这个概念构建一个HTML表

我很难概念化把这些放在一起的逻辑


谢谢

你走在正确的轨道上。大概是这样的:

echo '<table>';
foreach ($userquery as $user) {
  //load sites into $loadedsites

  $rowheight = count($loadedsites);
  $c = 0;

  //if there is a user without any sites, the rowheight would be 0.
  //You need to deceide what to do than, because now it wouldnt show the user at all.

  foreach($loadedsites as $site) {

    if ($c==0)
      echo '<tr><td rowspan="'.$rowheight.'">'.$user->user_id.'</td><td>'.$site->url.'</td></tr>';
    else
      echo '<tr><td>'.$site->url.'</td></tr>';

    $c++;
  }
}
echo '</table>';
echo';
foreach($userquery作为$user){
//将站点加载到$loadedsites中
$rowheight=计数($loadedsites);
$c=0;
//如果用户没有任何站点,则行高将为0。
//你需要做的是欺骗,因为现在它根本不会向用户显示。
foreach($loadedsites作为$site){
如果($c==0)
回显'.$user->user_id'.$site->url'.';
其他的
回显“.$site->url.”;
$c++;
}
}
回声';

这是制作桌子的可能方法。。 使用查询中的数据

<table>
  <thead>
    <tr>
      <th>user_id<th>
      <th>post_id<th>
    </tr>
  </thead>
  <tbody>
    <?php foreach($userquery as $uq): ?>
    <tr>
      <td><?php echo $uq['user_id']; ?></td>
      <td>
        <?php
           $posts = explode(',', $uq['post_id']);
           if(count($posts)>0):
           ?>
           <table>
             <?php foreach($posts as $p): ?>
             <tr><td><?php echo $p; ?> </td></tr>
             <?php endforeach; ?>
           </table>
     <?php else: ?>
           No posts...
     <?php endif; ?>
      </td>
    </tr>      
    <?php endforeach; ?>
  </tbody>
</table>

用户id
邮政编码
没有帖子。。。

使用每个数组的
计数
来了解要使用的
行span
。您的确切问题是什么?对不起,我不清楚。我的确切问题是:1)我使用嵌套foreach的路径是否正确,2)我将如何使用嵌套foreach构建上面的HTML表?您的答案几乎有效。它正确地显示了具有行高的用户名列,但它只为所有用户显示一个书签,并在完成之前重复该书签。我不知道它为什么这样做。你对网站的查询是什么?当你检查来源时,行高是多少?我错了。我发现我这边有个打字错误。今天我盯着这个看了太久了。它正在工作。非常感谢。