PDA

View Full Version : alternating logic needed?


student101
09-06-2008, 11:31 AM
It seems my <li>'s need to be coded by hand not sure.
All I need is the logic behind making the <li>=>divs alternating colors dynamically
<div class="post_body nicelist">
<ol>

<li class="palt"> <!-- background: #eee;-->
<div class="comment_author left">
<span class="comment"><?php echo $row_rsnews['txtHeading']; ?></span></div>
<div class="clearer"></div>
<div class="body">
<p><?php echo nl2br($row_rsnews['memdescription']); ?></p>
</div>
</li> <!--color end background: #eee;-->

<li id="comment-256"> <!--comment num random; background: #fff;-->
<div class="comment_author left">
<span class="comment"><a href="#">Elementum</a></span>
<div class="date"><a href="#">December 21st, 2007 at 12:46 pm</a></div>
</div>
<div class="clearer"></div>
<div class="body">
<p>Comments are cool!</p>
</div>
</li> <!--color end background: #fff;-->

</ol>
</div>


Cheers

student101
09-06-2008, 01:30 PM
Trying this but not looping to next record? any ideas
<?php
$ntotal = $totalRows_rsnews;
for($i=0;$i<$ntotal;$i++){
if($i%2==0){
echo '<li class="palt">'; ?>
<div class="comment_author left">
<span class="comment"><a href="news_view.php?id=<?php echo $row_rsnews['intarticleid']; ?>"><strong><?php echo $row_rsnews['txtHeading']; ?></strong></a></span></div>
<div class="clearer"></div>
<div class="body">
<p><?php echo nl2br($row_rsnews['memdescription']); ?></p>
</div>
</li>
<?php }else{ ?>
<?php echo '<li id="comment-'.$i.'">';?>
<div class="comment_author left">
<span class="comment">Elementum</span>
</div>
<div class="clearer"></div>
<div class="body">
<p>Comments are cool!</p>
</div>
</li>
<?php
}
}
?>

davidj
09-06-2008, 02:02 PM
i think this may help

its an alternate row color changer

i use this method all the time


