title: jQuery UI Tabs with php : iteration with foreach and current
body: <p><strong>Situation</strong>: I made a script which reads data from a database and renders it so that it can be manipulated by jQuery UI into tabs.</p>
  
  <p><strong>The Code</strong>:</p>
  
  <pre><code>function renderTabs(){
  
      $_db = array(
              'host' =&gt; MYSQL_HOST,
              'user' =&gt; MYSQL_USER,
              'password' =&gt; '',
              'database' =&gt; DB
          );
  
      $_channels = array(
          'chan1'     =&gt; 'db1',
          'chan2'     =&gt; 'db2',
          'chan3'     =&gt;'db3',
          'chan4'     =&gt; 'db4'
      );
  
      $html = '';
  
      $conn = new mysqli($_db['host'],$_db['user'],$_db['password'],$_db['database']);
  
      $html .= '&lt;div id="tabs"&gt;
                  &lt;ul&gt;';
  
      foreach ($_channels as $i =&gt; $channel){
  
          while($chan = current($_channels)){
              echo $tab = array_search($chan,array_values($_channels));
              $html .= '&lt;li&gt;&lt;a href="#tabs-'.$tab.'"&gt;'.array_keys($_channels)[$tab].'&lt;/a&gt;&lt;/li&gt;';
              next($_channels);}
  
          $html .= '&lt;/ul&gt;';
  
          $n_tab = array_search($i,array_keys($_channels));
  
          $n_tab++;
  
          $results = $conn-&gt;query('SELECT * FROM table_test WHERE date BETWEEN (CURDATE() - INTERVAL 7 DAY) AND CURDATE() AND channel = \''.$channel.'\' GROUP BY id ORDER BY total DESC LIMIT 10');
  
          $html .= '&lt;div id="tabs-'.$n_tab.'"&gt;
                    &lt;table style="width:100%"&gt;
                    &lt;tr&gt;&lt;td&gt;TEST&lt;/td&gt;
                    &lt;td&gt;TEST COL1&lt;/td&gt;
                    &lt;td&gt;TEST COL2&lt;/td&gt;
                    &lt;td&gt;TEST COL3&lt;/td&gt;
                    &lt;td&gt;TEST COL4&lt;/td&gt;&lt;/tr&gt;';
  
          foreach($results as $row){
  
              $html .= '&lt;tr&gt;&lt;td&gt;&lt;a href="'.$row['url'].'"&gt;'.$row['title'].'&lt;/a&gt;&lt;/td&gt;
                      &lt;td&gt;'.$row['data1'].'&lt;/td&gt;
                      &lt;td&gt;'.$row['data2'].'&lt;/td&gt;
                      &lt;td&gt;'.$row['data3'].'&lt;/td&gt;
                      &lt;td&gt;'.$row['data4'].'&lt;/td&gt;&lt;/tr&gt;';
          }
  
          $html .= '&lt;/table&gt;&lt;/div&gt;';
  
      }
  
      $html .= '&lt;/div&gt;';
  
      return $html;
  
  }
  </code></pre>
  
  <p>The HTML structure that jQuery UI needs to render the tabs:</p>
  
  <pre><code>&lt;div id="tabs"&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;a href="#tabs-1"&gt;tab1_title&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#tabs-2"&gt;tab2_title&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href="#tabs-3"&gt;tab3_title&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;div id="tabs-1"&gt;
      &lt;p&gt;content1&lt;/p&gt;
    &lt;/div&gt;
    &lt;div id="tabs-2"&gt;
      &lt;p&gt;content2&lt;/p&gt;
    &lt;/div&gt;
    &lt;div id="tabs-3"&gt;
      &lt;p&gt;content3&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>The Questions</strong>: I have one problem and one doubt. The problem is that only 3 out of 4 tabs are rendered (while all four contents are correctly generated) and i don't know why.</p>
  
  <p>I also tried using each() like this:</p>
  
  <pre><code>while($chan = each($_channels)){
    echo $chan[1];
    print_r(array_values($_channels));
    echo $tab = array_search($chan[1],array_values($_channels));
    $html .= '&lt;li&gt;&lt;a href="#tabs-'.$tab.'"&gt;'.array_keys($_channels)[$tab].'&lt;/a&gt;&lt;/li&gt;';}
  </code></pre>
  
  <p>But to no avail.</p>
  
  <p>The doubt: I did the above trying to avoid cycling through the array again, and also because i want to be able to add or remove tabs (along with their generated content) only by adding elements to the $_channels array. Is there any convinient way to achieve this without having to iterate twice?</p>
  
  <p>Thanks for your support in advance.</p>
  
  <p><strong>EDIT:</strong> I might have found the problem, seems the foreach shifts the internal pointer of $_channels by one, and that's why current() loop returns 3 elements instead of 4.
  I know about reset(), but it's useless here..</p>
  
  <p><strong>EDIT2</strong>: It really is a PHP problem, Manual says:</p>
  
  <blockquote>
    <p>Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.</p>
  </blockquote>
  
  <p>So, how can i reset the pointer without breaking the rest of the code?</p>