erme Posted June 29, 2010 Share Posted June 29, 2010 Basically have this code to list files in a folder (currently pointing to root /) I have added the is_odd function to try and get the files to be displayed in 2 columns via css. The question is how can I get the odd and even colums to be nested in a <div id="left"></div> and<div id="right"></div> so that I can stye them? <?php function is_odd($number) { return $number & 1; // 0 = even, 1 = odd } $dirname = "/"; $count = 0; if ($dir = opendir($dirname)) { while(false !== ($file = readdir($dir))) { echo "<div id='left'>"; $count = $count + 1; if (is_odd($count)) { if(($file != ".") and ($file != "..")) { echo "<a style='float:left;border:1px solid red;' href='$dirname$file'>$file</a><br />\n"; } echo "</div>"; } else { if(($file != ".") and ($file != "..")) { echo "<a style='float:right;border:1px solid red;' href='$dirname$file'>$file</a><br />\n"; } } } closedir($dir); } else { echo "could not open dir: $dirname<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/206154-is_odd-for-2-columns-via-css/ Share on other sites More sharing options...
kickstart Posted June 29, 2010 Share Posted June 29, 2010 Hi Presume you mean something like this:- <?php function is_odd($number) { return $number & 1; // 0 = even, 1 = odd } $dirname = "/"; $count = 0; if ($dir = opendir($dirname)) { while(false !== ($file = readdir($dir))) { if(($file != ".") and ($file != "..")) { $count++; if (is_odd($count)) { echo "<div class='leftDiv'><a style='float:left;border:1px solid red;' href='$dirname$file'>$file</a><br /></div>\n"; } else { echo "<div class='rightDiv'><a style='float:right;border:1px solid red;' href='$dirname$file'>$file</a><br /></div>\n"; } } } closedir($dir); } else { echo "could not open dir: $dirname<br>"; } ?> However I would think you would also want a div to surround each row. This would be easy to add (add an open div to the first echo and a close div to the 2nd echo), but you would need to add a close div outside the loop when there were an uneven number of items. Personally I would probably use modulus 2 (ie % 2) and to determine if it was an odd or even number. Makes it simple to change to cope with 3 or more columns. All the best Keith Link to comment https://forums.phpfreaks.com/topic/206154-is_odd-for-2-columns-via-css/#findComment-1078629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.