0

this is a follow up to another question which I was instructed to split up. I have different set of columns and their values some which have values, some which don't. For example:

$vCountries ['country1'] = "Value of country1";
$vCountries ['country2'] = "Value of country2";
$vCountries ['country3'] = "";
$vCountries ['country4'] = "Value of country4";
$vCountries ['country5'] = "";

...through to

$vCountries ['country50'] = "Value of country50";

I would like to generate the following code example (only when it contains a value):

<th id="country1">Value of country1</th>
<th id="country2">Value of country2</th>
<th id="country4">Value of country4</th>

...

Since all country ID's are generically named with only an index to differ them, I would like these headings to be generated dynamically.

Someone has suggested the following code:

for ($i = 1; $i <= 50; ++$i) {
$vCountries['country' . $i] = "Value of country " . $i;}

But firstly, I don't understand how it is working and how I can parse it to the code at the top because for now it only prints array array array etc. and the "Value of country .$i" is not generic and indexed, it is an actual value different for each country, so I would still have to list it manually, no?

Any ideas?

Thanks

3 Answers 3

2

This will do it.

foreach($vCountries as $id => $value)
{
    if($value)
    {
        echo "<th id=\"$id\">$value</th>";
    }
}

This outputs only entries with values. Each id is output as the <th> id, and each value is displayed inside the <th> tag.

Sign up to request clarification or add additional context in comments.

2 Comments

@Murilo: Next time fix it instead of deleting it. Your answer added value to the conversation, it just needed a little tweak.
Ok thank you! I am new here so I am learning how to use this tool. The guys here are very fast, when I started to write my answer, there was not answers yet, so as you answered I thought my answer is not needed.
0

This generates the code you want :

foreach ($vCountries as $key => $value)
{
    if (strlen($value) > 0)
        echo "<th id=\"$key\">$value</th>";
}

1 Comment

I think that using strlen() isn't good because if $value is big you can get loss of performance.
0

If your goal is to generate the said HTML code from the array, which is already defined, then this may be a solution for you:

// Loop the array
foreach ($vCountries as $key => $value) {
    // If value is not empty
    if (!empty($value)) {
        // display the line
        echo '<th id="'.$key.'">'.$value.'</th>'."\n";
    }
}

1 Comment

Thanks for your quick replies guys, I have tried Surreal Dreams solution and it seems to be working but as a result I have encountered the problem of having to check the excel file (it reads from) if the values are there. So I need to access the cell and check it so it will have to be another question bear with me. This site is confusing :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.