Jump to content

Condition for if to create div's with 2 different class in css.


tycy

Recommended Posts

Hello all

 

i have this code

<?php
$sql = "SELECT DISTINCT phonemodel FROM iphone ORDER BY phonemodel DESC LIMIT 4";
$rows = $conn->sqlExec($sql);

$nr_row = $conn->num_rows;
if($nr_row>=0) {
  $div_cls = (($nr_row % 2) == 0) ? 'linksmen' : 'linksmen_s';
  foreach($rows as $row) {
    echo '<div class="'.$div_cls.'" title="'.$row['phonemodel'].'"><a href="iphone.php?phonemodel='.$row['phonemodel'].'"><img src="images/mark.png" alt="" /> '.$row['phonemodel'].'</a></div>';
	 }
}
  ?>

And looks like,the code take only firs class (linksmen) ,the condition else not working.did someone what is wrong.

 

Thx :)

Link to comment
Share on other sites

You're defining $div_cls outside the loop so it's only going to have 1 value. If you're trying to alternate background colors for the table rows, do yourself a favor and use CSS :nth-child(2). Otherwise you'll have to increment a value within your foreach loop and test the modulo value of that on each iteration.

Link to comment
Share on other sites

That line of code needs to go inside the foreach loop. But you do not want to use the $nr_row for the condtion. Instead you need to create a counter. To do so add   $i = 0;  before the foreach loop then in place of   $nr_row  you'd use    ++$i

 

This will increment $i by one on very iteration of loop. That line of code will now toggle between linksmen and linksmen_s classes on every iteration.

Link to comment
Share on other sites

You can also use the index in the foreach() instead of manually incrementing a counter

foreach($rows as $count => $row) //Use array index called $count
{
  $div_cls = (($count % 2) == 0) ? 'linksmen' : 'linksmen_s'; //use the $count to determine odd/even and apply classname for each
  echo '<div class="'.$div_cls.'" title="'.$row['phonemodel'].'"><a href="iphone.php?phonemodel='.$row['phonemodel'].'"><img src="images/mark.png" alt="" /> '.$row['phonemodel'].'</a></div>';
}

This can also be accomplished using pure css, a single classname for each of those divs (instead of 2 separate ones) and using odd/even pseudoselectors.

http://www.w3.org/Style/Examples/007/evenodd.en.html

Link to comment
Share on other sites

Ch0cu3r thx, your ideea was working.So the code now is like this.maybe someone will need an example.

nr_row = $conn->num_rows;
$i = 0;
if($nr_row>=0) {
  foreach($rows as $row) {
    $i++;
    $div_cls = (($i % 2) == 0) ? 'linksmen_s' : 'linksmen';
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.