0

I have an application that looks like this:

enter image description here

As you can see, each row contains either a group heading (the rows where there is just an input), or a ingredient form (where there is a small input, then a select, then another larger input).

I use Javascript to add the new spans. I use the following PHP to group each ingredient span into an array, determine the order (because each span can be moved to a different order), and insert into my database.

foreach($_POST as $key => $value) {
        $value = $this->input->post($key);
        $ingredientQTY = $this->input->post('ingredientQTY');
        $measurements = $this->input->post('measurements');
        $ingredientNAME = $this->input->post('ingredientNAME');
        $ingredientsROW[] = array($ingredientQTY, $measurements, $ingredientNAME);
      
        for ($i = 0, $count = count($ingredientQTY); $i < $count; $i++) {
            $rows[] = array(
                'ingredientamount'      => $ingredientQTY[$i],
                'ingredientType'        =>  $measurements[$i],
                'ingredientname'        => $ingredientNAME[$i],
                'recipe_id'             => $recipe_id,
                'order'                 => $i + 1,
                'user_id'               => $user_id
            );
            $sql = "INSERT `ingredients` (`ingredientamount`,`ingredientType`,`ingredientname`, `recipe_id`, `order`, `user_id`) VALUES ";
            $coma = '';
            foreach ($rows as $oneRow) {
                $sql .= $coma."('".implode("','",$oneRow)."')";
                $coma = ', ';
            }
            
        }
        $this->db->query($sql);
        break;
}

This works wonders for inserting the ingredient rows. But I'm not sure how to insert group headings (which must be placed in the for loop to keep the order, the $i + 1, going).

I think I've figured out two solutions(though there may be others, and these might not even work):

  1. Have the group heading input field have the same name value as one of the ingredient text fields, and send a hidden value along with it, saying its a group heading?

  2. Send it as different input field with a different name value?

My question is: how can I do this with my current code, and are either of these efficient solutions, or is there an even better solution?

Thanks for all help! If you need more details, just ask!

1 Answer 1

1

You could use an empty heading like <input type="hidden" name="groupheading[]" value="product" /> and the open one like <input type="text" name="groupheading[]" value="" />. The first one should be inside the product-span.

This way, you can continue your loop just the way you are doing now. And $_POST['groupheading'][$key] either returns a groupheading or the phrase 'product'. So, in your script it would be:

if($_POST['groupheading'][$key] == "product") {
  // insert product
} else {
  // insert group heading
}

I think I helped you this morning or yesterday with an answer.. it's still a bit a weird way you are using to get the effect you need. It can be achieved much easier.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer! What do you mean by it can be achieved much easier?
Well, it seems that the setup you are using for your form (the HTML part) can be used in combination with much simpler code to achieve what you are trying to do. It has nothing to do with this question however and might be just a personal opinion on the way I tackle my programming problems :)
I'm fairly new at PHP, so if there's a better way, I'd love to hear about it!
That would invole a larger example including some HTML and jQuery functions. You'll learn the most from trial-error coding and solving the issues you run into :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.