2 Replies - 159 Views - Last Post: 04 February 2012 - 12:38 PM Rate Topic: -----

Topic Sponsor:

#1 NewToJava2011  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 21-November 11

Using counters to increment around array using PHP

Posted 04 February 2012 - 10:49 AM

My website design uses a layout with 3 separate columns and thus 3 separate headings. I'm trying to reuse code to keep things looking tidy.

The 3 cities below become my column headings as set in my cityConfig.php script

$cities = array('London', 'New York', 'Paris');

$columnHeadings = array($columnHeading . $cities[0], $columnHeading . $cities[1], $columnHeading . $cities[2]);




My threeColumnContainer.php script makes reference to these headings via the columnsHeading() function as below.

<ul class="columns">
            <li class="col1">
                <h3>
                    <?php
                    columnHeadings($columnHeadings); 
                    ?>
                </h3>
            </li>
            <li class="col2">
                <h3>
                    <?php
                    columnHeadings($columnHeadings);
                    ?>
                </h3>
            </li>
            <li class="col3">
                <h3>
                    <?php
                    columnHeadings($columnHeadings);
                    ?>
                </h3>
            </li>
  

<?php

function columnHeadings($columnHeadings) {
    
    static $counter = 0; 
    
    if (isset($columnHeadings[$counter])) {
        echo $columnHeadings[$counter];
        $counter ++; 
    }
    
    return result;
}
?>


My question being, is it bad practice to use a counter like this to increment around the array to display the relevant headings?

My previous solution was the following as as mentioned duplicated more code.

<ul class="columns">
            <li class="col1">
                <h3>
                    <?php
                    if (isset($columnHeadings[0])) {
                        echo $columnHeadings[0];
                    }
                    ?>
                </h3>
            </li>
            <li class="col2">
                <h3>
                    <?php
                    if (isset($columnHeadings[1])) {
                        echo $columnHeadings[1];
                    }
                    ?>
                </h3>
            </li>
            <li class="col3">
                <h3>
                    <?php
                    if (isset($columnHeadings[2])) {
                        echo $columnHeadings[2];
                    }
                    ?>
                </h3>
            </li>
  




Is This A Good Question/Topic? 0
  • +

Replies To: Using counters to increment around array using PHP

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 378
  • View blog
  • Posts: 740
  • Joined: 17-June 08

Re: Using counters to increment around array using PHP

Posted 04 February 2012 - 11:09 AM

Yeah, that looks a bit questionable. Seems to be that it would be much more straight-forward to just pass the index in as a separate parameter rather than using the static variable. That way you don't have to assume that you'll always be displaying those items in the same order. Also, it would be a bit clearer that the function shouldn't actually output the same thing every time. And while I'm at it, you don't need to be returning an undefined variable - it's pointless.
function columnHeadings($columnHeadings, $index) {
    if (isset($columnHeadings[$index])) {
        echo $columnHeadings[$index];
        $counter ++; 
    }
}



But really, I question the need for that function in the first place - all it does is an isset() check and an echo. You don't seem to be calling it in a loop, or getting the values in your array from an external source, so why do you need the isset() check at all? Shouldn't you already know which array indexes are set?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: Using counters to increment around array using PHP

Posted 04 February 2012 - 12:38 PM

I'm not really sure what you're trying to do, but I think that trying to use two languages at once is confusing you. Try this:
<?php
$htm = <<<HERE
<ul class="columns">
	<li class="col1">
		<h3>
			{Heading1}
		</h3>
	</li>
		<li class="col2">
			<h3>
				{Heading2}
			</h3>
		</li>
		<li class="col3">
			<h3>
				{Heading3}
			</h3>
		</li>
</ul>
HERE;
$headings = array("My first heading", "The second heading!", "The third and last heading");
$replace = array("{Heading1}","{Heading2}","{Heading3}");
$htm = str_replace($replace, $headings, $htm);
echo $htm;

?>

Isn't that easier?

This post has been edited by CTphpnwb: 04 February 2012 - 12:42 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1