12 Replies - 727 Views - Last Post: 05 May 2012 - 05:54 AM Rate Topic: -----

#1 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Need help with zebra strip or color alternation

Posted 03 May 2012 - 06:14 PM

I am getting information from a database I created and I would like to have zebra strip or alternating color for each row. I have being trying this code, but I have it either in the wrong place or I got the code wrong.
$i = 0;
while ($row = mysql_fetch_array($result)) {
    if ($i % 2 != 0) {
       $bgcolor = ($counter % 2 === 0) ? 'red' : 'blue';
       $i++;
    }  
}


here is rest of the code:
$query = "SELECT movie_id, movie_name, " . 
         "movie_director, movie_leadactor " .
         "FROM movie";

$result = mysql_query($query, $link)
    or die(mysql_error());
$num_movies = mysql_num_rows($result);

$movie_header=<<<EOD
<h2><center>Movie Review Database</center></h2>
<table width="70%" border="1" cellpadding="2" cellspacing="2"
    align="center">
    <tr class="classA">
        <th>Movie Title</th>
        <th>Movie Director</th>
        <th>Movie Lead Actor</th>
   </tr>
EOD;

function get_director() {
    global $movie_director;
    global $director;
    
    $query_d = "SELECT people_fullname " .
        "FROM people " .
        "WHERE people_id='$movie_director'";
$results_d = mysql_query($query_d)
    or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}

function get_leadactor() {
global $movie_leadactor;
global $leadactor;
$query_a = "SELECT people_fullname " .
    "FROM people " .
    "WHERE people_id='$movie_leadactor'";
$results_a = mysql_query($query_a)
    or die(mysql_error());
$row_a = mysql_fetch_array($results_a);
extract($row_a);
$leadactor = $people_fullname;
}

$movie_details = '';
while ($row = mysql_fetch_array($result)) {
    $movie_id = $row['movie_id'];
    $movie_name = $row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor = $row['movie_leadactor'];
    

    //get director's name from people table
    get_director();

    //get director's name from people table
    get_leadactor();

    $movie_details .=<<<EOD
    <tr>
        <td ><a href="movie_details.php?movie_id=$movie_id"
        title="Find out more about $movie_name">$movie_name</td>
        <td>$director</td>
        <td>$leadactor</td>
    </tr>
EOD;
}

$i = 0;
while ($row = mysql_fetch_array($result)) {
    if ($i % 2 != 0) {
       $bgcolor = ($counter % 2 === 0) ? 'red' : 'blue';
       $i++;
    }  
}
$movie_details .=<<<EOD
<tr>    
    <td>Total :$num_movies Movies</td>
</tr>
EOD;



$movie_footer ="</table>";
$movie =<<<MOVIE
$movie_header
$movie_details
$movie_footer
MOVIE;

echo "There are $num_movies movies in our database";
echo $movie;
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with zebra strip or color alternation

#2 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 06:47 PM

*
POPULAR

Instead of using php, you can use CSS3's nth child pseudo-selector. For example, to change the color of all even rows of a table, you could use:
tr:nth-child(2n) {
background-color:#f00;
}


Further reading
Using CSS3 could also reduce load on the server since there will be less PHP happening server-side.
Was This Post Helpful? 5
  • +
  • -

#3 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 10:15 PM

this is the CSS code I am using and I am still not getting the desire outcome
<style>

