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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.