tycy Posted December 4, 2014 Share Posted December 4, 2014 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted December 4, 2014 Share Posted December 4, 2014 (edited) What are you trying to do? If you are trying to change the appearance of alternate rows then a. The value of $nr_row needs to change b. It needs to change within your loop Edited December 4, 2014 by Barand Quote Link to comment Share on other sites More sharing options...
tycy Posted December 4, 2014 Author Share Posted December 4, 2014 That i try to do this,but how i can to do this? i try-it this part of code,and doesn't work.. $div_cls = (($nr_row % 2) == 0) ? 'linksmen' : 'linksmen_s'; Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 4, 2014 Share Posted December 4, 2014 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. Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted December 4, 2014 Share Posted December 4, 2014 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. Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 4, 2014 Share Posted December 4, 2014 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 Quote Link to comment Share on other sites More sharing options...
tycy Posted December 4, 2014 Author Share Posted December 4, 2014 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'; Quote Link to comment Share on other sites More sharing options...
CroNiX Posted December 4, 2014 Share Posted December 4, 2014 This is unnecessary: if($nr_row>=0) { You will always have 0 or more rows. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.