Skip to main content
Inspiring
November 18, 2012
Question

Looping through Multiple Text Fields

  • November 18, 2012
  • 5 replies
  • 1433 views

Ok so I am stumped right now. I have two fields that I want to loop through and insert into the db table links. I have initialized a counter but that is as far as I can figure out, that works properlly without errors. Does anyone know of a tutorial that can explain how to do it? I have searched but have not had any luck.

1) Link = Text Field

2) Categories = Select Menu

Form.php

<?php for ($o = 1; $o <= $videofields; $o++) { ?>

  <p>

    <label for="link<?php echo $o; ?>">Link: <?php echo $o; ?></label>

    <input value="<?php if ($_POST && $errors) {

  echo htmlentities($_POST["link{$o}"], ENT_COMPAT, 'UTF-8');

}?>" type="text" name="link<?php echo $o; ?>" id="link<?php echo $o; ?>" />

  </p>

  <p>

    <label for="category_id<?php echo $o; ?>">Select Catgegory:</label>

    <select name="category_id<?php echo $o; ?>" id="category_id<?php echo $o; ?>">

      <option value="0">-- Select place --</option>

      <?php

foreach ($categories as $row) {

  echo "<option value='{$row['category_id']}'";

  if ($errors && $_POST["category_id{$o}"] == $row['category_id']) {

    echo 'selected="selected"';

  }

  echo ">{$row['category_name']}</option>";

}

?>

    </select>

  </p>

<?php } ?>

Form_Definitions.php

  $data = array('link' => $_POST['link'],

                           'category_id' => $_POST['category_id']);

  $dbWrite->insert('videos', $data);

Database Tables

Videos

1. Video_id

2. Link

3. Category_id

Categories

1. Category_id

2. Category_name

Sorry if I am asking for too much. Or not giving enough information. I have been searching but haven't found a solution yet.

Thanks.

This topic is closed to new replies. Start a new post to keep the conversation going.

5 replies

Inspiring
November 19, 2012

Ok so I still can't figure this out. But this is what I beleive needs to happend. In my video_insert.php I need a foreach loop and array with something similar to the following.

$num = 1;

foreach () {

$link = $_POST["link{$num}"];

   if (isset($_POST["category_id{$num}"])) {

     $category_id = $_POST["category_id{$num}"];

   }

$o = 1;

$videos[] = array('videolink'  => $_POST['link'],

                  'category_id' => $_POST['category_id']);

}

I have a foreach that runs through each time, but don't know  what variable I need to assign it? What would I assign a variable to post text field?

Any help would be great.

Participating Frequently
November 20, 2012

I don't know that you need another array since you already know that the field names have incremental values appended. I would just loop through the $_POST array with a WHILE loop.

Inspiring
November 20, 2012

Can you please go into more detail? Forget about my second post, how do I get the "Link field" and "Category field" to insert into my db? I have a counter to display each field twice, but as it stands doesn't insert at all. I have been searching for 2 days now on this and can't find an answer.

If I take the counter field out, and have the fields display once, it works great! But now I have been tasked with this and haven't had much luck.