|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Building a Table With PHP, how to do two columns
I am trying to build a table through fwrite in my php, the script builds an HTML page based on information from the database.
The part for Categories, I want to be a table that has two columns. Code:
$query7 ="SELECT DISTINCT subCat1 FROM Products_copy_copy_copy_copy WHERE Manufacturer ='$manu' AND subCat1 <> '' ORDER BY subCat1 ASC";
$result7=mysql_query($query7);
while(list( $subcat1a)= mysql_fetch_row($result7))
{
$subcatstring = $subcat1a;
$subcatstring = str_replace(' ', '', $subcat1a);
$subcatstring = strtolower($subcatstring);
fwrite($newfile,"
<td colspan=\"1\" class=\"textmain\"><div align=\"center\"><b><a href=\"$subcatstring/\">$subcat1a</a></b></div></td></tr>
");
}
The start of the table happens before this SQL command, that's why you don't see it. Ideally, I need some way to set it so that when it goes through the loop and is writing the name of a Category that should go in the left column, it doesn't include the last </tr> so when it goes to the next one, it's on the same row. Then when it's on that one, it would include the </tr> to start the next row. I'd also rather it went from the top-left to the bottom-left and then picked up again on the top-right working it's way down to the bottom-right How can I set it so it checks if it's in the first column or second column and decides accordingly whether or not to include </tr> in that bit of code. Last edited by rXL3 : March 31st, 2008 at 01:53 PM. |
|
#2
|
||||||||
|
||||||||
|
Here's a couple of ways of doing it. I'm using a static array rather than pulling from a database, but you can easily convert your data into a suitable array using something like:
To print out the columns left to right, top to bottom is simpler: php Code:
Should output: a b c d e f g To output the table top to bottom and left to right is somewhat more complex. I've hacked together something that works, though I'm sure there's more elegant solutions. php Code:
Should output: a e b f c g d
__________________
!! Peanut free and Google friendly !!
New to SEO? SEOChat SEO FAQsForum Rules and Posting Guidelines URL canonicalization code solutions |
|
#3
|
||||
|
||||
|
To simplify it some, what you need is a modulus. A modulus divides the first number by the second and returns the remainder (just like all those seemingly useless grade school lessons did). In PHP it's written with a % so
PHP Code:
This is handy because you can implement a counter and tell where we are with that counter. In the loop below, I've added such a counter. If our modulus is 0, we need a new row. If our modulus is 1 we need to end a row. PHP Code:
|
![]() |
| Viewing: SEO Chat Forums > Other > SEO Scripts > Building a Table With PHP, how to do two columns |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|