Jump to content

How can you split a row returned from a MySQL query?


Conjurer

Recommended Posts

Currently, to generate a table row from my query I am using:
[code]foreach($row as $val)
          {
            echo "<td>$val</td>";
          }
        echo "</tr>\n";[/code]

That works great as long as I don't have too many columns in the query, but once it gets larger then it causes the output to scroll horizontally and messes up my page layout.

I am wondering how I could change this - basically if I know I am querying on 15 columns but can only fit about seven columns how could I fix this to put a "</tr><tr><td>Continued</td>" in between value 7 and value 8? Do I need to do an i counter and increment it or what? If so what am I counting?

Here is the more complete code I use now:
[code]while ($row = mysqli_fetch_assoc($result))
      {
        if (!$heading) //only do if header row hasn't been output yet
        {
          $heading = TRUE; //so we only do this once
            echo "<table>\n<tr>\n";
            foreach ($row as $key =>$val)
              {
              echo "<th>$key</th>";
              }
              echo "</tr>\n";
        }   
 
   
        if ($style=="odd") //set rows as even and odd
            $style = "even";
          else
            $style = "odd";
   
        echo "<tr class=\"$style\">";
   
          foreach($row as $val)
          {
            echo "<td>$val</td>";
          }
        echo "</tr>\n";
       

      }//close while
    echo "</table>\n";
} [/code]

Thoughts or suggestions?

Thanks!
Link to comment
Share on other sites

[code]
$count=0;
while($count<7)
{
      echo "<td>{$row[$count]}</td>";
}
echo "</tr><tr>";
while($count<14)
{
        echo "<td>{$row[$count]}</td>";
}
[/code]
this will output:
|------|------|------|------|------|------|------|------|
|        |        |        |        |        |        |        |_____|
|------|------|------|------|------|------|------|
|        |        |        |        |        |        |        |
--------------------------------------------------

with the values in the cells
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.