alternating colors in table

I have tried following at least a dozen examples from the internet and can’t get anything to come close to working. I have created a table from data pulled from a database, and have used html to format the rest of the table as best I could. The main portion of the table is generated from a database and can vary based on the number of events stored so simple html row tags can’t be used. It is data that has been entered into our Thyme calendar. The basic php portion of the file was written by someone else that understands php much better than I. I have been able to modify it for my use and thanks to some help from him and here, it works perfectly. I would like to finish the formatting with alternating color rows.

The current table looks like this http://nsrca.us/contest-results/2009/d1.html, and I am trying to match the formatting of this. http://nsrca.us/calendartop/calendar.html

I have tried several java scripts and php codes but I can’t seem to apply them specifically to my file. (instructions aren’t basic enough for me :slight_smile: )

Below is the file that I am working on. There are a lot of lines commented out that were used to test ideas etc.

If anyone can point me in the right direction I would appreciate it.

[php]<?php

//defined( ‘_JEXEC’ ) or die( ‘Restricted access from Kcal; Buff’ );

//echo ‘

Start K-Factor Calendar
’;

?>

K-Factor Calendar <?php ############################################################ # base path of files, with trailing slash #define("_CAL_BASE_PATH_", "/home/nsrca.org/public_html/thyme-1.3-ioncube/"); define("_CAL_BASE_PATH_", "/var/www/web109/web/thyme-1.3-ioncube/"); define("_CAL_BASE_URL_", "http://nsrca.us/thyme-1.3-ioncube/"); # base url for calendar, with trailing slash include_once(_CAL_BASE_PATH_ . "include/classes/class.calendar.php"); ############################################################ function order ($a, $b) # sort alphabetically by date and district { $retval = strnatcmp($a['district'], $b['district']); if(!$retval) return strnatcmp($a['date'], $b['date']); return $retval; } $nbsp = ' '; $cal = new calendar(); $cal->set("calendar", 2); $cal->set("event_types", "0"); # trying to get events to link to their details $cal->set("event_links", 1); $cal->set("event_view_url", "view.php?eid=%eid&inst=%inst"); # events yet to come in current year #$week = $cal->get_event_list($cal->year."-12-31", _ex_localtime()); # all events for current year #$week= $cal->get_event_list("year", _ex_localtime()); # suggestion from Thymenews events for specific year $week= $cal->get_event_list("year", 2009); $ect = count($week); $yr=$cal->year; # Buff was here. #echo "Event list day count is: ".$ect.'
'; #echo "The current year is ".$yr.'
'; # Buff was here. $contest = array(array()); ?> h2 { color: #EBEBEB; } .Arial {font-family: Arial, Helvetica, sans-serif; font-size: large; color: #CCCCCC; font-weight: bold; }     <?php #echo "<valignDistrict 1 ".$cal->year." Contests"; #echo "

<valignDistrict 1 ".$cal->year." Contests

"; #echo "District 1 ".$cal->year." Contests"; #?>
  District 1 2009 Contests
<table width='100%' style=' border: 1px solid #31639C; padding: 0px; border-collapse: collapse <pre><code>' bgcolor="#C1CEDE">  

Date

State

City

Contest Name

