RE: [PHP] putting a list of data into 3 columns?
Here is a basic (untested) example. $link = mysql_connect("localhost", "username", "password"); mysql_select_db("database", $link); $result = mysql_query("SELECT product_name FROM products", $link); $num_rows = mysql_num_rows($result); // read in data while ($row = mysql_fetch_array ($result)) { $products[] = $row["product_name"]; } // display data $num_display_rows = ceil($num_rows / 3) $indx = 0; while ($indx $num_display_rows) { echo "{$products[$indx]}, {$products[$indx+$num_rows]}, {$products[$indx+$num_rows*2]}"; $indx++; } -Stewart -Original Message- From: DRN [mailto:[EMAIL PROTECTED]] Sent: 07 April 2001 19:55 To: [EMAIL PROTECTED] Subject: [PHP] putting a list of data into 3 columns? Hi, I would like to display a list of products from a MySQL database in 3 columns. as shown below: Product A Product D Product G Product B Product E Product H Product C Product F The problem I have is I don't know how many products there will be, so I can't just print out the first 3, start new column, print out next three, start new column, print out rest, end table. I presume I will have to count the number of results, then use this to wok out how many have to go in the first column etc. As I have only recently started using PHP / MySQL I am not sure how this should be done, has anybody done this (or something similar) so that I could look at the relevant code. Many thanks for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] putting a list of data into 3 columns?
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("trtd%s/tdtd%s/tdtd%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" [EMAIL PROTECTED] wrote: Lindsay Adams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... | 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\ntr\ntd 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 "/tdtd width='30%'\n"; while ($i = (2*$num_rows/3) ){ echo "$type_name[$i] br\n"; $i++; } echo "/tdtd 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
Re: [PHP] putting a list of data into 3 columns?
Lindsay Adams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... okay. your solution is not going to scale well, is it. if you have more than nine items, you have to rewrite your code. I (sort of) understand your solution, but I don't see how my solution is not going to scale well? I am trying to count the number of items, then list until I get to 1/3 then start a new cell and list until I get to 2/3, then new cell, then finish table. The reason for doing it this way is that this will produce a valid table. It will also allow me to add in another 2 cells to give vertical lines separating the columns, but I missed this out originally for clarity. -- Cheers, Donald :) __ As well as learning more, I learn that there is even more I don't know http://www.donaldrnoble.f2s.com ¯¯ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] putting a list of data into 3 columns?
You don't need to count...in your loop you can do something like this: if($current_pos%3==0){ //then you're at a multiple of three //code to start new column here } -jack -Original Message- From: DRN [mailto:[EMAIL PROTECTED]] Sent: Saturday, April 07, 2001 2:55 PM To: [EMAIL PROTECTED] Subject: [PHP] putting a list of data into 3 columns? Hi, I would like to display a list of products from a MySQL database in 3 columns. as shown below: Product A Product D Product G Product B Product E Product H Product C Product F The problem I have is I don't know how many products there will be, so I can't just print out the first 3, start new column, print out next three, start new column, print out rest, end table. I presume I will have to count the number of results, then use this to wok out how many have to go in the first column etc. As I have only recently started using PHP / MySQL I am not sure how this should be done, has anybody done this (or something similar) so that I could look at the relevant code. Many thanks for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] putting a list of data into 3 columns?
Assuming your items are in an array $items =array() //assume a bunch of items in this array For($i = 0; $i (count($items)/3); $i +=1){ printf("%s\t%s\t%s",$items[$i],$items[$i+3],$items[$i+6]); } Should print 3 columns of tab separated text. Note: typed this quickly, untested, but the theory is sound. Might have to twiddle the $i(count... Section. If you have 8 itesm, as shown and you divide by 3 you get 2.xx So, the the loop will print out: $items[0] $items[3] $items[6] $items[1] $items[4] $items[7] $items[2] $items[5] $items[8} // because $items[8] doesn't exist, it won't print. // if it spits out an error there, put a @in front of printf to turn off error reporting. On 4/7/01 11:58 AM, "Jack Dempsey" [EMAIL PROTECTED] wrote: You don't need to count...in your loop you can do something like this: if($current_pos%3==0){//then you're at a multiple of three //code to start new column here } -jack -Original Message- From: DRN [mailto:[EMAIL PROTECTED]] Sent: Saturday, April 07, 2001 2:55 PM To: [EMAIL PROTECTED] Subject: [PHP] putting a list of data into 3 columns? Hi, I would like to display a list of products from a MySQL database in 3 columns. as shown below: Product A Product D Product G Product B Product E Product H Product C Product F The problem I have is I don't know how many products there will be, so I can't just print out the first 3, start new column, print out next three, start new column, print out rest, end table. I presume I will have to count the number of results, then use this to wok out how many have to go in the first column etc. As I have only recently started using PHP / MySQL I am not sure how this should be done, has anybody done this (or something similar) so that I could look at the relevant code. Many thanks for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] putting a list of data into 3 columns?
Lindsay Adams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... | Assuming your items are in an array | $items =array() //assume a bunch of items in this array | For($i = 0; $i (count($items)/3); $i +=1){ | printf("%s\t%s\t%s",$items[$i],$items[$i+3],$items[$i+6]); | } | | Should print 3 columns of tab separated text. | Note: typed this quickly, untested, but the theory is sound. Might have to | twiddle the $i(count... Section. | | If you have 8 itesm, as shown and you divide by 3 you get 2.xx | So, the the loop will print out: | | $items[0] $items[3] $items[6] | $items[1] $items[4] $items[7] | $items[2] $items[5] $items[8} | // because $items[8] doesn't exist, it won't print. | // if it spits out an error there, put a @in front of printf to turn off | error reporting. | | | On 4/7/01 11:58 AM, "Jack Dempsey" [EMAIL PROTECTED] wrote: | | You don't need to count...in your loop you can do something like this: | if($current_pos%3==0){//then you're at a multiple of three | //code to start new column here | } | | -jack | Neither of these were quite what I was looking for, I was hoping I could make a table with 3 td's side by side, each having a third of the list of products (with br between them). Is this possible? If not I will try to adapt one of these methods. Cheers for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] putting a list of data into 3 columns?
the method i gave you will do this..if you use the if clause i showed and work with it for more than a minute you'll see how you can test to see if you should add an /tdtd to your data, and this will let you make your columns.play with it and you'll see.. -jack -Original Message- From: DRN [mailto:[EMAIL PROTECTED]] Sent: Saturday, April 07, 2001 7:55 PM To: [EMAIL PROTECTED] Subject: Re: [PHP] putting a list of data into 3 columns? Lindsay Adams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... | Assuming your items are in an array | $items =array() //assume a bunch of items in this array | For($i = 0; $i (count($items)/3); $i +=1){ | printf("%s\t%s\t%s",$items[$i],$items[$i+3],$items[$i+6]); | } | | Should print 3 columns of tab separated text. | Note: typed this quickly, untested, but the theory is sound. Might have to | twiddle the $i(count... Section. | | If you have 8 itesm, as shown and you divide by 3 you get 2.xx | So, the the loop will print out: | | $items[0] $items[3] $items[6] | $items[1] $items[4] $items[7] | $items[2] $items[5] $items[8} | // because $items[8] doesn't exist, it won't print. | // if it spits out an error there, put a @in front of printf to turn off | error reporting. | | | On 4/7/01 11:58 AM, "Jack Dempsey" [EMAIL PROTECTED] wrote: | | You don't need to count...in your loop you can do something like this: | if($current_pos%3==0){//then you're at a multiple of three | //code to start new column here | } | | -jack | Neither of these were quite what I was looking for, I was hoping I could make a table with 3 td's side by side, each having a third of the list of products (with br between them). Is this possible? If not I will try to adapt one of these methods. Cheers for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] putting a list of data into 3 columns?
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: Print should be: printf("trtd%s/tdtd%s/tdtd%s/td/tr",$items[$i],$items[$i+3], $items[$i+6]); That will print your 3 columns in a table. And it is essentially identical to to what I sent. Just have to modify the format of the printf statement. You can also do it in a regular print statement: Print("trtd$items[$i]/tdtd$items[$i+3]/tdtd$items[$i+6]/td/tr \n"); Or how about a here doc for php4 Print EOF trtd$items[$i]/tdtd$items[$i+3]/tdtd$items[$i+6]/td/tr\n EOF The for loop stays the same, only the print statement changes. Lindsay Adams --- On 4/7/01 4:55 PM, "DRN" [EMAIL PROTECTED] wrote: Lindsay Adams [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... | Assuming your items are in an array | $items =array() //assume a bunch of items in this array | For($i = 0; $i (count($items)/3); $i +=1){ | printf("%s\t%s\t%s",$items[$i],$items[$i+3],$items[$i+6]); | } | | Should print 3 columns of tab separated text. | Note: typed this quickly, untested, but the theory is sound. Might have to | twiddle the $i(count... Section. | | If you have 8 itesm, as shown and you divide by 3 you get 2.xx | So, the the loop will print out: | | $items[0] $items[3] $items[6] | $items[1] $items[4] $items[7] | $items[2] $items[5] $items[8} | // because $items[8] doesn't exist, it won't print. | // if it spits out an error there, put a @in front of printf to turn off | error reporting. | | | On 4/7/01 11:58 AM, "Jack Dempsey" [EMAIL PROTECTED] wrote: | | You don't need to count...in your loop you can do something like this: | if($current_pos%3==0){//then you're at a multiple of three | //code to start new column here | } | | -jack | Neither of these were quite what I was looking for, I was hoping I could make a table with 3 td's side by side, each having a third of the list of products (with br between them). Is this possible? If not I will try to adapt one of these methods. Cheers for your help, Donald -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]