RopeADope Posted February 1, 2010 Share Posted February 1, 2010 Hi all, this may be in the wrong forums so mods feel free to move it. I'm just looking for some critique on this function I wrote. Keep in mind that I'm a novice ;-) ################################################## # Creates a Table # ################################################## <?php $rows=5; $columns=5; function createTable($rows,$columns){ echo "<table>"; for($x=0;$x<$rows;$x++){ echo "<tr>"; for($i=0;$i<$columns;$i++){ echo "<td>space</td>"; } echo "</tr>"; } echo "</table>"; } ?> <?php createTable($rows,$columns)?> Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted February 1, 2010 Share Posted February 1, 2010 Nothing wrong with this. Quote Link to comment Share on other sites More sharing options...
RopeADope Posted February 1, 2010 Author Share Posted February 1, 2010 Nothing wrong with this. Is there any way to make this more efficient, or is this about as bare bones as this sort of function can get? Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted February 1, 2010 Share Posted February 1, 2010 I don't know about making it more efficient, but you could add an optional third parameter to suppply a multidimensional array of data and insert it on the fly Quote Link to comment Share on other sites More sharing options...
RopeADope Posted February 1, 2010 Author Share Posted February 1, 2010 I don't know about making it more efficient, but you could add an optional third parameter to suppply a multidimensional array of data and insert it on the fly Thanks for the suggestion. That's actually what I plan on doing next but I'm not very well versed in arrays. I'm trying to get it to the point where the table created has prepopulated data from a database and is editable. I already have a database connector, but would you mind giving an example of how the multidimensional array would work with this? A link works as well if you have one :-) Quote Link to comment Share on other sites More sharing options...
dpacmittal Posted February 1, 2010 Share Posted February 1, 2010 <?php $rows=5; $columns=5; function createTable($rows,$columns,$array){ echo "<table>"; for($x=0;$x<$rows;$x++){ echo "<tr>"; for($i=0;$i<$columns;$i++){ echo "<td>$array[$x][$i]</td>"; } echo "</tr>"; } echo "</table>"; } ?> You also have to check if the array is multidimensional or not. A more efficient is not sending the rows and columns and using foreach instead. <?php function createTable($array){ echo "<table>"; foreach($array as $firstkey){ echo "<tr>"; foreach($firstkey as $value){ echo "<td>$value</td>"; } echo "</tr>"; } echo "</table>"; } /*Demo*/ createTable(array(array('John','24','Washington'),array('Frank','26',''NYC))); ?> 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.