Results 1 to 7 of 7

Thread: alternating div bg colour for each record in db

  1. #1
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Default alternating div bg colour for each record in db 11 Mar 2011 @ 22.06

    Hi All,

    i am displaying records from my db and i want all the odd and even records to have different backgrounds

    my code is
    PHP Code:
    $query = mysql_query("SELECT * FROM tbl_prices WHERE prodID = '$prodID' ORDER BY prodPrice ASC")or die(mysql_query());
    $rows = mysql_num_rows($query2);
    while($row=mysql_fetch_array($query))
    {
        $storeID = $row['storeID'];
        $link = $row['prodLink'];
        $storeinfo = mysql_query("SELECT * FROM stores WHERE storeID = '$storeID'")or die(mysql_error());;
        $info = mysql_fetch_array($storeinfo);
        $name = $info['storeName'];
        $name2 = $info['name'];
        $ids = $info['storeID'];
        $logo = $info['logo'];
        $rating = $info['rating'];?>
        <div class='storeindividual1'>            
        <a href="<?php echo htmlspecialchars($link)?>" target="_blank">
        <span class="prices_store1">
        <?php print "<img src='/$logo' alt='$name' title='$name' border='0' width='88' height='31' />";?>
        </span>
            
        <span class="prices_price1">
        <?php 
        
    if ($row['prodPrice'] == NULL){
        print 
    "-";
        }
        elseif (
    $row['prodPrice'] == 0){
        print 
    "-";
        }
        elseif (
    $row['prodPrice'] == 1){
        print 
    "-";
        }
        else{
        print 
    "£".$row['prodPrice']; 
        }
    ?>
        </span>
        
        <span class="prices_delivery1">
        Free
        </span>
        
        <span class="prices_total1">
        <?php echo "£".$row['prodPrice']?>
        </span>

        <span class="prices_percentage1">
        <?php
            
    #echo "rrp: ".$prodRRP;
            
    $strippedRRP trim($prodRRP,"£");
            
    $saving number_format($strippedRRP-$row['prodPrice'],2);
            
    #echo "saving: ".$saving;
            
    $percentage = ( $saving $strippedRRP ) * 100;
            
    $percentage number_format($percentage,0);
            echo 
    $percentage."%";?>
        </span>

        </a></div><?php  
    }
    and its the <div class='storeindividual1'> which i want to replace with the relivant bg changes.

    my css is
    Code:
    .storeindividual1{width:933px;float:left;}
    can anyone help with this please?
    cheers
    Luke
      Reply With Quote

  2. #2
    Super Moderator CloudedVision's Avatar
    Join Date
    Jan 2009
    Posts
    831
    Blog Entries
    4

    Default 12 Mar 2011 @ 04.58

    Keep a running total of the results. the mod (remainder) operator can be used to detect odds and evens.

    Code:
    num%2
    It will be one for odds, two for evens.
      Reply With Quote

  3. #3
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Default 13 Mar 2011 @ 11.42

    hi mate,

    i have added a counter to my loop which increments with each record, and ive tried the mod (remainder) operator like so
    Code:
    print ($count%2)
    which returns 1 for odd and 0 for even which is fine,

    so i have modified my code to check the result of the mod like so
    Code:
        if($count%2 == 1)
        {?>
            <div class='storeindividual1'><?php
        }
        else
        {?>
            <div class='storeindividual1'><?php
        }?>
    but can i add another class to the above like so
    Code:
    <div class='storeindividual1,redbg'> or
    <div class='storeindividual1,yellowbg'>
    for example to add background styling to the divs?

    cheers mate
      Reply With Quote

  4. #4
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Default 13 Mar 2011 @ 12.24

    ok sorted it mate thanks
      Reply With Quote

  5. #5
    Administrator WelshStew's Avatar
    Join Date
    Dec 2008
    Posts
    2,976
    Blog Entries
    3

    Default 14 Mar 2011 @ 07.18

    There are a couple of ways to do this, either with the method you have above with some slight changes:
    Code:
        if($count%2 == 1)
        {?>
            <div class='storeindividual1'><?php
        }
        else
        {?>
            <div class='storeindividual2'><?php
        }?>
    You can add multiple CSS elements by doing the following (notice that there is no comma):
    Code:
    <div class="storeindividual1 redbg"> or
    <div class="storeindividual1 yellowbg">
    or you can do this with a small jQuery script:
    Code:
    <script>
    $(document).ready(function()
    {
      //for each row
      $("div.storeindividual1:even").css("background-color", "#cOffee");
      $("div.storeindividual1:odd").css("background-color", "#efefef");
    });
    </script>
    How did you resolve it in the end?
      Reply With Quote

  6. #6
    Senior Member ljackson's Avatar
    Join Date
    Feb 2009
    Posts
    570

    Default 14 Mar 2011 @ 13.41

    hi mate,

    i solved it like you suggested
    Code:
    <div class="storeindividual1 redbg"> or[FONT=monospace]
    [/FONT]<div class="storeindividual1 yellowbg">
    was the easiest way for me
    cheers
      Reply With Quote

  7. #7
    Member janvt's Avatar
    Join Date
    Jan 2011
    Posts
    182

    Default 16 Mar 2011 @ 09.03

    I find inline-IFs easier to read in this case:

    PHP Code:
    print '<div class="storeindividual1 ' . (($count%2==0) ? 'red' 'yellow') . 'bg">'
      Reply With Quote

Similar Threads

  1. Two colour borders on one page?
    By Karti in forum Just Starting Out - Help Me!
    Replies: 4
    Last Post: 8 Feb 2011, @ 19.41
  2. help with code please, trying to get the tab colour to change.
    By ljackson in forum Javascript Libraries
    Replies: 20
    Last Post: 24 Aug 2009, @ 12.38
  3. Water Colour RSS Icon
    By Jack Franklin in forum Imagery, Graphics & Typography
    Replies: 3
    Last Post: 26 May 2009, @ 15.21
  4. Colour Blender
    By Michael in forum Design & Layout
    Replies: 4
    Last Post: 1 Feb 2009, @ 09.43

Tags for this Thread

Posting Permissions

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