<?php
/// just data to feed loop
$array = array(
"key1" => "value1",
"key2" => "value2",
"key3" => "value3",
"key4" => "value4",
"key5" => "value5",
"key6" => "value6",
"key7" => "value7",
"key8" => "value8",
"key9" => "value9",
"key10"=> "value10",
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.color1 {
background-color: #FFFF99;
}
.color2 {
background-color: #FF9999;
}
-->
</style>
</head>
<body>
<table width="285" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<?php
/// int count
$c = 1;
/// start loop
foreach($array as $values){
/// divide by 2 creating floats
$calc = $c/2;

/// detect float and swap classes (color)
if(is_float($calc)){

$class = "color1";

}else{

$class = "color2";
}

?>

<!-- $class alternates colour depending on logic above -->
<tr class="<?php echo $class; ?>">

<td><?php echo $values; ?></td>
<td><?php echo $values; ?></td>
<td><?php echo $values; ?></td>
</tr>

<?php

/// increment count
$c++;

/// end foreach brace
}


?>

</table>
</body>
</html>

student101
09-06-2008, 02:48 PM
That looks alot more promising than my idea.
Have to run out, will play later.

Thanks

student101
09-06-2008, 05:26 PM
For this to work with mine, I need it as $query = "SELECT * FROM tblarticles
$array = array(
"key1" => "value1",
"key2" => "value2",
"key3" => "value3",
"key4" => "value4",
"key5" => "value5",
"key6" => "value6",
"key7" => "value7",
"key8" => "value8",
"key9" => "value9",
"key10"=> "value10",
);
Mine also uses DIV's I tried it with this, but still no actual loop.

<?php
/// int count
$c = 1;
/// start loop
foreach($row_rsnews as $values){
/// divide by 2 creating floats
$calc = $c/2;
/// detect float and swap classes (color)
if(is_float($calc)){
$class = "palt";
}else{
$class = "";
}
?>
<!-- $class alternates colour depending on logic above -->
<li class="<?php echo $class; ?>">
<div class="comment_author left">
<span class="comment">><?php echo $row['txtHeading']; ?></strong></span></div>
<div class="clearer"></div>
<div class="body">
<p><?php echo nl2br($row['memdescription']); ?></p>
</div>
</li>
<?php
/// increment count
$c++;
/// end foreach brace
}
?>

I am also trying to understand how this is possible without tables?

davidj
09-06-2008, 05:36 PM
you need to define the dimentions of your <div

then loop through creating <divs under the first and so on

davidj
09-06-2008, 05:42 PM
<?php
/// just data to feed loop
$array = array(
"key1" => "value1",
"key2" => "value2",
"key3" => "value3",
"key4" => "value4",
"key5" => "value5",
"key6" => "value6",
"key7" => "value7",
"key8" => "value8",
"key9" => "value9",
"key10"=> "value10",
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
.color1 {
background-color: #FFFF99;
}
.color2 {
background-color: #FF9999;
}
#row{
width:500px;
height:30px;
}
-->
</style>
</head>
<body>
<table width="285" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>

<?php
/// int count
$c = 1;
/// start loop
foreach($array as $values){
/// divide by 2 creating floats
$calc = $c/2;

/// detect float or not swapping classes (color)
if(is_float($calc)){

$class = "color1";

}else{

$class = "color2";
}

?>

<!-- $class alternates colour depending on logic above -->
<div id="row" class="<?php echo $class; ?>"><?php echo $values; ?></div>

<?php

/// increment count
$c++;

/// end foreach brace
}


?>

</table>
</body>
</html>

student101
09-06-2008, 05:42 PM
My divs are defined in my stylesheet, please explain to what you mean by define?
Yes I see what you mean, but the array() and Select statement are not the same.

davidj
09-06-2008, 05:43 PM
see post above yours

student101
09-06-2008, 05:45 PM
Yes I saw it, but the array() and Select statement are not the same.

$query = "SELECT * FROM tblarticles WHERE bitshow = 'Y' ORDER BY dtedate DESC";

I think I'm going nuts.

student101
09-06-2008, 06:08 PM
I am going to try and explain my logic, then try and develop the code to make this nutty logic a reality.


<style type="text/css">
li.palt {background: #eee;}
</style>

<ol>

<li class="palt">
<div class="comment_author left">
<span class="comment">some_text</span></div>
<div class="clearer"></div>
<div class="body">
<p>some_text</p>
</div>
</li>

</ol>

My Logic;

<style type="text/css">
li.palt {background: #eee;}
</style>

<ol>
<?php do { ?>
<?php $cls = ($cls =='palt' ? '' : 'palt' ); // Switch the background color. ?>
<li class="<?php echo $cls ; ?>">
<div class="comment_author left">
<span class="comment">some_text</span></div>
<div class="clearer"></div>
<div class="body">
<p>some_text</p>
</div>
</li>
<?php } while ($row_rsnews = mysql_fetch_assoc($rsnews)); ?>
</ol>


Wow this works!
D*mn this is retarded!!!

davidj
09-06-2008, 06:22 PM
where you have this


<?php do { ?>
<?php $cls = ($cls =='palt' ? '' : 'palt' ); // Switch the background color. ?>



its better to do this and easier to follow


<?php

do {

$cls = ($cls =='palt' ? '' : 'palt' ); // Switch the background color.

?>

student101
09-06-2008, 06:31 PM
Thanks for that.

You see the code better than me.
I lose myself in my question(s) (what to do with my brain?)

Cheers

student101
09-15-2008, 07:17 PM
Just an update;

Not good to have an empty class;

$cls = ($cls =='palt' ? 'alt' : 'palt' ); // Switch the background color. ?>