0

I am creating a system where a user can enter an ingredient and then save its quantity, I use PHP to save it as XML.

It currently saves like this.

 <ingredients>
    <ingredient quantity="2">Apple</ingredient>
    <ingredient quantity="4">Banana</ingredient>
  </ingredients>

Instead i would like to save it like this but i can't figure out how.

  <ingredient>
            <quantity_amount>2</tns:quantity_amount>
            <quantity_name>teaspoons</tns:quantity_name>
            <ingredient_name>baking powder</tns:ingredient_name>
        </ingredient>

for each ingredient added

Here is my PHP and HTML i currently use.

HTML

  <div id="addIngredient">
                <p>
                <input type="text" id="ingredient_1" name="ingredient[]" value="" placeholder="Ingredient"/>
                <input type="text" id="quantity_1" name="quantity[]"  value="" placeholder="Quantity"/>
                <a href="#" id="addNewIngredient">Add</a>
                </p>

                </div>

PHP

// Start for loop, from 0 to ingredientName
for ($i = 0; $i < count($ingredientName); $i++) {
    // Select current item
    $element = $ingredientName[$i];

    // Updated used ingredients list ONLY
    // if the ingredient wasn't added yet
    $temp = search($upIngreds, 'value', $element);
    if (empty($temp)) {
        $upIngreds[] = array('value' => $element, 'label' => $element, 'image' => 'images/products/default.jpg');
    }

    // Select current item quantity
    $qty = $quantity[$i];
    // Create ingredient element and fill with $element
    $ingredient = $xml->createElement('ingredient', $element);
    // Set quantity attiribute to $qty value
    $ingredient->setAttribute('quantity', $qty);
    // Append it to ingredients element
    $ingredients->appendChild($ingredient);
}
3
  • oh sorry ignore the tns. it was just a rough way to show how i want it to come out as xml. With each ingredient in its own <ingredient> </ingredient> with them all being under <ingredients> </ingredients> Commented Jan 3, 2014 at 22:51
  • how do you create your XML? simplexml or DOM or ?? Commented Jan 3, 2014 at 22:53
  • i create it through dom Commented Jan 3, 2014 at 22:55

1 Answer 1

1

to create a new ingredient according to your desired XML structure:

// creating the document and its root
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('ingredients','');
$dom->appendChild($root);

// create new ingredient and link it to root 
$ingredient = $dom->createElement('ingredient','');
$root->appendChild($ingredient);    

// create children and link them to ingredient
$q_amount = $dom->createElement('quantity_amount',"1");
$q_name = $dom->createElement('quantity_name',"spoon");
$i_name = $dom->createElement('ingredient_name',"PHP");
$ingredient->appendChild($q_amount);
$ingredient->appendChild($q_name);
$ingredient->appendChild($i_name);

echo $dom->saveXML();

see it working: https://eval.in/85654

Be sure to sanitize user input from your form prior to inserting it into the XML.

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

3 Comments

is it possible to loop this for multiple ingredients?
I hae tried to follow your way with my work eval.in/85680 however i get this xml <ingredients/> <steps> <step_detail number="0">jhk </step_detail> </steps> </recipe> <ingredName> <ingredient quantity="hey">hey</ingredient> </ingredName> <ingredName> <ingredient quantity=" cskc">x skxs</ingredient> </ingredName> the ingredients just closes itself and then my other elements follow at the end of the xml
@NickPocock see eval.in/85869. I don't know what the search function in your code does, and how to read from your form. But this sample shows how to add elements in a loop.

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.