SEO Scripts
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   SEO Chat ForumsOtherSEO Scripts

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread SEO Chat Forums Sponsor:
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  
Old March 31st, 2008, 01:51 PM
rXL3 rXL3 is offline
Registered User
SEO Chat Newbie (0 - 499 posts)
 
Join Date: Oct 2007
Posts: 13 rXL3 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 27 m 33 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old April 1st, 2008, 09:04 AM
JagNet's Avatar
JagNet JagNet is offline
Smoke me a kipper...
Click here for more information
 
Join Date: Aug 2007
Posts: 1,506 JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level)JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level)JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level)JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level)JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level)JagNet User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Weeks 21 h 11 m 25 sec
Reputation Power: 24
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:
php Code:
Original - php Code
  1. <?php
  2. $dataTest = array();
  3. $result = mysql_query($query);
  4. while($row = mysql_fetch_assoc($result)){
  5.     $dataTest[] = $row;
  6. }
  7. echo '<pre>'; print_r($dataTest); echo '</pre>';
  8. ?>


To print out the columns left to right, top to bottom is simpler:
php Code:
Original - php Code
  1. <?php
  2. $dataTest = array('a','b','c','d','e','f','g');
  3. $table = '<table>';
  4. $counter = 0;
  5. // find total number of items in array. If it's not an even number
  6. // we need to add 1 so as to avoid breaking the table by
  7. // omitting the final cell
  8. $total = count($dataTest);
  9. if ($total%2 != 0) {
  10.         $total+=1;
  11. }
  12.  
  13. // loop through the array
  14. for ($i=0;$i<count($dataTest);$i++) {
  15.  if ($counter%2 == 0) { // first column
  16.      $table .= '<tr><td>' . $dataTest[$i] . '</td>';
  17.  } else { // second column
  18.         $table .= '<td>' . $dataTest[$i] . '</td></tr>';
  19.  }
  20.  $counter++;
  21. }
  22.  
  23. // complete table and output results
  24. $table .= '</table>';
  25. echo $table;
  26. ?>


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:
Original - php Code
  1. <?php
  2. // breaks array into required number of chunks
  3. function partition( $list, $p ) {
  4.     $listlen = count( $list );
  5.     $partlen = floor( $listlen / $p );
  6.     $partrem = $listlen % $p;
  7.     $partition = array();
  8.     $mark = 0;
  9.     for ($px = 0; $px < $p; $px++) {
  10.         $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
  11.         $partition[$px] = array_slice( $list, $mark, $incr );
  12.         $mark += $incr;
  13.     }
  14.     return $partition;
  15. }
  16.  
  17. $dataTest = array('a','b','c','d','e','f','g'); // test data
  18. $table = '<table>';
  19. $counter = 0;
  20. $arrayNum = 0;
  21.  
  22. // split data into 2 chunks
  23. $splitData = partition( $dataTest, 2);
  24.  
  25. // check if total number of items is an even number & add 1 if necessary
  26. $total = count($dataTest);
  27. if ($total%2 != 0) {
  28.         $total+=1;
  29. }
  30.  
  31. // loop through results
  32. for ($i=0;$i<$total;$i++) {
  33.  // find whether we're using the first or second array on each loop
  34.  // $set will be either 0 or 1
  35.  $set = $counter%2;
  36.  
  37.  if ($set == 0) { // first column
  38.      $table .= '<tr><td>' . $splitData[$set][$arrayNum] . '</td>';
  39.  } else { // second column
  40.      $table .= '<td>' . $splitData[$set][$arrayNum] . '</td></tr>';
  41.      
  42.      // add one to $arrayNum ready for next row
  43.      $arrayNum++;
  44.  }
  45.  
  46.  $counter++;
  47. }
  48.  
  49. // close table and output the results
  50. $table .= '</table>';
  51. echo $table;
  52. ?>


Should output:
a e
b f
c g
d
__________________
!! Peanut free and Google friendly !!
New to SEO? SEOChat SEO FAQs
Forum Rules and Posting Guidelines
URL canonicalization code solutions

Reply With Quote
  #3  
Old April 1st, 2008, 10:36 AM
Highland's Avatar
Highland Highland is offline
Extremely Googled
SEO Chat Novice (500 - 999 posts)
 
Join Date: Dec 2004
Posts: 546 Highland User rank is Corporal (100 - 500 Reputation Level)Highland User rank is Corporal (100 - 500 Reputation Level)Highland User rank is Corporal (100 - 500 Reputation Level)Highland User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 6 Days 1 h 26 m 47 sec
Reputation Power: 5
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:
echo 2%1;
//prints 1
echo 3%3;
//prints 0
echo 4%3;
//prints 1 

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:
 $query7 ="SELECT DISTINCT subCat1 FROM Products_copy_copy_copy_copy WHERE Manufacturer ='$manu' AND subCat1 <> '' ORDER BY subCat1 ASC";
$result7=mysql_query($query7);    
$counter=0;
while(list( 
$subcat1a)= mysql_fetch_row($result7))
{
    
$subcatstring $subcat1a;
    
$subcatstring str_replace(' '''$subcat1a);    
    
$subcatstring strtolower($subcatstring);

$fstr=NULL;
if(
$counter%2==0$fstr.="<tr>";
$fstr.="<td class=\"textmain\"><div align=\"center\"><b><a href=\"$subcatstring/\">$subcat1a</a></b></div></td>";    
if(
$counter%2==1$fstr.="</tr>";
fwrite($newfile,$fstr);
$counter++; 
 } 

Reply With Quote
Reply

Viewing: SEO Chat ForumsOtherSEO Scripts > Building a Table With PHP, how to do two columns


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Try It Free




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway