Re: putting a list of data into 3 columns?

From: Date: Mon, 09 Apr 2001 22:57:22 +0000
Subject: Re: putting a list of data into 3 columns?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-47896@lists.php.net to get a copy of this message
okay. your solution is not going to scale well, is it. if you have more than nine items, you have to rewrite your code. here is what I did: I created this table in mysql from the command line as follows: mysql> create table tabletest ( -> id int unsigned primary key not null auto_increment); Query OK, 0 rows affected (0.47 sec) mysql> desc tabletest; +-------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | | PRI | 0 | auto_increment | +-------+------------------+------+-----+---------+----------------+ 1 row in set (0.73 sec) mysql> insert into tabletest values (NULL),(NULL), (NULL), (NULL), (NULL), (NULL), (NULL),(NULL); Query OK, 8 rows affected (0.48 sec) Records: 8 Duplicates: 0 Warnings: 0 mysql> select * from tabletest; +----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | | 7 | | 8 | +----+ 8 rows in set (0.29 sec) Then, I copy and pasted the script I wrote earlier to the list, made some changes to match the database and got this: <? $link=mysql_connect(); mysql_select_db('tester',$link); $query = 'SELECT* FROM tabletest'; $result = mysql_query($query,$link); print <<<EOF <html> <head> </head> <body> <table width="400" border=1> EOF; while(list($id)=mysql_fetch_row($result)) { $items[]=$id; } reset($items); For($i = 0; $i< (count($items)/3); $i +=1){ printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>",$items[$i],$items[$i+3], $items[$i+6]); } ?> </table></body></html> I put that into tabletest.php view results at www.dingos.net/test/tabletest.php this script will scale to ANY size listing. Here, the 3 stands for number of columns. it can easily be cleaned up so that the number of columns is dynamic, and I am sure there is a cleaner way of doing multiple columns, without putting all the database stuff into an array, BUT, this shows you how the code works, and what it looks like On 4/9/01 3:06 PM, "DRN" <donaldrnoble@NOSPAM.yahoo.com> wrote: > > Lindsay Adams <lindsay@dingos.net> wrote in message > news:B6F54222.46F6%lindsay@dingos.net... > | This is exactly what I sent you. > | You have to realize that you can't print down one column, and then > start a > | new one. > | You have to print across, left to right before you go down. > | You have to modify the print statement to put it into a table, but > that is > | easy: > | > > I have been playing about with this problem and I have come up with > the following code. I know it is not right, because it isn't working > :) but I think it is nearly what I want to do. I only want to have 3 > cells in the table, with a third of the data in each. > > Cheers for your help, Donald > > <? $db = mysql_connect("localhost", "user","pass"); > mysql_select_db("database",$db); > > $query = "SELECT * FROM TYPES"; > $result = mysql_query($query); > $num_rows = mysql_num_rows($result); > > if ($result){ > $i=0; > echo "<table border=1>\n<tr>\n<td width='30%'>"; > $r = mysql_fetch_array($result); > $type_name = $r["type_name"]; > > while ( $i < ($num_rows/3) ){ > echo "$type_name[$i] <br>\n"; $i++; > } > echo "</td><td width='30%'>\n"; > while ($i <= (2*$num_rows/3) ){ > echo "$type_name[$i] <br>\n"; $i++; > } > echo "</td><td width='30%'>\n"; > while ($i <= $num_rows ){ > echo "$type_name[$i] <br>\n"; $i++; > } > echo "</tr></table>\n"; > > } else { > echo " sorry no data found "; > } > ?> > > > -- > Cheers, Donald :) > __________________________________________ > As well as learning more, > I learn that there is even more I don't know > > http://www.donaldrnoble.f2s.com > ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ > >

« previous php.general (#47896) next »