![]() |
php mod % 3 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: php mod % 3 (/showthread.php?tid=3727) |
php mod % 3 - El Forum - 10-18-2007 [eluser]webdezzo[/eluser] Hey all, This may or may not be a CI question (CI may have something useful to use for this) But, since this community rocks, I figured what the heck. Lets say I have a 3 column layout like so: Code: <div id='column1'> And I select some data from my Database, that may return any number of records. I want to split this data into 3 arrays, that are equal (close plus or minus 1 due to odd / even) in length, so that echo the data from the 3 diffrent arrays in each column respectively. How could one go about doing this? I would typically handle it if it were 2 columns with the mod function (% 2) but I am a little thrown off with the third column. I figure splitting the array based on the data would be the easiest method. Does CI have any easy array splitting functionality that I could say, take this data, split it into 3 arrays by taking the returned records and dividing them by 3? Any ideas would be much appreciated! Thanks! php mod % 3 - El Forum - 10-18-2007 [eluser]Majd Taby[/eluser] why don't you use modulo 3 ( % 3) ? php mod % 3 - El Forum - 10-18-2007 [eluser]John_Betong[/eluser] [quote author="Zaatar" date="1192777278"]why don't you use modulo 3 ( % 3) ?[/quote] Why don't you use modulo 5 ( % 5)[b] ? [b]controller.php Code: ... View: _five_col.php Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> php mod % 3 - El Forum - 10-18-2007 [eluser]Majd Taby[/eluser] John_Betong, the switch statement in your controller is completely unnecessary. In my experience, whenever you duplicate code like that, there's probably a better way to do it: Code: $data['a'.($row->id % 5)][] = anchor('/joke/show/'. $row->id, sprintf('M %s', $row->id, $row->title)); That should work just as well. php mod % 3 - El Forum - 10-19-2007 [eluser]John_Betong[/eluser] Many thanks Zaatar for your suggestions, Here are the results: http://johns-jokes.com/joke/five_col Code: foreach($result->result()as $row): php mod % 3 - El Forum - 10-29-2007 [eluser]Pygon[/eluser] Happened to this from your other page... I'm more concerned with how much slower your page processes because you calculate the modulus of almost 1000 numbers. Code: $i = 0; is faster, taking about 1/3rd of the time. php mod % 3 - El Forum - 10-31-2007 [eluser]John_Betong[/eluser] Hi Pygon, I was curious to see how much faster your code was so took the trouble to modify my code: contoller Code: // Benchmark different methods of calculating links Test results shown in the title ("Five column using $i++ Elapsed time ==>"): using++ using %5 Cheers, John_Betong php mod % 3 - El Forum - 10-31-2007 [eluser]Pygon[/eluser] Hi John -- I took a further look at my results and it seemed I had some stuff out of whack with my benchmarking system. I tore it all up and rewrote it again -- modulos seems to be the faster of the two, as well as !empty() over strlen()>0 and $i++ over $i+=1, in case you wanted to know, haha. Anyway, my appologies. php mod % 3 - El Forum - 10-31-2007 [eluser]John_Betong[/eluser] Hi Pygon, Many thanks for your code I learnt how easy it was to use the BenchMark Class, This will no doubt come in handy in the future. I would not have imagined Modulos to be much faster and think it was the repeated if test to see if $i==0 was the time grabber. Empty() just tests for Null, 0 or an empty string whereas strnlen() should test and cater for string lengths, etc. Maybe also allocate memory for the counter. A long time ago I read why $i++ is faster than $i+=1 and think the reason is something to do with just shifting a bit in the byte whereas the +1 must use the addition routine. I have just remembered the good old days and having to write a multiplication routine using Z80 Assembler - no mean task! Aren't computers fun ![]() Cheers, John_Betong php mod % 3 - El Forum - 10-09-2008 [eluser]mdriscol[/eluser] Hey, is there a way I can do this without putting it into html in the controller. I want 3 columns but I don't want to write it as a row and html. |