Pregunta

I managed to achieve specific database array order, now I'd like to print it in proper combination, but I have no idea how the 'foreach' loop should look like.

What I have now is:

$textfields = get_settings('test_testbutton');
if (!empty($textfields)) {
    foreach ($textfields as $textfield) {
        ?>
        <p>
            <input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[0][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
            <input type="text" id="<?php echo $value['id']; ?>" name="test_testbutton[1][]" value="<?php echo $textfield; ?>" placeholder="Input Value"/>
            <a href="#" id="removebutton">Remove</a>
        </p>
    <?php
    }
} else {
}

I really don't know how to change 'echo $textfield' and make it work. I tried adding [] to '$textfields' value and then echo $textfield[] or $textfield[0], but with no success :(

I attached a .jpg file to make it more understandable.

attachment

¿Fue útil?

Solución

After your update. It seems that you forget a foreach. I hope it's helpful to achieve what you want.

    // Following your dump
    $textfields = array( 0 => array( 1234, "qwer", "abcd"), 1 => array("5678", "tyui", "efgh"));

    if (!empty($textfields)) {
        foreach ($textfields as $textfield) {
            // First loop : 0 => array( 1234, "qwer", "abcd")
            // Second loop: 1 => array("5678", "tyui", "efgh")
            foreach ($textfield as $oneValue) {
                // Loop on the second array $textfield
            }
    }

Otros consejos

The Text in the attached image is a serialized array, so if you want to use foreach on this text you have to unserialize it, then an array will be created

$arr = unserialize($textfields);

I think this will help you and it will work :)

$textfields_data = get_settings('test_testbutton');
if (!empty($textfields_data)) 
{
    $textfields = unserialize($textfields_data);
    $i = 0;
    foreach ($textfields as $textfield) {
        ?>
        <p>
            <input type="text" id="" name="test_testbutton1_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
            <input type="text" id="" name="test_testbutton2_<?php echo $i; ?>" value="<?php echo $textfield[$i]; ?>" placeholder="Input Value"/>
            <a href="#" id="removebutton">Remove</a>
        </p>
    <?php
    $i++;
    }
} 
else 
{
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top