0

I'm looking to generate an HTML table like below.

|nick_name| bookmarked_sites  |
-------------------------------
| admin   | http://test1.com  |
|         | http://test2.com  |
|         | http://test3.com  |
-------------------------------
| John    | http://mysite.com |
-------------------------------

I'm query the database using $wpdb and it's building an array of information which looks like this:

$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 ) )

I started to build my first foreach to extract the user_id, then I had a nested foreach to extract the post_id for that user. But I'm realizing now that I can't easily build an HTML table off that concept.

I'm having a hard time conceptualizing the logic to put this all together.

Thanks!

3
  • 2
    Use the count of each array to know what rowspan to use. Commented Mar 18, 2013 at 15:45
  • What is your exact question? Commented Mar 18, 2013 at 15:47
  • Sorry I wasn't clear. My exact questions are: 1) Am I on the right path using the nested foreach, and 2) how would I build the HTML table above using the nested foreach? Commented Mar 18, 2013 at 15:48

2 Answers 2

1

You are on the right track. It would something like:

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>';
Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is working almost. It's correctly displaying the user name column with the rowheight, but it's only displaying 1 bookmark for all users and its repeating until done. I'm not sure why it is doing that.
what is your query to get the sites? And what is the rowheight when you check the source?
My mistake. I found a typo on my side. Been staring at this for too long today. It is working. Thank you!
1

This is possible way to make your table.. With the data you have from your query..

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.