This is an included file on one of my PHP sites on localhost:

<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","root","PASSWORDDELETED"); 
	
//select which database you want to edit
mysql_select_db("myradiostation1"); 

//select the table
$result = mysql_query("select * from presenters");

//grab all the content
while($r=mysql_fetch_array($result))
{
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $showtime=$r["showtime"];
   $dj=$r["dj"];
   $showinfo=$r["showinfo"];
   $presenterimage=$r["presenterimage"];
	echo "  <ol class=\"schedList\">
  <li class=\"\">

    <h4><span>$showtime</span> $dj</h4>
	
	
  
	
  
    <p>$showdesc</p>
  
    <a href=\"http://www.example.com\" class=\"more\">More about example</a>
	
	<img src=\"$presenterpic\" alt=\"Non - Stop Music\" />
  
  </li>";
}
?>

What I'm trying to do is use a switch statement in this code, so that one column is like this:

<li class=\"\">

    <h4><span>$showtime</span> $dj</h4>
	
	
  
	
  
    <p>$showdesc</p>
  
    <a href=\"http://www.example.com\" class=\"more\">More about example</a>
	
	<img src=\"$presenterpic\" alt=\"Non - Stop Music\" />
  
  </li>";

and

<li class=\"test1\">

    <h4><span>$showtime</span> $dj</h4>
	
	
  
	
  
    <p>$showdesc</p>
  
    <a href=\"http://www.example.com\" class=\"more\">More about example</a>
	
	<img src=\"$presenterpic\" alt=\"Non - Stop Music\" />
  
  </li>";

I've already got the li class declared in my CSS, I just need to try and switch it to the test1 class every other record, like this:
1 - no class
2 - test1 css
3 - no class
4 - test1 css

If anyone knows how to do this I'd appreciate the help!

Thanks

Recommended Answers

All 4 Replies

Member Avatar for diafol

Don't quite get what you need. Are you after zebra-striping a list or just selectively styling certain list items?

For the latter:

$output = "\n<ul>";
while($d = mysql_fetch_array($r)){ //create an array of each record in loop
  $cssclass = "normclass"; //default class
  if($d['somefield'] == "somevalue"){
     $cssclass = "myclass"; //if class is special
  }
  $output .= "\n\t<li class=\"$cssclass\">...(content)...</li>";
}
$output .="\n</ul>\n"

For the former:

$output = "\n<ul>";
$ i = 1;
while($d = mysql_fetch_array($r)){ //create an array of each record in loop
  $cssclass = "oddclass"; //default odd class
  if($i%2 == 0){ //check to see if item is even
     $cssclass = "evenclass"; //if class is even
  }
  $output .= "\n\t<li class=\"$cssclass\">...(content)...</li>";
 $i = $i + 1;
}
$output .="\n</ul>\n"

I would like to elaborate his idea of implementing alternate CSS's for the rows.
He just want to change the CSS for the rows coming alternately like they do in the listing pages of wordpress or bbpress etc.
Don't you think a simple if() loop will achieve the same thing here instead of the big switch statements, something like -

<li class=<?php echo if($some_var!='')?"$your_class_name":"" ; ?> >

Its just that simple, if your some condition matches echo the class you want or else don't echo anything.

one more idea for alternate css's like I said above
set some variable to count the number of records (Say ) $i and increment it in each loop, on comparing for multiple's of 2 , echo the CSS class else don't echo anything.something like below -

$i = 0;$str = '';
while($row =mysql_fetch_array($dkf))
{
$str = "<li class=\"";
if($i%2==0) $str.="myclass";
$str.="\"";
$i++;
}
Member Avatar for diafol

I would like to elaborate his idea of implementing alternate CSS's for the rows.
He just want to change the CSS for the rows coming alternately like they do in the listing pages of wordpress or bbpress etc.
Don't you think a simple if() loop will achieve the same thing here instead of the big switch statements, something like -

<li class=<?php echo if($some_var!='')?"$your_class_name":"" ; ?> >

Its just that simple, if your some condition matches echo the class you want or else don't echo anything.

@N18

OK, zebra-striping. This is my solution - note the lack of ternary operators - to avoid confusion. "oddclass" is the default class for the stripe as the two striped list item colours may be different normal list items. However, this could be set to "", as you have done.
My solution returns the full html for the entire list, which I admit isn't always a good thing.

$output = "\n<ul>";
$ i = 1;
while($d = mysql_fetch_array($r)){ //create an array of each record in loop
  $cssclass = "oddclass"; //default odd class
  if($i%2 == 0){ //check to see if item is even
     $cssclass = "evenclass"; //if class is even
  }
  $output .= "\n\t<li class=\"$cssclass\">...(content)...</li>";
 $i = $i + 1;
}
$output .="\n</ul>\n"

However, your solution

$i = 0;$str = '';
while($row =mysql_fetch_array($dkf))
{
$str = "<li class=\"";
if($i%2==0) $str.="myclass";
$str.="\"";
$i++;
}

doesn't seem to do anything. It just overwrites itself with '<li class=\"' at every iteration. Sorry didn't mean to nit-pick. I can see what the script is trying to do, but can't see how that would be very different from mine when complete.

@WS
As N18 says - don't use switch statements!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.