runman Posted November 16, 2012 Share Posted November 16, 2012 <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 More sharing options...
MDCode Posted November 16, 2012 Share Posted November 16, 2012 explode You aren't showing any code...unless that is it Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393000 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. Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393003 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. Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393004 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"; ?> Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393020 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. Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393029 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> Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393032 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. Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393035 Share on other sites More sharing options...
Psycho Posted November 16, 2012 Share Posted November 16, 2012 ** Marking solved ** Link to comment https://forums.phpfreaks.com/topic/270794-placing-array-results-into-html-tables/#findComment-1393036 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.