driz Posted April 1, 2009 Share Posted April 1, 2009 Hi please take a look at this website: http://hermes.hud.ac.uk/u0558234/mediadrop/The images in the middle are created using UL and LI's the first of each three LI's has a class of "first"My problem is that this list will be displayed using PHP, how can I get it so that when my list is shown, the first li of every 3 li's will have a class of "first"?The list will be shown using a simple echo '<li>$row</li>' variable from an MySQL database.Thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted April 1, 2009 Share Posted April 1, 2009 You should be able to keep track of which item you're printing. You can use a counter for that, or even just a boolean to say whether or not it's the first one. Link to comment Share on other sites More sharing options...
driz Posted April 1, 2009 Author Share Posted April 1, 2009 Hi, thanks for the reply. Would you be able to post an example please, I learn better when I can see the actual code and see how its constructed. Thank you. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 1, 2009 Share Posted April 1, 2009 $first = true;foreach ($list as $item){ if ($first) echo '<li class="first">'; else echo '<li>'; $first = false; ...} Link to comment Share on other sites More sharing options...
driz Posted April 2, 2009 Author Share Posted April 2, 2009 <?php $host="localhost"; $username="u0559023"; $pass="02dec86"; mysql_connect($host,$username,$pass); mysql_select_db($username); $query="SELECT name, work_location FROM Portfolios, Work WHERE Portfolios.portfolio_id = Work.portfolio_id"; $result=mysql_query($query); echo "<ul id='home-recent-work'>"; $first = true; while($row = mysql_fetch_array($result)){ if ($first) echo '<li class="first">'; else echo '<li>'; $first = false; echo "<h3>".$row['name']."</h3>"; echo "<a href='profile.php?name=".$row['name']."'><img src='".$row['work_location']."'/></a></li>"; } echo "</ul>"; mysql_close(); ?> This is what I have so far, it will add the class to the first LI right? How do I get it so it adds it to the first of every 3 LI'sSo I get this effect: <li class="first"></li><li></li><li></li><li class="first"></li><li></li><li></li><li class="first"></li><li></li><li></li> Link to comment Share on other sites More sharing options...
driz Posted April 2, 2009 Author Share Posted April 2, 2009 I take it I'm going to need a count to do this? Can you please show an example of how I could do this thanks Link to comment Share on other sites More sharing options...
justsomeguy Posted April 2, 2009 Share Posted April 2, 2009 $nr = 0;while (){ if (!($nr++ % 3)) echo '<li class="first">'; else echo '<li>'; } Link to comment Share on other sites More sharing options...
driz Posted April 2, 2009 Author Share Posted April 2, 2009 shouldn't that be? if (!($nr++ % 3)) { echo '<li class="first">';} else{ echo '<li>';} Link to comment Share on other sites More sharing options...
justsomeguy Posted April 2, 2009 Share Posted April 2, 2009 That's the same thing, brackets aren't necessary if a control structure only has a single statement. Link to comment Share on other sites More sharing options...
Ingolme Posted April 2, 2009 Share Posted April 2, 2009 shouldn't that be? if (!($nr++ % 3)) { echo '<li class="first">';} else{ echo '<li>';} Braces are not needed if there is only a single line of code after the if() statement. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.