Foreach repeating the same content

Asked

Viewed 1,593 times

-1

Setting

I have a form and via POST send 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 the foreach for the artist I have a problem as he repeats the same content 10 times. How to improve this code with a smarter solution?

Code

php preview.

$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>
  • And how would be the correct, 1 artist for each song? How is the association of artists with music?

  • The form that sends the data is generated dynamically, ie the fields artista1, artista2, artista3 ... are created dynamically?

1 answer

4


You can use form fields as if you were a 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];
  }

The following 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>
  • How I put echo output in HTML on another page to validate and then send?

  • In the attribute action form there, you can put the address of the other page to be sent. By loading the page you can put the data in session variables $_SESSION if you want to take the data to another page yet, remembering that you have to log in right at the beginning of the script <?php session_start();.

  • @fricks here is an example of how you can do... http://plnkr.co/edit/d05eN7

  • @Kaduamarual Your example helped me too! Thanks man! Give us a look at how you’re getting (http://joaopaulomusic2.appspot.com)

  • Ta cool @fricks, then marks the answer to close the question. And if you have any problem again can count on us.

  • Problem with programming I have several @Kaduamaral hehehe. Now I need to save in bank this information. And create a list of these recorded Charts.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.