0

I have this script that reads a list of domains from an external .ini file and transforms them into a list of links:

<?php
$listSeparator = ",";
$lines = file('list.ini');
foreach ($lines as $line) {
    $listvalues = explode('=',$line);

    echo implode("<br />",array_map("add_link",explode($listSeparator,str_replace(' ', '', $listvalues[1]))));
}

function add_link($n)
{
    return "<p><a href=\"$n\">$n</a></p>";
}
?>

what I am trying to achieve is having two outputs (odd/even), starting with the first value, something like this:

return "<section>
            <p class=\"odd\">
                <a href=\"{odd}\">{odd}</a>
            </p>
            <p class=\"even\">
                <a href=\"{even}\">{even}</a>
            </p>
        </section>";

Thanks in advance!

1 Answer 1

1

easiest way to do that:

$odd = false;

function add_link($n)
{
    global $odd;

    $odd = !$odd;
    $class = ($odd) ? 'odd' : 'even';
    return "<p class=\"$class\"><a href=\"$n\">$n</a></p>";
}

Of course there are other concerns in the code about mixing HTML with PHP, functions and scopes, etc. but I just built upon your code.

also if you are using these classes only for styling you can use pure CSS: :nth-child()

( provided that you are not using older browsers )

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

1 Comment

thank you, I modified this code from an existing example and I would be very grateful to see a clean solution, excluding my mistakes. Only if you have a few minutes to add the right example. Thank you!

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.