Jump to content

New HTML table within a while loop foreach $row['sectionid'] ?


RJP1

Recommended Posts

Hi guys,

 

This is hard to explain... I'll try. I have a MySQL table for articles, each article is in a section (id). I can do a while loop to create a HTML table of title, section etc. How do I split the code so that I create a new HTML table per $row['sectionid']?

 

Any ideas would be great! I can group by section and then title in the table, but i'd rather not have the section id/or name repeated in a column for every article in a particular section if you get me...? Simplified code below.

 

Cheers, RJP1

 

public function listArticle() {
    require_once('../includes/DbConnector.php');
    $connector = new DbConnector();
    $result = $connector->query('SELECT * FROM article ORDER BY section, title');
    echo '<h2>Articles:</h2>';
    if ( $result !== false && mysql_num_rows($result) > 0 ) {
    echo '<table id="table">
         <thead>
        	<tr>
        	  <th scope="col">Title</th>
        	  <th scope="col">Section</th>
            <th scope="col" style="text-align:center;">Edit?</th>
            <th scope="col" style="text-align:center;">Delete?</th>
          </tr>
        </thead>
    <tbody>';
    while ($row = $connector->fetchArray($result)){
         echo '<tr><td>'.$row['title'].'</td>';
         echo '<td>'.$row['section'].'</td>';
  }
  }
  echo '</tbody>
</table>';
return;
  }

Link to comment
Share on other sites

I would suggest using a single table with intermediary headers for each section. It will make this much easier. Or, put an empty row between sections.

 

Here's a quick rewrite (not tested)

public function listArticle() {
    require_once('../includes/DbConnector.php');
    $connector = new DbConnector();
    $result = $connector->query('SELECT * FROM article ORDER BY section, title');
    echo '<h2>Articles:</h2>';
    if ($result === false)
    {
        $output = "Unable to query results.";
    }
    else if(mysql_num_rows($result) < 1)
    {
        $output = "There were no results.";
    }
    else
    {
        $header  = "<tr>\n";
        $header .= "<th scope=\"col\">Section</th>\n";
        $header .= "<th scope=\"col\" style=\"text-align:center;\">Edit?</th>\n";
        $header .= "<th scope=\"col\" style=\"text-align:center;\">Delete?</th>\n";
        $header .= "</tr>\n";
    
        $output  = "<table id=\"table\">\n";
        $output .= "<tbody>\n";
    
        $current_title = '';
        while ($row = $connector->fetchArray($result))
        {
            if($current_title != $row['title'])
            {
                $current_title = $row['title'];
                $output .= "<tr><td colspan=\"3\"> </td></tr>\n";
                $output .= "<tr><td colspan=\"3\"><b>{$current_title}</b></td></tr>\n";
                $output .= $header;
            }
            $output .= "<tr>\n";
            $output .= "<td>{$row['section']}</td>\n";
            $output .= "<td>[Edit Link]</td>\n";
            $output .= "<td>[Delete Link]</td>\n";
            $output .= "</tr>\n";
        }
        $output .= "</tbody>\n";
        $output .= "</table>\n";
    }
    
    echo $output;
    return;
}

Link to comment
Share on other sites

do something like this, it will echo a new section name as a row/header if it differs from the last article.

and it will echo a blank row to separate them. you can play around with the HTML to style it how you like.

 


public function listArticle() {
    require_once('../includes/DbConnector.php');
    $connector = new DbConnector();
    $result = $connector->query('SELECT * FROM article ORDER BY section, title');
    echo '<h2>Articles:</h2>';
    if ( $result !== false && mysql_num_rows($result) > 0 ) {
    echo '<table id="table">
         <thead>
        



<tr>
        



  <th scope="col">Title</th>
        



  <th scope="col">Section</th>
            <th scope="col" style="text-align:center;">Edit?</th>
            <th scope="col" style="text-align:center;">Delete?</th>
          </tr>
        </thead>
    <tbody>';

$lastSection = '';

    while ($row = $connector->fetchArray($result)){
         if ($lastSection != $row['section'])
{
echo "<tr><td colspan='4'> </td></tr>
<tr><td colspan ='4'>{$row['section']}</td></tr>";
}

echo '<tr><td>'.$row['title'].'</td>';
         echo '<td>'.$row['section'].'</td>';

  }
  }
  echo '</tbody>
</table>';
return;
  }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.