Page 1 of 1

HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 10:31 am
by eanicom
Hello PHP Gurus,

I'm new to php and I'm trying to format the output of a php array into the form of an HTML table format. The Php program calls on a perl script and just takes the output of the perl program and outputs it into the browser. The problem is that I've been having troubles trying to iterate through the php array and formatting the output into an html table. The original output of the program comes out as the following sample:

MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
----------- ----------- ------------ ------------ -------- -------
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N


Below is the php program that calls the perl program and stores the output above into a php variable and then attempts to format into an HTML table. Can somebody assist me? I've been looking everywhere online but i've had no success in trying to successfully execute what i'm attempting. Is this possible?

Code: Select all

  
<?php
function PrintRoaming() {
    include 'config.inc' ;
    ini_set('display_errors', '1');
    error_reporting(E_ALL);
    ini_set('max_execution_time', 300); //300 seconds = 5 minutes

    $output = array(`$PERL $BASEPATH/PrintRoaming.pl`);
    echo "<table border='1'>";
    for ($i=0; $i < count($output); $i++){
        echo "<tr><td>$output[$i]</td></tr>";
    }
    echo "</table>";

}
PrintRoaming();

?>

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 10:36 am
by Celauran
At a glance, that looks like it should work. What's going wrong?

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 10:46 am
by eanicom
Its not really coming out in a proper format. Instead of coming out in an nice and organize table format it comes out all meshed together with a border around it. The output comes out as the following sample:


MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA ------- ------- ------- -------- ----- ---- 20201 Greece Cosmote Launched No N 20205 Greece Vodafone Launched No Y 20210 Greece Wind_TIM Launched Yes N 20404 Netherland Vodafone Launched No N 20408 Netherland KPNMobile_Telefort Launched Yes Y 20412 Netherland KPNMobile_Telefort Launched No Y 20416 Netherland T-Mobile NI (Orange) Launched No Y 20420 Netherland T-Mobile NI (Orange) Launched No Y 20601 Belgium Proximus (Belgacom) Launched Yes Y 20610 Belgium Mobilestar (Orange) Launched No Y 20620 Belgium Base KPN Launched Yes Y 20801 France OrangeF Launched Yes Y 20810 France SFR(Vodafone) Launched Yes Y 20820 France Bouygues Telecom Launched Yes Y 21401 Spain Vodafone Launched Yes Y 21403 Spain RetevisionMovil,Amena Launched Yes N 21407 Spain Movistar Launched Yes Y 21601 Hungary Telenor (Pannon) Launched No N 21630 Hungary T-Mobile H (Westel) Launched Yes Y 21670 Hungary Vodafone Launched No N 21890 Bosnia GSMbih Launched No Y 21901 Croatia Criatian Telecom Launched Yes N 22001 Montenegro Mitel Launched No N 22210 Italy Vodafone Omnitel Launched Yes N 22288 Italy Wind Launched Yes Y 22601 Romania Vodafone_Connex Launched No N 22610 Romania Orange Launched No Y 22801 Switzerland Swisscom GSM Launched Yes Y 22802 Switzerland Sunrise Launched No Y 22803 Switzerland Orange Launched No Y 23002 Czech Republic Eurotel Praha Launched Yes Y 23101 Slovak Republic Orange Sk Launched No N 23102 Slovak Republic Eurotel GSM Launched No Y 23201 Austria A1(Mobilicom) Launched No Y 23203 Austria T-Mobile Launched No Y 23205 Austria Orange (One) Launched No N 23207 Austria Tele.Ring (TMO) Launched No N 23403 Jersey Airtel Launched No N 23410 UK O2 Launched Yes Y 23415 UK Vodafone Launched Yes N 23420 UK Hutchison (3G) Launched Yes N 23430 UK T-Mobile UK Launched No N 23433 UK Orange Launched Yes Y 23450 Jersey Jersey Telecom Launched No Y 23455 Guernsey Cable & Wireless Launched Yes Y 23458 Isle of Man Pronto GSM (Manx) Launched No Y 23801 Denmark TDC Mobil Launched No N 23802 Denmark Telenor Launched No Y 23820 Denmark Telia DK Launched No Y 23830 Denmark Telia(Former Orange) Launched No Y 24001 Sweden Telia Sonera Mobile Launched Yes Y 24008 Sweden Vodafone (Telenor) Launched No Y 24201 Norway Telenor Launched No Y 24202 Norway Netcom Launched No Y 24205 Norway Network Mobile Launched No Y 24403 Finland DNA Ltd (former Finnet) Launched No Y 24405 Finland Elisa Launched No N 24412 Finland DNA Ltd (former Finnet) Launched No N 24414 Finland Alands Mobiltelefon Launched No N 24421 Finland Elisa Launched No Y 24491 Finland Sonera Launched No Y 24601 Lithuania Omnitel Launched No Y 24602 Lithuania BiteGSM(UAB) Launched No N 24701 Latvia LMT

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 10:49 am
by Celauran
What is the output of var_dump($output[0])?

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 11:00 am
by eanicom
When i execute the php program using var_dump in the command terminal i get the following sample but a little bit more organized and lined up. Its comes out like below because of the paste on this site. I wonder if it has something to do with the perl program output. The perl program output on the command terminal comes out nice and organized.


