dani33l_87 Posted November 6, 2012 Share Posted November 6, 2012 (edited) Hi, I am working to an exercise and I have a little problem. Make a script that populates a PHP array of 10 times 10 cells. The value in each cell must be the array indexes referencing that cell, multiplied together. e.g. 7*4 = 28 and 0*9=0. Hint: Make you script use e.g. a while loop inside a while loop to fill the array. Now write a second script part, in the same script file, that makes the PHP array being printed in a nice formatted way. For example by using HTML table tags. Again make the script use some kind of loop inside a loop to print the table and take out the values from the array. I make the first part and almost I finish the second but I have some problem with that, I don t understand so well how this suppose to work. This suppose to be the final result: http://zuscience.com/php.png This is the code almost finish but I don t understand how to fix the last part to make it work. <?php //File array section $yStart = 0; $y = $yStart; while( $y < (10 + $yStart)) { $x = 0; while ( $x < 10) { $myArray[$x][$y] = $x * $y; //array entry $x++; } $y++; } //Print Section $x = 0; echo '<table border="1">'; echo '<tr>'; //print vertical index row echo '<td>'; echo 'Index'; echo '<td>'; while ( $x < 10 ){ $x++; } echo '</tr>'; $y = 0; while ( $y < 10 ) { echo '<col with= "30">'; $x = 0; echo '<tr><td>' . $y . '</td>'; //print horisontal index while ( $x < 10) { echo '</td>' $myArray[$x][$y]; //cell content $x++; } echo '</tr>' $y++; } ?> Some sugestions what I am doing wrong? Edited November 7, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270356-array-print-the-table-and-take-out-the-values/ Share on other sites More sharing options...
play_ Posted November 6, 2012 Share Posted November 6, 2012 (edited) A bunch of syntax errors on that code <?php $rows = range(0, 9); $cols = range(0, 9); echo "<table border='1' cellpadding='10'cellspacing='5' align='center'>"; $r = 0; while ($r < count($rows)) { $c = 0; echo "<tr> "; while ($c < count($cols)) { echo "<td>"; echo $rows[$r] * $cols[$c]; echo "</td>"; $c++; } echo "</tr>"; $r++; } echo '</table>'; Edited November 6, 2012 by play_ Quote Link to comment https://forums.phpfreaks.com/topic/270356-array-print-the-table-and-take-out-the-values/#findComment-1390520 Share on other sites More sharing options...
dani33l_87 Posted November 7, 2012 Author Share Posted November 7, 2012 look amazing. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/270356-array-print-the-table-and-take-out-the-values/#findComment-1390893 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.