Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Formatting Columns

Codes here !

Moderators: macek, egami, gesf

Formatting Columns

Postby daveygz » Fri Nov 29, 2013 9:52 am

I haven't been working with PHP/MYSQL very long and I have a sorting issue. My code for displaying the data from my database is:

<?php
$queryFloor1 = "SELECT * FROM $table4 WHERE floor='Floor 1' ORDER BY size ASC";
$result = mysql_query($queryFloor1) or die(mysql_error());
$col_count = 0; // set up loop counter
echo '<table><tr>'; // start table and first tr
while ($row=mysql_fetch_array($result)) {
if ($col_count == 4) {
echo '</tr><tr>';
$col_count = 0;
}echo '<td style="width:200px;" align="left">'. "{$row['size']}".'&nbsp;-&nbsp;$'. " {$row['amount']}".'</td>';
$col_count++;
}
echo '</tr></table>';
?>


Get get my results but they are displayed across then down:
1 2 3 4
5 6 7 8

Is there a way to display them Down then across?
1 3 5 7
2 4 6 8
daveygz
New php-forum User
New php-forum User
 
Posts: 6
Joined: Mon May 28, 2012 6:39 am

Re: Formatting Columns

Postby nanirao89 » Mon Dec 02, 2013 5:32 am

Hi daveygz,
We can print the numbers like the way you need. But, we need to add some code & logic. Here my suggestion of code.

Code:

<?php
$queryFloor1 = "SELECT * FROM $table4 WHERE floor='Floor 1' ORDER BY size ASC";
$result = mysql_query($queryFloor1) or die(mysql_error());
$col_count = 0; // set up loop counter
$row1='';
$row2='';
while ($row=mysql_fetch_array($result)) {
if($num&1) {
$row1.='<td style="width:200px;" align="left">'. "{$row['size']}".'&nbsp;-&nbsp;$'. " {$row['amount']}".'</td>';
} else {
$row2.='<td style="width:200px;" align="left">'. "{$row['size']}".'&nbsp;-&nbsp;$'. " {$row['amount']}".'</td>';
}
$col_count++;
}
echo '<table><tr>'; // start table and first tr
echo $row1.'</tr><tr>';
echo $row2;
echo '</tr></table>';
?>
I have just stored the elements to variable and finally printed it. We can still continue with your logic, but we need to use div/span instead of table to display the result .

Have a nice day !
nanirao89
New php-forum User
New php-forum User
 
Posts: 4
Joined: Mon Nov 18, 2013 10:41 pm


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 3 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.