FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


array diplay in a table using php





scot
I want a php program please help me,i am not an expert in php just need little help .

here's a multidimensional array
$arr = array (
array ("a" => "apple", "aeroplane" ),
array ("b" => "bat","ball"),
array ("c" => "cat","catch"),
array ("d" => "dog","doll"));
i want to display it in a table format like:
a aeroplane apple
b bat ball
c cat catch
d dog doll

and
a b c d
aeroplane ball cat dog
apple bat catch doll

please help me thanku so much Smile
Peterssidan
array ("a" => "apple", "aeroplane" ) is an array where "a" maps to "apple" and 0 maps to "aeroplane". There is probably better ways of storing this data.

I think you can foreach useful when printing the table.
sonam
Peterssidan is right there is for sure better way for storing data. This is little bit tricky to get all datas from your array but if you haven't nothing better then you can try this:

Code:
foreach($arr  as $val) {
   $key = array_keys($val);
   sort($val);
      
for($i=0; $i<count($val); $i++) {
if ( $i==0 )
{
$keys =$key[0] ;
} else {
$keys ="";
}
   
if ( $i&1 )
{
$br ="<br />";
} else {
$br ="";
}
          $line = @$line . ' ' . $keys . ' ' . $val[$i] . $br;
      } 
}
   $lines =  @$lines . @$line;

echo $lines;



For vertical table you need little bit more work because you must include (write) html in line definition and of course order of elements.

Sonam
ogah
for this table format:
a aeroplane apple
b ball bat
c cat catch
d dog doll
Code:
echo '<table border="1">';
foreach($arr as $ary) {
   $kyy = array_filter(array_keys($ary));
   sort($ary);
   echo '<tr>';
   foreach($kyy as $ky){
      echo '<td>'.$ky.'</td>';
   }
   foreach($ary as $vl){
      echo '<td>'.$vl.'</td>';
   }
   echo '</tr>';
}
echo '</table>';


and for this format:
a b c d
aeroplane apple ball bat cat catch dog doll
Code:
echo '<table border="1">';
echo '<tr>';
foreach($arr as $ary) {
   $col = count($ary);
   $ary = array_filter($ary);
   foreach($ary as $ky2 => $vl2) {
   if($ky2 != '0') { echo '<td colspan="'.$col.'">'.$ky2.'</td>'; }
   }
}
echo '</tr>';
echo '<tr>';
foreach($arr as $ary) {
   sort($ary);
   foreach($ary as $ky2 => $vl2) {
      echo '<td>'.$vl2.'</td>';
   }
}
echo '</tr>';
echo '</table>';
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
"Drop Down List Box" Using PHP and MySql
Table data print feature using php or javascript?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.