This is a code for my radio station schedule, which works well, no error messages:

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

//select the table
$result = mysql_query("select * from schedule order by ID");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   //the format is $variable = $r["nameofmysqlcolumn"];
   //modify these to match your mysql table columns
   $image=$r["image"];
   $airtime=$r["airtime"];
   $show=$r["show"];
   $showinfo=$r["showinfo"];
   $showinfo1=$r["showinfo1"];
echo "

  <li class=''>

    <h4><span>$airtime</span> $show</h4>
	
	
  
	
  
    <p>$showinfo</p>
  
    <a href='http://' class='more'>More about $showinfo1</a>
	
$image


  
  </li>";
}
  ?>

However, what I'm trying to achieve with the <li> bit is like this:

<li class="">

<h4><span>MIDNIGHT</span> <a href="http://">Presenter1</a></h4>

<p>Description</p>

<a href="http://" class="more">More about Presenter1</a>

<img src="http://" alt="Image" />


</li>
<li class="alt">

<h4><span>6:00 AM</span> <a href="presenter2.php">Presenter2</a></h4>

<p>Description</p>

<a href="http://" class="more">More about Presenter2</a>

<img src="http://" alt="Presenter2" />


</li>

yet I am not sure how to do this in the above file (which, by the way, is included in my radio station schedule pages).

How would I be able to do it so it was
li class=
then
li class="alt"
like the example above within my echo statement?

All help is appreciated - I have tried to do this, but can't figure out how to get it to work!

Recommended Answers

All 7 Replies

You want a different class for the even and odd rows ? Use a counter in the while loop (initialized by zero), and increment it at the end.

You can then use:

$class = ($counter % 2) == 0 ? '' : 'alt';

and use $class in the echo. (it is either empty, or alt.)

I tried your code, and put $class into the echo like this:

$counter = 2;
   $class = ($counter % 2) == 0 ? '' : 'alt';

echo "

  <li class='$class'>

yet it displays:

<li class=''>

<h4><span>MIDNIGHT</span> <a href="http://">Presenter1</a></h4>

<p>Description</p>

<a href='http://' class='more'>More about Presenter1</a>

<img src="myimage.jpg" alt="test">

</li>

<li class=''>

<h4><span>6:00 AM</span> <a href="http://">Presenter 2</a></h4>

<p>Presenter 2 description</p>

<a href='http://' class='more'>More about Presenter 2</a>

</li>

How do I get this to work properly, what values should I use?

Thanks for the initial suggestion, I'd never thought of it before!

Before the while: $counter = 0; Before the echo, only the $class = ... Just before the close bracket of the while: $counter++;

Is this correct?:

$counter = 0;
   $class = ($counter % 2) == 0 ? '' : 'alt';
   $counter++;
 }
echo "

  <li class='$class'>

    <h4><span>$airtime</span> $show</h4>
	
	
  
	
  
    <p>$showinfo</p>
  
    <a href='http://www.starbristol.com/dj/non--stop-music-238180' class='more'>More about $showinfo1</a>
	
$image


  
  </li>";
  ?>
<?php
  mysql_connect("localhost","root","PASSWORD"); 
  mysql_select_db("radio1"); 
  $result = mysql_query("select * from schedule order by ID");
 
  $counter = 0;
  while($r=mysql_fetch_array($result))
  {	
    $image=$r["image"];
    $airtime=$r["airtime"];
    $show=$r["show"];
    $showinfo=$r["showinfo"];
    $showinfo1=$r["showinfo1"];
    $class = ($counter % 2) == 0 ? '' : 'alt';
    echo "<li class='$class'><h4><span>$airtime</span> $show</h4><p>$showinfo</p><a href='http://' class='more'>More about $showinfo1</a>$image</li>";
    $counter++;
  }
?>

How would I use it with this code:

<tr class="rowOdd">
			<td width="15%" style="padding:2px;">12:00AM</td>
			<td width="15%" style="padding:2px;">1:00AM</td>
			<td width="70%" style="padding:2px;"><a href="/showdj.php?id=1" target="_top">Presenter</a></td>

		</tr>
		<tr class="rowEven">
			<td width="15%" style="padding:2px;">1:00AM</td>
			<td width="15%" style="padding:2px;">6:00AM</td>
			<td width="70%" style="padding:2px;"><a href="/showdj.asp?id=2" target="_top">Presenter</a></td>
		</tr>

where it's RowOdd and RowEven for the values (note $airtime, $airtime1 and $show are the variables in this case).

$class = ($counter % 2) == 0 ? 'rowEven' : 'rowOdd';

echo "<tr class='$class'><td>$airtime</td><td>$airtime1</td><td><a>$show</a></td></tr>";

I think you can fill in the details yourself.

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.