Solved

PHP alternating table colors in table - populated from string

Posted on 2007-12-05
4
674 Views
Last Modified: 2012-06-27
Would very much appreciate some suggestions on the following:

I have a string which is being read from a MySQL field.
The string is the contents of an excel spreadsheet but it contains simplified form of BB code, which I use a series of str_replace() to convert to an HTML table.

That part works OK

My roblem is that I need to alternate the BG color in the table and as I don't always know how many rows there wil be I can't hard code the colou.

I thought that if I used explode() to turn the string in to an array with each each value of the array being a row of the table and the contents of the table being echoed via a "for" loop which ran the color change script it would work ...... but no luck.

Following is a code snippet so that you see what I mean.

Thanks
<?php
 
// I set the colour variables
$color1 = "#C5AE9E"; 
$color2 = "#D2C1B5";  
$ $row_count = 0;
 
// here is the string that will become the HTML table
// this is just a rogh example of it
$string = "[Otitle]    Starters    [Ctitle]            
[Odish]    Garlic Bread     [Cdish]    [Oprice]    7.00    [Cprice]
[Odish]    Bruschetta    [Cdish]    [Oprice]    7.50    [Cprice]
[Odish]    Nico's Salad    [Cdish]    [Oprice]    9.00    [Cprice]";
 
// again just an example - this time of the str_replace() section
 
$string = str_replace("[Otitle]", "<tr bgcolor='colourMyRow'> <td><strong>", $string);  
$string = str_replace("[Ctitle]", "</strong></td></tr> %^&*", $string); 
 
// now to convert the string into an Array using the un needed "%^&*" text 
 
$myArray = explode("%^&*", $string);
 
 
// here I count the total values in the Array and start the loop
$arrayCounted = sizeof($myArray);
for ($i=0;$i<=$arrayCounted;$i++){
 
// this should alternate my colours
  $row_colour = ($row_count % 2) ? $color1 : $color2;  
 
// this should assign the colour to each row in the table
    $menuArray [$i] = str_replace("colourMyRow", "$row_colour", $menuArray[$i]);  
 
$row_count++;      
  
  echo ($myArray[$i]);                                                                             
} 
 
// as I said before, everything but the colour change works
?>

Open in new window

0
Comment
Question by:pepenuro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 24

Expert Comment

by:glcummins
ID: 20411547
The problem appears to be here:

$ $row_count = 0;


Try changing that to:

$row_count = 0;
0
 
LVL 5

Accepted Solution

by:
nacker2000 earned 250 total points
ID: 20413456
Hi,

Try the following code:
<?php
	// I set the colour variables
	$color1 = '#C5AE9E';
	$color2 = '#D2C1B5'; 
	$row_count = 0;
 
	// here is the string that will become the HTML table
	// this is just a rogh example of it
	$string = "[Otitle]    Starters    [Ctitle]            
	[Odish]    Garlic Bread     [Cdish]    [Oprice]    7.00    [Cprice]
	[Odish]    Bruschetta    [Cdish]    [Oprice]    7.50    [Cprice]
	[Odish]    Nico's Salad    [Cdish]    [Oprice]    9.00    [Cprice]";
 
	// again just an example - this time of the str_replace() section
	$string = str_replace("[Otitle]", "<tr bgcolor='colourMyRow'> <td><strong>", $string);  
	$string = str_replace("[Ctitle]", "</strong></td></tr> %^&*", $string); 
 
	// now to convert the string into an Array using the un needed "%^&*" text 
	$myArray = explode("%^&*", $string);
 
	foreach($myArray as $row){
		// this should alternate my colours
		$row_count++;
		$row_colour = ($row_count % 2) ? $color1 : $color2;  
		 
		// this should assign the colour to each row in the table
		$row = str_replace('colourMyRow', $row_colour, $row);     
		
		// output 
		echo($row);                                                                             
	} 
 
	// as I said before, everything but the colour change works
?>

Open in new window

0
 

Author Closing Comment

by:pepenuro
ID: 31412846
Worked perfectly the first time.
I had included a typo in my question ($ $row_count = 0;
) apologies but that was just  typo in the copy and pasting for the snippet.

Many thanks!!
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20414616
Glad I could help :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question