Jump to content

Arrays and HTML tables!


Strydaris

Recommended Posts

Ok I am completely new to this whole PHP scripting stuff and really only try to deal with simple scripts and such for my website. It has done me fine until now. I know this is going to be a simple question to answer and probaly has been answerd before but I have spent an hour on this forum and could not find one yet.

 

Anyways here it is...

 

I have an array of lets say something like

 

$my_array = array( 1=> 
		'Numbers',
		'one',
		'two',
		'three',
		'four',
		'five',
		'six',
		'seven',
		'eight',
		'nine',
		'ten',
		'eleven',

 

Now I have been searching for a way to put that array into an html table without any luck.

Want I want to do is have lets say 5 columns and 3 rows where it would be laid out something like this in the end.

 

Numbers|one    |two  |three|four  |

five      |six    |seven|eight|nine |

ten      |eleven|        |        |      |

 

Any help here would be much appiciated!!

Cheers

Link to comment
Share on other sites

Try this and see if it is what your looking for:

 

 

<?php

$my_array = array( 1=> 
		'Numbers',
		'one',
		'two',
		'three',
		'four',
		'five',
		'six',
		'seven',
		'eight',
		'nine',
		'ten',
		'eleven');

$count = 1;

foreach ($my_array as $num){

   echo $num.' | ';
   
   if ($count == 5){ 
      echo '<br>';
      $count = 0;
   }

$count++;   
}


?>

Link to comment
Share on other sites

its closer then I was... lol I think you misunderstood my little illustration up there but for the most part works. I did the little illustration of a table to show that I wanted to use <table></table> in the code.

 

Thanks... now I need to figure out how to get the HTML code in there. I think I do the the variables for the amount of columns and rows I want to use. Something like $rows and $cols. lol i dont know for sure though.

Link to comment
Share on other sites

try this

<?php

$my_array = array( 1=> 
		'Numbers',
		'one',
		'two',
		'three',
		'four',
		'five',
		'six',
		'seven',
		'eight',
		'nine',
		'ten',
		'eleven');

$count = 1;
echo "<table border='1'>";
echo "<tr>";
foreach ($my_array as $num){

   echo "<td>$num</td>";
   
   if ($count == 5){ 
      echo "</tr><tr>";
      $count = 0;
   }

$count++;   
}
//cleanup
while ($count != 1){

   echo "<td> </td>";
   
   if ($count == 5){ 
      echo "</tr><tr>";
      $count = 0;
   }
$count++;   
}


echo "</tr>";
echo "</table>";

?>

 

 

EDIT: added a cleanup for the table ;)

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.