Jump to content

Is it possible to allign each row (sql query) different


Klein_Kipje

Recommended Posts

I googled myself lame to find out, but no luck.

 

Is it possible to allign every row in a sql result table different. And i mean, not the text but the whole row.

 

01 | ** | ** | left
                  02 | ** | ** | center
                                    03 | ** | ** | right
                  04 | ** | ** | center
05 | ** | ** | left

 

depending of the value of one of the cell in that row.

 

I'm able to assign a different class to <TD> to change color of the cell (or whatever) depending on that value.

 

Is it possible at all to allign rows different from each other ?

Link to comment
Share on other sites

"an sql result table"?  Do you perhaps mean an html table?  Assuming that is what you meant to say, I would say 'No'.  An html table doesn't have that facility (afaik).  So - how to do this?  Well - how important are 'column alignments once you decide where a specific row should being (on the x axis)?  If no so important you could simply display each row in different divs which have specific left margins set to do what you want.  If important, then you might do it with the same divs scheme but with individual divs defined for each 'column' that are then output inside of each 'row' div.

 

Psuedo-like-this:

<div id='left-row'>

    data

</div>

<div id='right_row'>

   data

</div>

 

Or:

<div id='left_row'>

    <div id='col1'>

       col1 data

    </div>

    <div id='col2'>

       col2 data

    </div>

    <div id='col3'>

        col3 data

    </div>

</div>

and so on.

Link to comment
Share on other sites

I mean a sql result table

 

its to create a timetable

 

now i have every event on the same left allign like a normal sql table

 

its has 3 locations and my idea was to allign every location different to make it more easy viewable

 

This is the code so far, the <td class = "'.$NewClass.'"> makes the table cell a different color depending on the location

$result = mysql_query($sql) or die(mysql_error());


$begin = strtotime('13:00');

echo'<table>';


while($row = mysql_fetch_array($result))

{

$PODIUM = $row['PODIUM'];

if ($row['PODIUM'] == 1) {
  $NewClass = "JA" ;
} elseif ($row['PODIUM'] == 2) {
  $NewClass = "NEE";
} elseif ($row['PODIUM'] == 3) {
  $NewClass = "MISSCHIEN";
}


$duur = $row['DUUR'];

$end = strtotime("+ $duur minutes", $begin);

echo'<tr>
        <td class = "'.$NewClass.'">'.$row['POSITIE'].'</td>
		<td class = "'.$NewClass.'">'.$row['NAAMBAND'].'</td>
		<td class = "'.$NewClass.'">';
			    if($PODIUM == 1){
				echo "<div class='item'><label>Hoofdpodium</label></div>";
			    };
				if($PODIUM == 2){
				echo "<div class='item'><label>DJ Toren</label></div>";
			    }; 
			    if($PODIUM == 3){
				echo "<div class='item'><label>Talentstage</label></div>";
			    }; 
   echo'<td class = "'.$NewClass.'">'.$row['DUUR'].'</td>
        <td class = "'.$NewClass.'">'.date('H:i', $begin).'</td>
        <td class = "'.$NewClass.'">'.date('H:i', $end).'</td> ';

echo'</tr>';
$begin = $end;
}
echo'</table>';



?>
Edited by Klein_Kipje
Link to comment
Share on other sites

Ok - that is an html table.  Sql results are not in a table (they are in a 'resource') until you output them as part the data in an html table.  (Semantics are VERY important in the IT realm.)

 

So - now you can consider my previous suggestions - or not.

 

BTW - isn't this display idea going to make viewing a little troubling?  Personally if I'm viewing a set of data that is homogeneous I want to see it in a clear format, not have to make my eyes wander from column to column to find like items which this format will cause.

Link to comment
Share on other sites

If you allocate 3x as many columns to your HTML table then you can arrange them by outputting empty cells. In this example there are 3 rooms, and the room number sets the left, centre or right position

mysql> SELECT * FROM timetable;
+--------------+-----------+------+----------+----------+
| timetable_id | subject   | room | timefrom | timeto   |
+--------------+-----------+------+----------+----------+
|            1 | English   |    1 | 09:00:00 | 10:00:00 |
|            2 | Maths     |    2 | 10:00:00 | 11:00:00 |
|            3 | Biology   |    3 | 11:00:00 | 12:00:00 |
|            4 | Geography |    2 | 13:00:00 | 14:00:00 |
|            5 | History   |    3 | 14:00:00 | 15:00:00 |
+--------------+-----------+------+----------+----------+

the sample code

$mysqli = new mysqli(HOST,USERNAME,PASSWORD,'test');
$sql = "SELECT subject
        , room
        , timefrom
        , timeto
        FROM timetable
        ORDER BY timefrom";
$res = $mysqli->query($sql);
$ttdata = '';
while (list($sub,$room,$from,$to) = $res->fetch_row()) {
    $from = date('g:i', strtotime($from));
    $to = date('g:i', strtotime($to));
    $ttdata .= '<tr>';
    switch ($room) {
        case 1:
            $ttdata .= "<td>$sub</td><td>$from</td><td>$to</td><td colspan='6' class='empty'></td></tr>";
            break;
        case 2:
            $ttdata .= "<td colspan='3' class='empty'></td><td>$sub</td><td>$from</td><td>$to</td><td colspan='3' class='empty'></td></tr>";
            break;
        case 3:
            $ttdata .= "<td colspan='6' class='empty'></td><td>$sub</td><td>$from</td><td>$to</td></tr>";
            break;
    }
}

?>
<html>
<head>
<title>Sample</title>
<style type="text/css">
table {border-collapse: collapse;}
th {background-color: blue; color: white;}
td {padding: 2px 4px;}
td.empty {background-color: #eee;}
</style>
</head>
<body>

    <table border="1" cellspacing="3">
    <tr><th colspan='3'>Room 1</th><th colspan='3'>Room 2</th><th colspan='3'>Room 3</th></tr>
    <?=$ttdata?>
    </table>

</body>
</html>

Output

 

post-3105-0-06196700-1418831931_thumb.png

Link to comment
Share on other sites

The relevant bit is

 


switch ($room) {
case 1:  //LEFT
$ttdata .= "<td>$sub</td><td>$from</td><td>$to</td><td colspan='6' class='empty'></td></tr>";
break;
case 2:  //CENTRE
$ttdata .= "<td colspan='3' class='empty'></td><td>$sub</td><td>$from</td><td>$to</td><td colspan='3' class='empty'></td></tr>";
break;
case 3:  //RIGHT
$ttdata .= "<td colspan='6' class='empty'></td><td>$sub</td><td>$from</td><td>$to</td></tr>";
break;

}

Edited by Barand
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.