Jump to content

Placing Array Results Into Html Tables


runman

Recommended Posts

<p>Hi,

 

I am currently working on creating a code that grabs information from an array and places them into an html table. The only extra part is that I also have to separate the information within the array where there are colons and put them into separate table columns. I was able to create the code below, but just cant seem to get the tables to align or fit in the correct places. Any help or suggestion is really appreciated and thank you guys in advance.




			
		
Link to comment
https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/
Share on other sites

sorry about that. the bbcodes dont want to work for me. so I just removed the php tags.

 

/////////////

$nocolon = $colon = "";

 

$feat = array(

"feat1" => "nocolon1:colog1",

"feat2" => "nocolon2:colog2",

"feat3" => "nocolon3:colog3",

"feat4" => "nocolon4:colog4",

);

 

foreach ($feat as $val){

$nocolon .= strstr($val, ':', true);

$nocolon = $nocolon . "<br />";

 

$colon .= strstr($val, ':', false);

$colon = $colon . "<br />";

}

 

$nocolon = "<br />$nocolon";

$nocolon = str_replace("<br />", "<tr><td>", $nocolon);

$colon = str_replace("<br />", "</td><td>", $colon);

 

echo <<<_END

<table border="1" border-spacing="0px" border-spacing="0px" >

$nocolon $colon</td></tr>

</table>

_END;

/////////////

 

 

Thanks.

I am really not following your code, so not sure what you are really trying to accomplish.

 

But, taking your original request, start with this

 

<?php

$feat = array(
   "feat1" => "nocolon1:colog1",
   "feat2" => "nocolon2:colog2",
   "feat3" => "nocolon3:colog3",
   "feat4" => "nocolon4:colog4",
);

echo "<table border='1'>\n";
foreach($feat as $name => $dataAry)
{
   echo "<tr>\n";
   echo "<td>{$name}</td>\n";
   foreach(explode(':', $dataAry) as $data)
   {
    echo "<td>{$data}</td>\n";
   }
   echo "</tr>\n";
}
echo "</table>\n";

?>

Hey Psycho,

 

Thanks. Thats almost what I needed. I just have to be to place those results into a varibale. The thing is that I am writting the result to a page.

Pretty much i when I echo a single variable it should be equal to the resuts that I get now.

 

Thank you.

So append the strings to a variable rather than echo them.

<?php
$tableData = '';
foreach($feat as $name => $dataAry)
{
   $tableData .= "<tr>\n";
   $tableData .= "<td>{$name}</td>\n";
   foreach(explode(':', $dataAry) as $data)
   {
       $tableData .= "<td>{$data}</td>\n";
   }
   $tableData .= "</tr>\n";
}
?>
<html>
<body>
<table border='1'>
<?php echo $tableData; ?>
</table>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.