0

I am working on a script and want to use an array called $states twice. $states is an array with a list of 50 states.

I am using the code snippet below to display the results. If I only use the div with class="table-responsive" by itself it works and displays correctly. If I only use the div with class="container" by itself it also displays the results correctly.

However, when I try using both together i.e. one following the other the first one works and the second one displays nothing or at most displays the last result in the array. I have tried resetting array but nothing seems to work.

What am I doing wrong? Any help would be appreciated. Thanks.

<div class="table-responsive" id="defaultview">

    <table  class="table"  width="100%"  border="0" cellspacing="0" cellpadding="0">
    <? array_shift($states);
    $rows3 = array_chunk($states, 2);
    foreach ($rows3 as $states) { ?>
    <tr>
    <? foreach($states as $state){ ?>

    <td><a href="/search.php?state=<?=$state['state']?>" title="<?=$state['state']?>">
    <?=$state['state']?>
    </a></td>
    <? } ?>
      </tr>
      <? }?>
    </table>

</div><!-- /End #state -->



<div class="container" id="smallview" style="padding-left: 1px;"> 
    <ul class="list-group">

    <? 
    reset($states);
    array_shift($states);
    foreach($states as $state)
    { ?>
    <li class="list-group-item"><a href="/search.php?state=<?=$state['state']?>" title="<?=$state['state']?>">
     <?=$state['state']?>
     </a></li>
     <? } ?>


     </ul>
 </div><!-- /End #state -->
8
  • Try unset($states);. Commented Feb 27, 2015 at 0:37
  • you don't use the same array now do you. Commented Feb 27, 2015 at 0:37
  • 1
    @Krii sure deleting the variable will help Commented Feb 27, 2015 at 0:38
  • @Dragon what do you mean don't use the same array? Commented Feb 27, 2015 at 0:44
  • reset() ? php.net/manual/en/function.reset.php - surely the answer's not actually in the title of the question? Commented Feb 27, 2015 at 0:46

1 Answer 1

1

The problem is here:

array_shift($states);
$rows3 = array_chunk($states, 2);
foreach ($rows3 as $states) {
//                 ^^^^^^^

At this point you overwrite the original $states array; if the original array had an odd number of elements, say 49, at the end of the loop you would now have a $states array with just one element.

Further in the code:

reset($states);
array_shift($states);

After array_shift() the array would now be empty.

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

2 Comments

thank you so much. That is exactly what is happening. I am a newbie at all this. How would I fix this to make this work? Any suggestion would really be appreciated.
You can fix it by avoiding the same variable name as your original array, e.g. foreach ($rows3 as $row).

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.