Results 1 to 7 of 7

Thread: table alternate row color

  1. #1
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default table alternate row color

    i am trying to change this function to show me alternate row color, not alternate column color.

    how can i modify this function without remaking it completely and without java-script (it can be made directly)..?
    i tried with come css 3 command i found on google
    Code:
    tr:nth-child(odd) { background-color:#eee; }
    tr:nth-child(even) { background-color:#fff; }
    and with examples from here:
    http://lorenzod8n.wordpress.com/2007/06/02/alternating-row-color-in-htmlcss-with-php/
    http://www.experts-exchange.com/Web_..._23003019.html
    and more....
    and i dind't manage to remake it...
    can someone please help me?
    thank you in advance!!!

    Code:
    function table($res) {
    
    	// Columns
    	$cols = explode(",", $site_config["torrenttable_columns"]);
    	$cols = array_map("strtolower", $cols);
    	$cols = array_map("trim", $cols);
    	$colspan = count($cols);
    	// End
    	
    	echo '<table align=center cellpadding="3" cellspacing="0" class="class1" width=99%>
    	<tr >';
    	
    	foreach ($cols as $col) {
    	      	switch ($col) {
    			case 'text':
    				echo "<td class=ttable_head>Data I<b></td>";
    			break;
    		        case 'text2':
    				echo "<td class=ttable_head>Data II<b></td>";
    			break;
    			case 'modif':
    				echo "<td class=ttable_head>Data III<b></td>";
    			break;
    			
    		}
    	}
    	
    	
    	echo "</tr>";
    
    
    	while ($row = mysql_fetch_assoc($res)) {
    		$id = $row["id"];
    		print("<tr class=testtabel5>\n");
    	$x = 1;
    
    	foreach ($cols as $col) {
    	
    
    		switch ($col) {
    			
    			case 'text':
    			    
    				print("<td  align=center>".$row['data1']);
    			break;
    			case 'text2':
    				print("<td class=class2 align=center>".$row['data2']);
    			break;
    			case 'modif':
    				$char1 = 35; //cut name length 
    				$smallname =CutName(htmlspecialchars($row["name"]), $char1);
    				$test = $row["data1"];
    				$dispname = "<b>".$smallname."</b>";
    
    				
    				print("<td class=class3 nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"snow.php?id=$id\">Snows</a>");
    			break;
    					}
    		if ($x == 2)
    			$x--;
    		else
    			$x++;
    	}
    	
    		print("</tr>\n");
    
    	}
    
    	print("</table><BR>\n");
    
    }

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    PHP Code:
    foreach($rows as $row) {
        
    ///this is where your stuff for a row goes... like alternate tr class
       
    foreach($row as $cell) {
          
    ///this is where your stuff for a "column" goes,
          ///though it is generated cell by cell within each row
          ///alternate columns here, using td class
       
    }

    So the simple answer is just move anything from the column area (inner loop) to the row area (outer loop).

    Your code is also spaced very strangely. It would be a lot easier to make this stuff work in it if all of the tabs lined up (and also to help you).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    pilotvtm (12-15-2009)

  4. #3
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    PHP Code:
    foreach($rows as $row) {
        
    ///this is where your stuff for a row goes... like alternate tr class
       
    foreach($row as $cell) {
          
    ///this is where your stuff for a "column" goes,
          ///though it is generated cell by cell within each row
          ///alternate columns here, using td class
       
    }

    So the simple answer is just move anything from the column area (inner loop) to the row area (outer loop).

    Your code is also spaced very strangely. It would be a lot easier to make this stuff work in it if all of the tabs lined up (and also to help you).

    thanks for the answer!
    i will try it an d come back and announce you if it worked!
    Last edited by pilotvtm; 12-15-2009 at 10:15 AM.

  5. #4
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    i ralised that this posts one row 5 times...why this happens?
    i need 10 on page, now are all on page and are in alphabetical order not in date order...
    how can i arrange them??


    the second part became something like this:

    Code:
            $rowclass = 0;
    
    foreach ($cols as $rows) {
    	
    echo "<tr class=row$rowclass>";
    
               foreach ($cols as $col) {
    	
    
                $rowclass = 1 - $rowclass;
    
    
    		switch ($col) {
    			
    			case 'text':
    			    
    				print("<td  align=center>".$row['data1']);
    			break;
    			case 'text2':
    				print("<td class=class2 align=center>".$row['data2']);
    			break;
    			case 'modif':
    				$char1 = 35; //cut name length 
    				$smallname =CutName(htmlspecialchars($row["name"]), $char1);
    				$test = $row["data1"];
    				$dispname = "<b>".$smallname."</b>";
    
    				
    				print("<td class=class3 nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"snow.php?id=$id\">Snows</a>");
    			break;
    					}
    		if ($x == 2)
    			$x--;
    		else
    			$x++;
    	}
    	}
    		print("</tr>\n");
    
    	
    }
    
    	print("</table><BR>\n");
    
    }
    thanks for your help!!!!!!
    Last edited by pilotvtm; 12-15-2009 at 01:58 PM.

  6. #5
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    sorry for the double post, but i didn't manage to make this work....any other ideas?

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I think this may be because you are using $cols in the loop. The code looks ok, but I don't know what the data inserted is.
    You can insert the method in my code into your original code easily: just put the alternation code in the outer loop. You don't need to use foreach loops like that-- that was just the easiest example for the code.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    pilotvtm (12-18-2009)

  9. #7
    Join Date
    Dec 2009
    Posts
    9
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    I think this may be because you are using $cols in the loop. The code looks ok, but I don't know what the data inserted is.
    You can insert the method in my code into your original code easily: just put the alternation code in the outer loop. You don't need to use foreach loops like that-- that was just the easiest example for the code.
    did it but in another way...this way didn't work..

    Code:
    while ($row = mysql_fetch_assoc($res)) {
    		$id = $row["id"];
    		
    		$rowclass = 1 - $rowclass;
    	
    print("<tr class=rowt$rowclass>\n");
    	
    
    echo "<tr class=row$rowclass>";
    
    				$char1 = 35; //cut name length 
    				$smallname = CutName(htmlspecialchars($row["name"]), $char1);
    				$dispname = "<b>".$smallname."</b>";
    	
    	print("<td class=rowt$rowclass nowrap><a title=\"".$row["name"]."\" href=\"titluri-detalii.php?id=$id&amp;hit=1\">$dispname</a>");
            print("<td class=rowt$rowclass align=center>".$row['data1']);
            print("<td class=rowt$rowclass class=ttable_col4 align=center>".$row['data2']);
    	print("<td class=rowt$rowclass nowrap>&nbsp;<a title=\"".$row["name"]."\" href=\"titluri-modificari.php?id=$id\">Modificari</a>");
    			
    	
         
     $rowclass = 0;
    	print("</tr>\n");}
    	print("</table><BR>\n");
    $rowclass = 1 - $rowclass;
    thanks for your help!!!
    Last edited by pilotvtm; 12-18-2009 at 01:06 PM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •