Stupid Noobie question - turn table info 90deg? [solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Stupid Noobie question - turn table info 90deg? [solved]

Post by Noobie »

Hi - me again with, no doubt another stupid noobie question!

I'm trying to figure out how to turn the data output from a query onto it's side - but I just can't work out how to do it.

Currently the query looks like this:

Code: Select all

$query="select year, low, mid, high, peak from uk_rates ORDER BY year ASC";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
    
$i=0;
while ($i < $num) {

$year=mysql_result($result,$i,"year");
$low=mysql_result($result,$i,"low");
$mid=mysql_result($result,$i,"mid");
$high=mysql_result($result,$i,"high");
$peak=mysql_result($result,$i,"peak");
?>

<table>
  <thead>
    <tr>
      <th>Year</th>
      <th>Low Season<br />Jan/Feb<br />Oct/Nov</th>
      <th>Mid Season<br />Mar/Apr/May<br />June/Sep</th>
      <th>High Season<br />Jul/Aug</th>
      <th>Peak Season<br />Christmas<br />New Year</th>
    </tr>
 </thead>
<tbody>

<?
 
$i=0;
   while ($i < $num) {

$year=mysql_result($result,$i,"year");
$low=mysql_result($result,$i,"low");
$mid=mysql_result($result,$i,"mid");
$high=mysql_result($result,$i,"high");
$peak=mysql_result($result,$i,"peak");

?>

 <tr>
    <td><? echo $year; ?></td>
    <td><? echo $low; ?></td>
    <td><? echo $mid; ?></td>
    <td><? echo $high; ?></td>
    <td><? echo $peak; ?></td>
  </tr>

<?

   $i++;
}

 echo "</table>";

}
That produces an output that looks like this:

Image

But I'd like it to look like this:
Image
However I can't figure out how to do this as I can't just cycle through rows with this approach.

(once I get it working I plan on making it a little "cleaner" I just need to figure out the approach required!)

Any advice gratefully accepted!
Last edited by Noobie on Wed Aug 15, 2007 8:24 am, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Stupid Noobie question - turn table info 90deg?

Post by aceconcepts »

Noobie wrote:I'm trying to figure out how to turn the data output from a query onto it's side - but I just can't work out how to do it.
Do you mean you want the text to display vertically?
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Code: Select all

while ($i < $num) {

$year[]=mysql_result($result,$i,"year");
$low[]=mysql_result($result,$i,"low");
$mid[]=mysql_result($result,$i,"mid");
$high[]=mysql_result($result,$i,"high");
$peak[]=mysql_result($result,$i,"peak");



   $i++;
}
$num_of_years = count($year);
?>

<tr>
 <td>Low season ..... </td>
 <?php

   for($i=0;$i<$num_of_years;$i++) {
     echo '<td>'.$low[$i].'</td>'
   }
 ?>
</tr>

<tr>
 <td>Mid season ..... </td>
 <?php

   for($i=0;$i<$num_of_years;$i++) {
     echo '<td>'.$mid[$i].'</td>'
   }
 ?>
</tr>

<tr>
 <td>High season ..... </td>
 <?php

   for($i=0;$i<$num_of_years;$i++) {
     echo '<td>'.$high[$i].'</td>'
   }
 ?>
</tr>

<tr>
 <td>Peak season ..... </td>
 <?php

   for($i=0;$i<$num_of_years;$i++) {
     echo '<td>'.$peak[$i].'</td>'
   }
 ?>
</tr>
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Yes as per the second image with the season info down the left hand side and the year info across the top.

thanks
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

If I understand you correctly, this page should help with the text: http://www.ssi-developer.net/css/vertical-text.shtml
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

aceconcepts wrote:If I understand you correctly, this page should help with the text: http://www.ssi-developer.net/css/vertical-text.shtml
Sorry no - not literally vertical, I mean the table should be laid out with the current rows being columns and current columns being rows (seasons down the side and years across the top) as per the second image.

I'm just giving miro_igov's code a try - will report back, thanks.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Re: Stupid Noobie question - turn table info 90deg?

Post by CoderGoblin »

Untested...

Code: Select all

<?php
$query="select year, low, mid, high, peak from uk_rates ORDER BY year ASC";
$result = mysql_query($result);

// Simple test if result found (error in sql)... If not do something
if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}

// Simple test to check if you have any results
if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// Step through results building an array as we want it for output
$display_array=array('low'='','mid'=>'','high'=>'','peak'=>'');
$found_years=array();
while ($row = mysql_fetch_assoc($result)) {
   $found_years[]=$row['year'];
   $display_array['low'].="      <td rowspan=\"2\">{$row['low']}</td>\n";
   $display_array['mid'].="      <td rowspan=\"2\">{$row['mid']}</td>\n";
   $display_array['high'].="      <td rowspan=\"2\">{$row['high']}</td>\n";
   $display_array['peak'].="      <td rowspan=\"2\">{$row['peak']}</td>\n";
}

// We need columnised years for the table headers
$years="      <th>".implode('</th>\n  <th>',$found_years.'</td>';

$table_out=<<<EOTABLE
<table>
  <thead>
    <tr>
      <th>Season</th>
{$years}
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Low Season</td>
{$display_array['low']}
    </tr>
    <tr>
       <td>January/February/October/November</td>
    </tr>
    <tr>
      <td>Mid Season</td>
{$display_array['mid']}
    </tr>
    <tr>
       <td>March/April/May/June/September</td>
    </tr>
    <tr>
      <td>High Season</td>
{$display_array['high']}
    </tr>
    <tr>
       <td>July/August</td>
    </tr>
    <tr>
      <td>Peak Season</td>
{$display_array['peak']}
    </tr>
    <tr>
       <td>Easter/Christmas/New Year</td>
    </tr>
  </tbody>
</table>
EOTABLE;

echo $table_out; // could be moved outside this processing block as you have it as a variable
?>
Couple of notes about the code provided by you (personal preferences really)...

1) Always use full <?php, never simply <?. Reason - <? relies on php short tags setting which is not always on.
2) Have a processing section then output rather than mixing the two (store possible output in variables). Later on when you need things like header you can avoid a lot of headaches. Also php brackets are a nightmare to handle.

Hope this helps
CoderGoblin
Last edited by CoderGoblin on Wed Aug 15, 2007 8:39 am, edited 1 time in total.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thank you to everyone for your inputs - I've just implemented miro_igov's code and with the addition of column headers it's absolutely perfect - thank you very much indeed!

CoderGoblin - yes, usually I do have the full opening tag - not sure what happened here as it's in the code on my page, probably just my poor editing! Could you expand on what you mean by PHP brackets being a nightmare?

Thanks again!
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

Cheers
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Noobie wrote:CoderGoblin - yes, usually I do have the full opening tag - not sure what happened here as it's in the code on my page, probably just my poor editing! Could you expand on what you mean by PHP brackets being a nightmare?
OK As stated this is personal preference but which code above do you find more readable ? Also look at this example

Code: Select all

<?php
  if ($dummy_var == 1) {
?>
    <p>This is dummy var when it is one</p>
<?php
  } else {
?>
    <p>This is dummy var when not one</p>
<?php
  }
?>
and compare it with

Code: Select all

if ($dummy_var == 1) {
     $out='<p>This is dummy var when it is one</p>';
  } else {
     $out='<p>This is dummy var when not one</p>';
  }
  echo $out;
This is for a simple if statement. If you have this sort of processing where you are nesting if's and functions it gets a lot worse. It is easy to miss a bracket somewhere and then find it is a lot harder to debug, especially if moving blocks around.

The general idea is to keep processing logic away as much as possible from the output. It also helps if you need the identical text in several possible areas on the page to store them as variables.
Noobie
Forum Commoner
Posts: 85
Joined: Sun May 15, 2005 11:38 am

Post by Noobie »

Thanks for that - I'm trying to improve my skills so all information like this is very useful.
Post Reply