runman Posted November 16, 2012 Share Posted November 16, 2012 (edited) <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 November 16, 2012 by runman Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 16, 2012 Share Posted November 16, 2012 (edited) explode You aren't showing any code...unless that is it Edited November 16, 2012 by SocialCloud Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 Yeah, you need to show a sample of the data in the array and give a better explanation of what you are trying to achieve (such as an image). Kinf of hard to know what doesn't "fit" from your description. Quote Link to comment Share on other sites More sharing options...
runman Posted November 16, 2012 Author Share Posted November 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 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"; ?> Quote Link to comment Share on other sites More sharing options...
runman Posted November 16, 2012 Author Share Posted November 16, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 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> Quote Link to comment Share on other sites More sharing options...
runman Posted November 16, 2012 Author Share Posted November 16, 2012 Pshyco you are the man. Thats exactly what I needed. Any way I could keep you as a contact. Im sure I will need your help again in the future. Thanks a lot. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 ** Marking solved ** Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.