cssfreakie Posted March 11, 2011 Share Posted March 11, 2011 Hi all, I was playing a bit around with results from a database and thought lets give them a nice order in a table, so i thought i set the class per row, so my style sheet can finish it. This is what i got and it works. But some 6th sense whispers me that this can be done smoother. If anyone has ideas or tips let me know i have a lot to learn in php. and after all want to be a good code writer in php. // start table echo '<table class="users"> <th>name</th><th>email</th><th>active</th>'; $odd = 1; $num = 1; while($row = mysqli_fetch_assoc($result)){ // if( $odd == $num%2 ){ $evenodd = 'odd'; $num++; }else{ $evenodd = 'even'; $num++; } // echo'<tr class="'.$evenodd.'"><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['accept'].'</td><tr>'; } echo '</table>'; thanks Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/ Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 It could be condensed a little by using the ternary operator, but there's nothing wrong with the way you have it now. Some people don't like using ternaries; it's all a matter of preference. I assume you'll be using the $odd variable somewhere, but if not it can be removed. // start table echo '<table class="users"> <th>name</th><th>email</th><th>active</th>'; $odd = 1; $num = 1; while($row = mysqli_fetch_assoc($result)) { $evenodd = $num % 2 === 0 ? 'even' : 'odd'; echo'<tr class="'.$evenodd.'"><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['accept'].'</td><tr>'; $num++; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186199 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Your code and logic look good. Is there something in particular that you're not happy with (performance, format, etc.)? Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186200 Share on other sites More sharing options...
mattal999 Posted March 11, 2011 Share Posted March 11, 2011 Well, actually, you can do this in CSS: tr:nth-child(even) { background: #ccc } tr:nth-child(odd) { background: #fff } Then you have no need for the $evenodd variable. // start table echo '<table class="users"><th>name</th><th>email</th><th>active</th>'; while($row = mysqli_fetch_assoc($result)){ echo '<tr><td>'.$row['name'].'</td><td>'.$row['email'].'</td><td>'.$row['accept'].'</td><tr>'; } echo '</table>'; Note that this isn't currently supported by ALL major browsers, but as far as I'm aware it works on Chrome, Firefox, Safari and I think Opera supports it too. Internet Explorer doesn't, but then again you could just use your own code just fine. I would suggest Pikachu's code (above). Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186202 Share on other sites More sharing options...
cssfreakie Posted March 11, 2011 Author Share Posted March 11, 2011 Thanks all, I always designed any website i made to be able to work with ie 5 and up, and since some retards still use it i keep it that way untill MS invites me to the funeral of those retards. That's as far as css goes But i really like the looks of that code pickachu gave. Although i find the ternary operator hard to read. In my mind i have to start at the middle than move back and than forth again. I also used that $odd = 1; because i never worked with the % which is still a bit odd for me since the explanation of it says 25%2 has a remainder of 1 for me 25/2 is 12.5 but i guess i need to round it up. Any ways thanks alot guys, i am getting a little better every day at php: Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186206 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Thanks all, I always designed any website i made to be able to work with ie 5 and up Ugh, must be hell supporting that. Although i find the ternary operator hard to read. In my mind i have to start at the middle than move back and than forth again. Like Pika said, it's all about preference. You compromise either way. One is easier to read while longer and vice versa. Any ways thanks alot guys, i am getting a little better every day at php Good, you'll be an expert in no time Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186209 Share on other sites More sharing options...
cssfreakie Posted March 11, 2011 Author Share Posted March 11, 2011 Thanks all, I always designed any website i made to be able to work with ie 5 and up Ugh, must be hell supporting that. nah ones you know the bugs, it's pretty easy, in fact IE 5 is easier than ie 6 But i do use some extra cool css3 things to award people for having a decent browser. But only for extra's , just like javascript Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186210 Share on other sites More sharing options...
AbraCadaver Posted March 11, 2011 Share Posted March 11, 2011 Thanks all, I always designed any website i made to be able to work with ie 5 and up, and since some retards still use it i keep it that way untill MS invites me to the funeral of those retards. That's as far as css goes But i really like the looks of that code pickachu gave. Although i find the ternary operator hard to read. In my mind i have to start at the middle than move back and than forth again. I also used that $odd = 1; because i never worked with the % which is still a bit odd for me since the explanation of it says 25%2 has a remainder of 1 for me 25/2 is 12.5 but i guess i need to round it up. Any ways thanks alot guys, i am getting a little better every day at php: 25 / 2 = 12 (with a remainder of 1) 12 * 2 + 1 = 25 24 / 2 = 12 1 / 2 = .5 12 * 2 = 24 .5 * 2 = 1 Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186268 Share on other sites More sharing options...
silkfire Posted March 11, 2011 Share Posted March 11, 2011 When using even/odd I always use true and false and keep alternating between them. Also you forgot a <tr> tag for your theads. // start table <table class="users"> <tr> <th>name</th> <th>email</th> <th>active</th> </tr> <? $even = true; while ($row = mysql_fetch_assoc($result)) { ?> <tr class="<?=($even ? 'even' : 'odd')?>"> <td><?=$row['name']?></td> <td><?=$row['email']?></td> <td><?=$row['accept']?></td> <tr> <? $even = !$even; // Invert even to odd and vice-versa. } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186330 Share on other sites More sharing options...
mattal999 Posted March 11, 2011 Share Posted March 11, 2011 When using even/odd I always use true and false and keep alternating between them. Also you forgot a <tr> tag for your theads. // start table <table class="users"> <tr> <th>name</th> <th>email</th> <th>active</th> </tr> <? $even = true; while ($row = mysql_fetch_assoc($result)) { ?> <tr class="<?=($even ? 'even' : 'odd')?>"> <td><?=$row['name']?></td> <td><?=$row['email']?></td> <td><?=$row['accept']?></td> <tr> <? $even = !$even; // Invert even to odd and vice-versa. } ?> </table> Don't use shorttags. Ever. Seriously, I mean ever. Always use <?php. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186332 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 Edit: ^ That Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186333 Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 What he said ^ ^ ^ Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186335 Share on other sites More sharing options...
silkfire Posted March 11, 2011 Share Posted March 11, 2011 <php is fugly and for people who don't understand better. When I switched to short tags I had no intention of going back. Admit my code is neat ^^. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186356 Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 Short tags are deprecated, no longer enabled by default, and lead to code that is less portable. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186360 Share on other sites More sharing options...
silkfire Posted March 11, 2011 Share Posted March 11, 2011 No they're not, Pikachu Short tags are enabled in 95% of webservers. And in the future Zend Framework encourages use of short tags. They've never been deprecated and will never be. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186364 Share on other sites More sharing options...
mattal999 Posted March 11, 2011 Share Posted March 11, 2011 <php is fugly and for people who don't understand better. When I switched to short tags I had no intention of going back. Admit my code is neat ^^. 1. Your code is not neat - I'd say it's neater with the full tag. 2. Shortags are disabled by default in PHP5 and I'm pretty sure they're going to be deprecated soon. They cause issues. They're wrong. You're wrong. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186365 Share on other sites More sharing options...
silkfire Posted March 11, 2011 Share Posted March 11, 2011 1. They don't cause issues for most PHP users and are encouraged in PHP5/6. 2. You're wrong, mate. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186369 Share on other sites More sharing options...
Pikachu2000 Posted March 11, 2011 Share Posted March 11, 2011 Even if it's true that they aren't deprecated *yet*, the fact remains that they are no longer enabled by default, and as a result, do lead to code that is less portable. From the PHP manual: "Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags." Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186374 Share on other sites More sharing options...
Maq Posted March 11, 2011 Share Posted March 11, 2011 1. They don't cause issues for most PHP users and are encouraged in PHP5/6. 2. You're wrong, mate. Maybe not most, but still cause issues. Where did you hear that short tags are encouraged in PHP5/6? I don't believe that at all. Actually, short tags were going to be deprecated in PHP6 but they decided against it for reasons I do not know. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186375 Share on other sites More sharing options...
cunoodle2 Posted March 11, 2011 Share Posted March 11, 2011 No they're not, Pikachu Short tags are enabled in 95% of webservers. And in the future Zend Framework encourages use of short tags. They've never been deprecated and will never be. My favorite part was when the noob tried to tell one of the top 20 programmers on this site that he was wrong.... Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186444 Share on other sites More sharing options...
silkfire Posted March 12, 2011 Share Posted March 12, 2011 No they're not, Pikachu Short tags are enabled in 95% of webservers. And in the future Zend Framework encourages use of short tags. They've never been deprecated and will never be. My favorite part was when the noob tried to tell one of the top 20 programmers on this site that he was wrong.... You're the noob. Let people code as they want as long as it works. I'm just not a big fan of redundant code. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186457 Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2011 Share Posted March 12, 2011 No they're not, Pikachu Short tags are enabled in 95% of webservers. And in the future Zend Framework encourages use of short tags. They've never been deprecated and will never be. My favorite part was when the noob tried to tell one of the top 20 programmers on this site that he was wrong.... It's not like I'm never wrong, because I'm wrong fairly often. But in this instance, I was mostly right and in that regard I stand by what I said. In fact it does seem that short tags have been removed from the deprecation list. But support for them is not enabled by default, leading to problems with code portability. The percentage of users who have problems with short tags will continue to increase as more hosts upgrade php versions, while the percentage of users who have problems with full tag syntax will remain the same, 0%. The question here is really why would you take a chance on something that might work 80% of the time, when you can use something that will work 100% of the time? And in reference to ZF, the closest I can find to a recommendation for using short tags is not a recommendation at all. It's more of a "We no longer recommend it, but if you're so stubborn that you must use short tags, at least have the sense to enable it in the php.ini file, rather than use Zend_Stream_Wrapper()". In our examples, we make use of PHP long tags: <?php. We also favor the use of » alternate syntax for control structures. These are convenient shorthands to use when writing view scripts, as they make the constructs more terse, keep statements on single lines, and eliminate the need to hunt for brackets within HTML. In previous versions, we often recommended using short tags (<? and <?=), as they make the view scripts slightly less verbose. However, the default for the php.ini short_open_tag setting is typically off in production or on shared hosts -- making their use not terribly portable. If you use template XML in view scripts, short open tags will cause the templates to fail validation. Finally, if you use short tags when short_open_tag is off, the view scripts will either cause errors or simply echo PHP code back to the viewer. If, despite these warnings, you wish to use short tags but they are disabled, you have two options: * Turn on short tags in your .htaccess file: 1. php_value "short_open_tag" "on" This will only be possible if you are allowed to create and utilize .htaccess files. This directive can also be added to your httpd.conf file. * Enable an optional stream wrapper to convert short tags to long tags on the fly: 1. $view->setUseStreamWrapper(true); This registers Zend_View_Stream as a stream wrapper for view scripts, and will ensure that your code continues to work as if short tags were enabled. Warning View Stream Wrapper Degrades Performance Usage of the stream wrapper will degrade performance of your application, though actual benchmarks are unavailable to quantify the amount of degradation. We recommend that you either enable short tags, convert your scripts to use full tags, or have a good partial and/or full page content caching strategy in place. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186459 Share on other sites More sharing options...
cssfreakie Posted March 12, 2011 Author Share Posted March 12, 2011 Sorry for my late reply guys I was sleeping And i see this thread is becoming a battlefield between short and long tags Anyway thanks Abracadaver for giving the maths examples. It really helps in understanding this. Ihave done math, but never used that thing ( % ) <php is fugly and for people who don't understand better. When I switched to short tags I had no intention of going back. Admit my code is neat ^^. I just use the long tags since 3 extra letters could potentially save me a lot of time, like you said 95 %, means 5 % where it doesn't work (with .htaccess it seems you can).(i see it here and than on this forum, and when they adjust it to long tags it works) Any way thanks for the code above I Take it with me. And good thing pointing out the <tr> that was indeed missing. (didn't do a w3cvalidation Thanks guys for the help! And the only noob here is me so that settles the above discussion P.s. noobs have the underdog position so i am glad to be one haha Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186539 Share on other sites More sharing options...
Pikachu2000 Posted March 12, 2011 Share Posted March 12, 2011 Ihave done math, but never used that thing ( % ) LOL, if you'd like to look at the manual entry, it's the modulus operator. It's basically the same as all those years ago when you first started to learn how to divide numbers, and rather than start by teaching decimals, they taught that 15/4 was 3, with remainder of 3. Modulus returns that remainder. Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186610 Share on other sites More sharing options...
cssfreakie Posted March 12, 2011 Author Share Posted March 12, 2011 LOL, if you'd like to look at the manual entry, it's the modulus operator. It's basically the same as all those years ago when you first started to learn how to divide numbers, and rather than start by teaching decimals, they taught that 15/4 was 3, with remainder of 3. Modulus returns that remainder. Well i can do the math but it seems they should rewrite the IQ crap or i should pay more attention Any ways thanks for pointing it out, it's carved into my brains now Quote Link to comment https://forums.phpfreaks.com/topic/230346-can-this-code-be-made-smoother/#findComment-1186644 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.