array(1) {
[0]=>
string(39784) "MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
------- ------- ------- -------- ----- ----
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N
20408 Netherland KPNMobile_Telefort Launched Yes Y
20412 Netherland KPNMobile_Telefort Launched No Y
20416 Netherland T-Mobile NI (Orange) Launched No Y
20420 Netherland T-Mobile NI (Orange) Launched No Y
20601 Belgium Proximus (Belgacom) Launched Yes Y
20610 Belgium Mobilestar (Orange) Launched No Y
20620 Belgium Base KPN Launched Yes Y
20801 France OrangeF Launched Yes Y
20810 France SFR(Vodafone) Launched Yes Y
20820 France Bouygues Telecom Launched Yes Y
21401 Spain Vodafone Launched Yes Y
21403 Spain RetevisionMovil,Amena Launched Yes N
21407 Spain Movistar Launched Yes Y
21601 Hungary Telenor (Pannon) Launched No N
21630 Hungary T-Mobile H (Westel) Launched Yes Y
21670 Hungary Vodafone Launched No N
21890 Bosnia GSMbih Launched No Y
21901 Croatia Criatian Telecom Launched Yes N
22001 Montenegro Mitel Launched No N
22210 Italy Vodafone Omnitel Launched Yes N
22288 Italy Wind Launched Yes Y
22601 Romania Vodafone_Connex Launched No N
22610 Romania Orange Launched No Y
"
}

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 11:05 am
by Celauran
Looks like it's tab-spaced and using new lines for the terminal output. Replacing the newlines with '</tr><tr>' is trivial, but getting the table cells right will take a little more work.

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 11:24 am
by eanicom
The perl program just opens a file that is already generated as such sample below and prints it out in the terminal as below. I wanted the php program to just take that sample output of the perl program and place it on the browser but formatted into table format. Do you believe it has something to do with the way the file is formatted? Perhaps the file needs to be formatted in a different way in order for the php program to format into html table?


[text]
MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
------- ------- ------- -------- ----- ----
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N


[/text]

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 11:28 am
by eanicom
I forgot to mention here is a sample of the perl program. You'll notice that the logic is simple. It ftps into a server and obtains the file sample as below. Then, it opens the file and iterates through it and prints it out in the terminal.

[text]
MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
------- ------- ------- -------- ----- ----
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N
20408 Netherland KPNMobile_Telefort Launched Yes Y
20412 Netherland KPNMobile_Telefort Launched No Y

[/text]


[text]
#!/usr/local/bin/perl -w
use strict;
use Net::FTP;

sub getRoamingList{
my $ftp=Net::FTP->new("xxx.xx.x.xxx",Timeout=>36000) or die "ftp session failed for xxx.xx.x.xxx\n";
$ftp->login("omcadmin","0mcadmin") or die "cannot connect to xxx.xx.xx.xxx\n";
$ftp->binary;
$ftp->cwd('/export/home/omcadmin/EMSC/EGTT') or die "can't change to directory /export/home/omcadmin/EMSC/EGTT\n";
$ftp->get("Web_Roaming");
$ftp->quit;
}
getRoamingList();
sub PrintRoaming{
my @directory = 'Web_Roaming';
open(Roaming_List,"@directory") or die "Can't open Roaming_List: $!\n";
if (@directory){
foreach (<Roaming_List>){
print "$_";
}
}else{ print "Roaming List Not Found\n"; die;}
close Roaming_List;
}
PrintRoaming();

[/text]

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 12:25 pm
by Celauran
I'd open the file directly in a text editor and have it display non-printing characters to see exactly how it's formatted. From there, you could probably come up with some preg_replace() rules to insert table cells.

Re: HTML Table From Php Array Output

Posted: Wed Sep 28, 2011 1:32 pm
by eanicom
Hello I tweaked the php program as so:

Code: Select all

<?php
function PrintRoaming() {
    include 'config.inc' ;
    ini_set('display_errors', '1');
    error_reporting(E_ALL);
    ini_set('max_execution_time', 300); //300 seconds = 5 minutes
    `$PERL $BASEPATH/getRoamingList.pl`;
    
    $file_handle = fopen("Web_Roaming", "r");
    if ($file_handle){
        echo "<table align='center' border='3'>";
        while (!feof($file_handle)) {
            $line_of_text = fgets($file_handle);
            echo "<tr><td>$line_of_text</td></tr>";
        
        }
        echo "</table>";
    }
    fclose($file_handle);

}
PrintRoaming();

?>

And now i get the output close to what i want but I'd like to have 6 columns. The colums would be setup as MCC MNC, COUNTRY, CARRIER, POSTPAID, CAMEL, and then DATA. And the rest would line up underneath each column.

This is the sample output i get after i messed around with the php code some more:

[text]
MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
------- ------- ------- -------- ----- ----
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N
20408 Netherland KPNMobile_Telefort Launched Yes Y
20412 Netherland KPNMobile_Telefort Launched No Y
20416 Netherland T-Mobile NI (Orange) Launched No Y
20420 Netherland T-Mobile NI (Orange) Launched No Y
20601 Belgium Proximus (Belgacom) Launched Yes Y
20610 Belgium Mobilestar (Orange) Launched No Y
20620 Belgium Base KPN Launched Yes Y
20801 France OrangeF Launched Yes Y
20810 France SFR(Vodafone) Launched Yes Y
20820 France Bouygues Telecom Launched Yes Y
21401 Spain Vodafone Launched Yes Y
21403 Spain RetevisionMovil,Amena Launched Yes N
[/text]

This is the final product i want but in HTML table format:

[text]
MCC MNC COUNTRY CARRIER POSTPAID CAMEL DATA
------- ------- ------- -------- ----- ----
20201 Greece Cosmote Launched No N
20205 Greece Vodafone Launched No Y
20210 Greece Wind_TIM Launched Yes N
20404 Netherland Vodafone Launched No N
20408 Netherland KPNMobile_Telefort Launched Yes Y

[/text]