Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looping through Multiple Text Fields

Explorer ,
Nov 18, 2012 Nov 18, 2012

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.

TOPICS
Server side applications
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 19, 2012 Nov 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 19, 2012 Nov 19, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 19, 2012 Nov 19, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 19, 2012 Nov 19, 2012

>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.

Show us the code you currently have that does insert properly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 19, 2012 Nov 19, 2012
LATEST

Ok so here is the form.php (The cms to insert a new post)

  <p>

    <label for="videolink">Video Link: </label>

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

  echo htmlentities($_POST["videolink"], ENT_COMPAT, 'UTF-8');

}?>" type="text" name="videolink" id="videolink" />

  </p>

  <p>

    <label for="category_id">Select Catgegory:</label>

    <select name="category_id" id="category_id">

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

      <?php

foreach ($categories as $row) {

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

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

    echo 'selected="selected"';

  }

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

}

?>

    </select>

  </p>

And here is the form_definitions.php

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

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

So what I am doing here is inserting text which will be a video link, not the whole link, just the ID of the video and will select a category to associate that video with. What I want is for these fields to display twice so I can insert up to two videos with each post, though each video may be associated with a different category. And for some posts I may not even have a video.

I hope this helps. And thank you for taking the time to help me.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines