views:

30

answers:

3

hi very good, I have a label on a form select and I get that value with $ _POST like this:

$gallery = array($_POST['gallery']);

and that value will put it here:

$image = $sitemap->gallery[$gallery]->addChild('image');

the problem is giving me error is as follows:

Fatal error: Call to a member function addChild() on a non-object in

I do not understand is that if I put a value directly asin me do it like so:

$gallery = 0;
$Image = $sitemap->gallery[$gallery]->addChild('image');

I do well, what happens is that I want the user to choose, Kind of strange as it may fix.

A: 

use this:

$gallery = $_POST['gallery'];

instead of this:

$gallery = array($_POST['gallery']);

You passing an array that you did not index properly

Or you can try it this way:

$gallery = array('gallery' => $_POST ['gallery']);

or

$image = $sitemap->gallery[$gallery[0]]->addChild('image');

either way should fix the problem

Phill Pafford
I do not think so, but I've fixed it with this simple function $id=intval($_POST['gallery']);
chiqui3d
A: 

thank you very much, but I tested this and is giving me the same error

  $gallery=array('gallery'=>$_POST['galeria']);
$image = $sitemap->gallery[$gallery]->addChild('image');

I do not understand the select form that I have is as follows:

<select id="textfield" name="galeria">
        <option id="textfield" value="">Escoger de la Lista</option>
        <?php
    $source = 'content.xml';
    // load as string
    $xmlstr = file_get_contents($source);
    $sitemap = new SimpleXMLElement($xmlstr);
    // load as file
    $sitemap = new SimpleXMLElement($source,null,true);
    //$bar_count = $sitemap->gallery->count();
    //for($i=0;$i<$bar_count;$i++){
    $contador="0";
    foreach($sitemap->gallery as $content) {
    $atributo = $content->attributes();
    echo "<option id='textfield' value='".$contador."'>".$atributo['Name']. "</option>";
    //}
    $contador++; 
    } 
    ?>
      </select>

anyway I've tried has done an echo and I get the result, do not really understand

chiqui3d
this line: '$image = $sitemap->gallery[$gallery]->addChild('image');' Should be: '$image = '$sitemap->gallery[$gallery['gallery']->addChild('image');'
Phill Pafford
$sitemap->gallery[$gallery['gallery']->addChild('image');
Phill Pafford
You should edit your question and not post an answer unless you have answered your own question
Phill Pafford
$id=intval($_POST['gallery']);
chiqui3d
I do not think so, but I've fixed it with this simple function $id=intval($_POST['gallery']);
chiqui3d
yes so instead of passing an array you are now casting that value to an integer $id=intval($_POST['gallery']);
Phill Pafford
A: 

Understanding Arrays:

$gallery = array($_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

Output:

Array
(
    [0] => 'value in array'
)

How to get the value out of an array:

echo "Get Array Value: ".$gallery[0]."<br />"; // You should be displaying the array index 0

Adding a custom index

$gallery = array('gallery' => $_POST['gallery']);
echo "Gallery Array: <pre>".print_r($gallery,true)."</pre><br />";

Output: Array

(
    [gallery] => 'value in array'
)

Getting the value from the customer index array

echo "Get Array Value: ".$gallery['gallery']."<br />"; // You should be displaying the array index gallery
Phill Pafford