Results 1 to 7 of 7
Thread: alternating div bg colour for each record in db
-
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
and its the <div class='storeindividual1'> which i want to replace with the relivant bg changes.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
}
my css is
can anyone help with this please?Code:.storeindividual1{width:933px;float:left;}
cheers
Lukewww.kernow-connect.com - follow on Twitter
-
12 Mar 2011 @ 04.58 Keep a running total of the results. the mod (remainder) operator can be used to detect odds and evens.
It will be one for odds, two for evens.Code:num%2
-
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
which returns 1 for odd and 0 for even which is fine,Code:print ($count%2)
so i have modified my code to check the result of the mod like so
but can i add another class to the above like soCode:if($count%2 == 1) {?> <div class='storeindividual1'><?php } else {?> <div class='storeindividual1'><?php }?>
for example to add background styling to the divs?Code:<div class='storeindividual1,redbg'> or <div class='storeindividual1,yellowbg'>
cheers matewww.kernow-connect.com - follow on Twitter
-
13 Mar 2011 @ 12.24 ok sorted it mate thanks
www.kernow-connect.com - follow on Twitter
-
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:
You can add multiple CSS elements by doing the following (notice that there is no comma):Code:if($count%2 == 1) {?> <div class='storeindividual1'><?php } else {?> <div class='storeindividual2'><?php }?>
or you can do this with a small jQuery script:Code:<div class="storeindividual1 redbg"> or <div class="storeindividual1 yellowbg">
How did you resolve it in the end?Code:<script> $(document).ready(function() { //for each row $("div.storeindividual1:even").css("background-color", "#cOffee"); $("div.storeindividual1:odd").css("background-color", "#efefef"); }); </script>
-
14 Mar 2011 @ 13.41 hi mate,
i solved it like you suggested
was the easiest way for meCode:<div class="storeindividual1 redbg"> or[FONT=monospace] [/FONT]<div class="storeindividual1 yellowbg">

cheerswww.kernow-connect.com - follow on Twitter
-
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">';
Similar Threads
-
Two colour borders on one page?
By Karti in forum Just Starting Out - Help Me!Replies: 4Last Post: 8 Feb 2011, @ 19.41 -
help with code please, trying to get the tab colour to change.
By ljackson in forum Javascript LibrariesReplies: 20Last Post: 24 Aug 2009, @ 12.38 -
Water Colour RSS Icon
By Jack Franklin in forum Imagery, Graphics & TypographyReplies: 3Last Post: 26 May 2009, @ 15.21 -
Colour Blender
By Michael in forum Design & LayoutReplies: 4Last Post: 1 Feb 2009, @ 09.43



LinkBack URL
About LinkBacks











Hello every one
Hi, I am new on this forum and saying hello.