Jump to content

Array - Print The Table And Take Out The Values


dani33l_87

Recommended Posts

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? :happy-04:

Edited by Zane
Link to comment
Share on other sites

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 by play_
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.