Dynamically generating headings to columns with their values

Hello fellow PHP newbies and experts alike. I am trying to create a script that generates table headings based on records from a Spreadsheet.

Each of the corresponding excel sheet headings begins as a definer plus a numberical index so you have:

Country1, Country2, Country3 …through to Country50.

For now I am working with the following which is not very efficient:

$vCountries [‘country1’] = “Value of Country 1”;
$vCountries [‘country2’] = “Value of Country 2”;
$vCountries [‘country3’] = “”;
$vCountries [‘country4’] = “Value of Country 4”;

Since all the column names only vary in number how can I generate them as a string for example as an $array[Index] and parse it to the values above?

The next thing to do is to only print the table headings if they contain a value so I am using the following script

if (!empty($value) && strlen($value) > 0) {
$result .= “<th id=‘$key’” .$value. "</th>
";
}

But it is not quite working, how can I improve this?

Thanks!

if you use the function explode which make an array of the whole content,
then you will get easily what you want,
like
$name= here read your file
$makearray=explode(" ",$name);
then acess the heading through
$makearray[o];
i hope it will help you,
if need more alaways welocme

Hey Thanks Mehran but I am using:http://sourceforge.net/projects/phpexcelreader

Which calls the excel file in the following fashion:

[FONT=“Courier New”]

require_once ‘Excel/reader.php’;
error_reporting(E_ALL ^ E_NOTICE);

$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding(‘ASCII’);
$data->read($in);
$items = $data->sheets[0][‘cells’];

fwrite($handle, "
<table>
\ <thead>
\ <tr>");

$data = “”;
$cnt = 0;

foreach($items as $item)
{
if ($cnt == 0)
{
$cnt+=1;
continue;
}

$vCountries = $item[6]; 
$vCities = $item[4]; 
$vStreets= $item[1];[/FONT]

etc…

[FONT="Courier New"]$data .= "My result in HTML";[/FONT]

That’s why I wanted to put everything into:

$vCountries [‘country1’] = $item[12];
$vCountries [‘country1’] =$item[9];
$vCountries [‘country2’] = $item[8];
$vCountries [‘country3’] = “”;
$vCountries [‘country4’] = "$item[13];

But it gets too specific and prone to human error because you have to know exactly which field you need, so I wanted to be able to match field name + index + value and then print them in order IF this field exists. But I don’t know if I’m over complicating things.

I hope you understand what I am trying to say :slight_smile: