Professional Web Applications Themes

index number for an array - PHP Development

Hi php4.3.11 I am using <a href=\"$lhsdata_link$lhsdata[0]\">$lhsdata[1]</a> in a table. I need to print out around 20 lines which is not the problem [0] & [1] need to increment by 5 on each write to give <a href=\"$lhsdata_link$lhsdata[0]\">$lhsdata[1]</a> <a href=\"$lhsdata_link$lhsdata[5]\">$lhsdata[6]</a> <a href=\"$lhsdata_link$lhsdata[10]\">$lhsdata[11]</a> i am probably going about this the wrong way as i am still a relative newbie to php. Mark...

  1. #1

    Default index number for an array

    Hi

    php4.3.11

    I am using <a href=\"$lhsdata_link$lhsdata[0]\">$lhsdata[1]</a> in a table.

    I need to print out around 20 lines which is not the problem [0] & [1] need
    to increment by 5 on each write

    to give

    <a href=\"$lhsdata_link$lhsdata[0]\">$lhsdata[1]</a>
    <a href=\"$lhsdata_link$lhsdata[5]\">$lhsdata[6]</a>
    <a href=\"$lhsdata_link$lhsdata[10]\">$lhsdata[11]</a>

    i am probably going about this the wrong way as i am still a relative newbie
    to php.

    Mark

    Mark Guest

  2. #2

    Default Re: index number for an array

    for($i = 0; $i <= 100; $i += 5) {
    echo "<a href=\"$lhsdata_link" . $lhsdata[$i] . "\">" .
    $lhsdata[$i+1] . "</a>";
    }

    ZeldorBlat Guest

  3. #3

    Default Re: index number for an array

    Mark D Smith wrote:
     


    The 'for' loop is your friend. Given an array $A structured the way
    yours is,
    then

    $end = count($A) / 5; # calc the end value before looping
    for ($i=0; $i<$end; $i += 5) {
    echo "Link $A[$i] is tied to name $A[$i+1]";
    }


    But if you can redefine your data structure, then you can make the
    'for' loop
    simpler:

    $A[0][link] = 'index.html';
    $A[0][name] = 'My Home Page';
    $A[1][link] = 'bio.html';
    $A[1][name] = 'All About Me';

    then:

    $end = $count($A); # still good to factor this out first
    for ($i=0; $i<$end; $i++) {
    echo "link $A[$i][link] is tied to name $A[$i][name]";
    }

    which has good performance and would probably simplify the process for
    loading the array, too.

    The 'foreach' loop is also your friend, and when it can be used, like
    probably here, it is an even better friend than 'for':

    $A['index.html'] = 'My Home Page';
    $A['bio.html'] = 'All About Me';

    then

    foreach ($A as $link => $name) {
    echo "The link is $link and it is named $name";
    }

    Performance would probably be better than the 'for' loop and debugging
    and maintenance would be a lot simpler.

    will@thornhenge.org Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139