Results

  <?php $j=-1; $savtitle = null; for($i = 0; $i < $ect; $i++) { if(!count ($week[$i])) continue; foreach($week[$i] as $event) { $ev = $cal->get_event($event['id'], $event['instance']); if ($savtitle == $ev->title) continue; $cpos = 0; $mystring = $ev->addr_ci; $findme = ','; $pos = strpos($ev->addr_ci, $findme); $clen = $pos; if ($pos === false) {echo ""; ($cal->format_date("n/j/y",$ev->starttime)).". CD ".$ev->org_name.". Please correct the Thyme Calendar data for this event.
"; continue; } else { #if ($pos === false) { # echo "A comma and/or the city is missing in the address (Location: City, St, Zip Field) of the ".$ev->cal_title."; ".$ev->title." contest, dated: " # .($cal->format_date("n/j/y",$ev->starttime)).". CD ".$ev->org_name.". Please correct the Thyme Calendar data for this event.
"; # continue; #} else { $spos = $clen + 2; $slen = 2; $st = substr($ev->addr_ci, $spos, $slen); $ci = substr($ev->addr_ci, $cpos, $clen); $dis = substr($ev->cal_title, 1, 2); $savtitle = $ev->title; $j = $j+1; # $contest[$j] = array('state'=>$st, 'district'=>$dis, 'date'=>($cal->format_date("n/j/y",$ev->starttime)), 'city'=>$ci, 'name'=>$ev->title); #$contest[$j] = array('date'=>($cal->format_date("n/j/y",$ev->starttime)), 'state'=>$st, 'city'=>$ci, 'name'=>$ev->title, 'url'=>$ev->url); $contest[$j] = array('date'=>($cal->format_date("n/j/y",$ev->starttime)), 'state'=>$st, 'city'=>$ci, 'name'=>$ev->title, 'results'=>$ev->results); } } } usort ( $contest, 'order' ); $savtitle = null; $savdis = $contest[0]['district']; $j = $j+1; for($i = 0; $i < $j; $i++) { if ($savtitle == $contest[$i]['name']) continue; if ($savdis != $contest[$i]['district']) echo "

".$nbsp."

".$nbsp."

".$nbsp."

".$nbsp."

".$nbsp."

".$nbsp."

"; echo ""; echo "

".$contest[$i]['']."

"; echo "

".$contest[$i]['date']."

"; echo "

".$contest[$i]['state']."

"; #echo "

".$contest[$i]['district']."

"; echo "

".$contest[$i]['city']."

"; echo "

".$contest[$i]['name']."

"; # echo "

".$contest[$i]['url']."

"; #$url= $contest[$i]['url']; # Contetst results section, left blank if no results in the DB. Clickable "results" link if data is present. url statements used to prove concept. $results= $contest[$i]['results']; # echo "Results"; #echo "Results"; echo ""; //if nothing is in the url column of the database then "Nothing" is displayed #if($url == "") {echo "";} if($results==""){echo"";} #else { echo "Results";} else { echo "Results";} echo ""; echo "

".$contest[$i]['']."

"; echo ""; ; $savtitle = $contest[$i]['name']; $savdis = $contest[$i]['district']; } ?>
 

Back
<?php //echo "End K-Factor Calendar"; //echo "


Out of the office; gone to visit the grandkids. Will return Sunday evening, 4/20/2008
Buff Miller"; ?> [/php]

Where you have the loop (outputting table rows), just make row color dependent on loop counter:
[php]
for($i = 0; $i < $j; $i++)
{
if ($savtitle == $contest[$i][‘name’]) continue;
if ($savdis != $contest[$i][‘district’]) echo "

// … etc.
[/php]

I appreeciate the response but in this case I need more of a push instead of just the point in the right direction:) I don’t know enough php to implement your idea.
Thank you,

Well, just update this line in your code:
[php]if ($savdis != $contest[$i][‘district’]) echo "

[/php]

so that it look like this:
[php]if ($savdis != $contest[$i][‘district’]) echo "

[/php]

If I understand what you are trying to do is to look at the remainder from $i/2 if it is 1 use 1 color and if not choose the second color. seems much too easy :slight_smile:

Unfortunately that line even though it lists a background color does not control the background of this portion of the table. I tried changing the color of the original file and it does nothing. The color of this section is controlled up higher above the php code.

[code]

[/code]

‘bgcolor=cicede> controls the color of the main field. If I remove the reference to background color and change that line to’> the background turns white. Unfortunately the modified line still does not control the background.

Thanks again,
Stuart

 

Date

State

City

Contest Name

Results

 

I do not seem to be able to edit my last response so some additional information. I have to speak to the person that wrote the original file. He chose ‘district’ as part of the looping statements and I removed the district column from my table. Even without it it still works. When I change that line to use ‘date’, that line will control color and the line given by phphelp will cause alternating colors. The problem is that the colors are new lines without data added between my lines with the data in them. This happens with the original statement as well. With phphelp’s statement the new line alternates between the 2 colors :slight_smile: In order to get this to work to this point I also had to chage the way the background color was listed in that line to

if ($savdis != $contest[$i]['date']) echo "<tr valign='middle' bgcolor='#bbccbb'>

I need to play with this some more to see if I can figure out why the additional line is being added. Doesn’t look too bad but can make the table a little longer than I would like.

After doing a little more “learning” and trimming down trhe unneccessary parts of the code I was able to apply the suggestion given here in the ight place and it works.

Adding the code to the first line of my table as below.

[code]echo “

”;
echo “

”.$contest[$i][‘date’]."

";
echo “

”.$contest[$i][‘state’]."

";
echo “

”.$contest[$i][‘city’]."

";
echo “

”.$contest[$i][‘name’]."

";

[/code]

Thank you very much.

Sponsor our Newsletter | Privacy Policy | Terms of Service