.even { background-color:#CCFFCC; }
.odd { background-color:#FFCC00;}

</style>


at
<tr class="even">
        <td ><a href="movie_details.php?movie_id=$movie_id"
        title="Find out more about $movie_name">$movie_name</td>
        <td>$director</td>
        <td>$leadactor</td>
    </tr>

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,164
  • Joined: 08-June 10

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 10:18 PM

does that one row has the correct colour at least?
Was This Post Helpful? 0
  • +
  • -

#5 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 10:49 PM

yes here is the image Posted Image
the movie names rows should alternate colors not all orange

This post has been edited by gemoney: 03 May 2012 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,164
  • Joined: 08-June 10

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 10:54 PM

did you alternate the class names?
Was This Post Helpful? 0
  • +
  • -

#7 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 11:14 PM

here is the code for css
<style>

.even { background-color:#CCFFCC; }
.odd { background-color:#FFCC00;}

</style>


the php
$query = "SELECT movie_id, movie_name, " . 
         "movie_director, movie_leadactor " .
         "FROM movie";

$result = mysql_query($query, $link)
    or die(mysql_error());
$num_movies = mysql_num_rows($result);

$movie_header=<<<EOD
<h2><center>Movie Review Database</center></h2>
<table width="70%" border="1" cellpadding="2" cellspacing="2"
    align="center">
    <tr class="even">
        <th>Movie Title</th>
        <th>Movie Director</th>
        <th>Movie Lead Actor</th>
   </tr>
EOD;

function get_director() {
    global $movie_director;
    global $director;
    
    $query_d = "SELECT people_fullname " .
        "FROM people " .
        "WHERE people_id='$movie_director'";
$results_d = mysql_query($query_d)
    or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;
}

function get_leadactor() {
global $movie_leadactor;
global $leadactor;
$query_a = "SELECT people_fullname " .
    "FROM people " .
    "WHERE people_id='$movie_leadactor'";
$results_a = mysql_query($query_a)
    or die(mysql_error());
$row_a = mysql_fetch_array($results_a);
extract($row_a);
$leadactor = $people_fullname;
}


$movie_details = '';
while ($row = mysql_fetch_array($result)) {
    $movie_id = $row['movie_id'];
    $movie_name = $row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor = $row['movie_leadactor'];
    

    //get director's name from people table
    get_director();

    //get director's name from people table
    get_leadactor();

    $movie_details .=<<<EOD
    
    <tr class="odd">
        <td><a href="movie_details.php?movie_id=$movie_id"
        title="Find out more about $movie_name">$movie_name</td>
        <td>$director</td>
        <td>$leadactor</td>
    </tr>
EOD;
}


$movie_details .=<<<EOD
<tr>    
    <td>Total :$num_movies Movies</td>
</tr>
EOD;



$movie_footer ="</table>";
$movie =<<<MOVIE
$movie_header
$movie_details
$movie_footer
MOVIE;

echo "There are $num_movies movies in our database";
echo $movie;
?>


Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,164
  • Joined: 08-June 10

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 11:32 PM

yepp, as I thought. you do not alternate the class names in your table (you’re using $movie_details, which has a fixed class name of "odd")
Was This Post Helpful? 0
  • +
  • -

#9 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 11:37 PM

View PostDormilich, on 04 May 2012 - 12:32 AM, said:

yepp, as I thought. you do not alternate the class names in your table (you’re using $movie_details, which has a fixed class name of "odd")

Well, that is my problem...where do I place the odd or even to alternate through the table?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,164
  • Joined: 08-June 10

Re: Need help with zebra strip or color alternation

Posted 03 May 2012 - 11:44 PM

    $movie_details .=<<<EOD
    
    <tr class="odd"> <!-- this needs to be made dependent on the row number -->
        <td><a href="movie_details.php?movie_id=$movie_id"
        title="Find out more about $movie_name">$movie_name</td>
        <td>$director</td>
        <td>$leadactor</td>
    </tr>
EOD;

you need to pass the appropriate class name on each iteration cycle.

simple example:
$str = '<tr class="%d">' . PHP_EOL;
$i = 10;
while ($i--)
{
    printf($str, ($i % 2));
}

Was This Post Helpful? 3
  • +
  • -

#11 gemoney  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 30-October 09

Re: Need help with zebra strip or color alternation

Posted 04 May 2012 - 05:32 AM

I've being trying to implement this code in the right place and I still don't know where or how to get it to work right...please help..
$i = 0;
while ($i++)
{
 $str = '<tr class="even">' .PHP_EOL; 
        <td><a href="movie_details.php?movie_id=$movie_id"
        title="Find out more about $movie_name">$movie_name</td>
        <td>$director</td>
        <td>$leadactor</td> 
   printf($str, ($i % 2)); 
}
str =  '</tr>';    
EOD;
}

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3524
  • View blog
  • Posts: 10,164
  • Joined: 08-June 10

Re: Need help with zebra strip or color alternation

Posted 04 May 2012 - 07:06 AM

that code would give you a syntax error. and you have to altername the <tr>’s class name. (despite that you wouldn’t even enter the loop, and even if, you’d be caught in an infinite loop)
Was This Post Helpful? 0
  • +
  • -

#13 hiddenghost  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 39
  • View blog
  • Posts: 621
  • Joined: 15-December 09

Re: Need help with zebra strip or color alternation

Posted 05 May 2012 - 05:54 AM

Oh god. Where do you put it? I don't know.

You can use this to make alternation though.
//$limit = mysql_num_rows($result);
$limit = 6; // I use 6. you use mysql_num_rows.
$i = 0;
$alternate = 0;
$alternateArray = array(' right ', ' left ');
while($i < $limit){
    echo $alternateArray[++$alternate];
    echo $alternateArray[--$alternate];
    $i++;
}


This post has been edited by hiddenghost: 05 May 2012 - 05:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1