Foreach repeating the same content

-1

Scenery

I have a form and via POST submitting to the page preview.php 10 song names that are divided between 4 arrays. I can generate the 10 divs containing the position array but when I create foreach for the artist I have a problem because it repeats the same content 10 times. How to improve this code with a smarter solution?

Code

preview.php

$track = array (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

$artistname = array (
  'artist1'=>$_POST['artist1'],
  'artist2'=>$_POST['artist2'],
  'artist3'=>$_POST['artist3'],
  'artist4'=>$_POST['artist4'],
  'artist5'=>$_POST['artist5'],
  'artist6'=>$_POST['artist6'],
  'artist7'=>$_POST['artist7'],
  'artist8'=>$_POST['artist8'],
  'artist9'=>$_POST['artist9'],
  'artist10'=>$_POST['artist10']
);

$trackname = array (
  'track1'=>$_POST['track1'],
  'track2'=>$_POST['track2'],
  'track3'=>$_POST['track3'],
  'track4'=>$_POST['track4'],
  'track5'=>$_POST['track5'],
  'track6'=>$_POST['track6'],
  'track7'=>$_POST['track7'],
  'track8'=>$_POST['track8'],
  'track9'=>$_POST['track9'],
  'track10'=>$_POST['track10']
);

$recordname = array (
  'record1'=>$_POST['record1'],
  'record2'=>$_POST['record2'],
  'record3'=>$_POST['record3'],
  'record4'=>$_POST['record4'],
  'record5'=>$_POST['record5'],
  'record6'=>$_POST['record6'],
  'record7'=>$_POST['record7'],
  'record8'=>$_POST['record8'],
  'record9'=>$_POST['record9'],
  'record10'=>$_POST['record10']
);


<div class="row">
 <div class="large-12 columns">

  <h2>Confirm and submit your chart below.</h2>
  <h3>September 2014 Top 10</h3>
  <div class="row">

    <?php for ($i = 1; $i < count($track);): ?>
      <?php for($j = 0; $j < 3 && $i + $j < count($track); ++$j): ?>
        <div class="large-12 columns">
          <div class="row collapse prefix-radius">

            <div class="small-1 columns">
              <p><?php echo $track[$j + $i] ?></p>
            </div>

            <?php foreach($artistname as $value): ?>
              <div class="small-4 columns">
                <p><?php echo $value; ?></p>
              </div>
            <?php endforeach; ?>

            <div class="small-4 columns">
              <p></p>
            </div>

            <div class="small-3 columns">
              <p></p>
            </div>

        </div>
      </div>
      <?php endfor; $i += $j;?>
    <?php endfor; ?>

    <div class="large-12 columns">
      <input class="button" type="submit" value="Submit">
      <a href="javascript:history.back();" class="button">Back</a>
    </div>
  </div>

  </div>
  </div>
    
asked by anonymous 16.09.2014 / 19:08

1 answer

4

You can use form fields as an Array and access them with PHP.

 <td><input type="text" name="track[]"></td>
 <td><input type="text" name="artist[]"></td>
 <td><input type="text" name="recorder[]"></td>

And access them like this:

  $traks = count($_POST['track']);
  for ($i=0; $i < $traks; $i++) { 
     $track      = $_POST['track'][$i];
     $artist    = $_POST['artist'][$i];
     $recorder  = $_POST['recorder'][$i];
  }

Here is a complete example:

<?php
   if (isset($_POST['submit'])):

      $traks = count($_POST['track']);

      for ($i=0; $i < $traks; $i++) { 
         $track      = $_POST['track'][$i];
         $artist    = $_POST['artist'][$i];
         $recorder  = $_POST['recorder'][$i];
         echo "Música: {$track}; Artista: {$artist}; Gravadora: {$recorder}. <br>";
      }

   echo '<br><hr><br>';
   endif;
?>

<form action="" method="POST">
   <table>

   <?php 

   for ($i=0; $i < 10; $i++) { 

?>

      <tr>
         <th>Música</th>
         <th>Artista</th>
         <th>Gravadora</th>
      </tr>
      <tr>
         <td><input type="text" name="track[]"></td>
         <td><input type="text" name="artist[]"></td>
         <td><input type="text" name="recorder[]"></td>
      </tr>
      <tr><td colspan="3">&nbsp;</td></tr>

<?php
   }

   ?>
   </table>
   <input type="submit" value="Enviar" name="submit">
</form>
    
16.09.2014 / 19:55