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.




			
				


	Edited  by runman
	
	

			
		
Link to comment
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.

Link to comment
Share on other sites

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";

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

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.