php - foreach echo into 2 different input from json

435

I'm stuck at this point, what I wish :

For each name not containing "+price" get the value echo into input For each name containing "+price" get the value echo into unput

My php and html :

foreach($fields as $field):

<input type="text" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>"/>

Prix : <input type="text" name="<?php echo $field['name'].'+price'; ?>" value="<?php echo $optionprix; ?>"

endforeach;

My records, $fields, are in JSON format =>

 array(8) {
    [0] => array(2) {
        ["name"] => string(26) "checkboxes-label---3499163"  
        ["value"] => string(7) "Options"
    }
    [1] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(34) "Aide à l installation du parcours"
    }
    [2] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price"  
        ["value"] => string(3) "500"
    }
    [3] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(27) "Location du système vidéo"
    }
    [4] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price"  
        ["value"] => string(2) "10"
    }
    [5] => array(2) {
        ["name"] => string(25) "single-checkbox---3499163"  
        ["value"] => string(4) "test"
    }
    [6] => array(2) {
        ["name"] => string(31) "single-checkbox---3499163+price" 
        ["value"] => string(2) "13"
    }
    [7] => array(2) {
        ["name"] => string(18) "required---3499163"  
        ["value"] => bool(false)
    }
}

By this way when I dump echo var_dump($field) =>

array(2) {
    ["name"] => string(25) "single-checkbox---3499163" 
    ["value"] => string(34) "Aide à l installation du parcours"
}

array(2) {
    ["name"] => string(31) "single-checkbox---3499163+price" 
    ["value"] => string(3) "500"
}

I'm not sure the best way to complete it, I search with count, in_array and explode methodes but without success

513

Answer

Solution:

Most accurately without regex, you only need to check the last 6 characters for a match in your condition block.

foreach ($fields as $field) {
    if (substr($field['name'], -6) !== "+price") {
        echo "<input type=\"text\" class=\"optionname\" name=\"{$field['name']}\" value=\"{$field['value']}\"/>";
    } else {
        echo "Prix : <input type=\"text\" class=\"optionprix\" name=\"{$field['name']}+price\" value=\"$optionprix\">";
    }
}
656

Answer

Solution:

This only checks if name is ending with 'price'

if(preg_match('/price$/', $field['name'])) {
    //Prix input
} else{
    //normal input
}
102

Answer

Solution:

You can do this quite simply with the use ofstrpos() as follows

<?php
foreach($fields as $field):
  if ( strpos($field['name'], '+price') === FALSE ) :
?>
    <input type="text" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>"/>

<?php
  else:
?>
    Prix : <input type="text" name="<?php echo $field['name'].'+price'; ?>" value="<?php echo $optionprix; ?>"

<?php
  endif;
endforeach;
?>

People are also looking for solutions to the problem: What is the simplest way to use an external php library in a custom magento 2 module?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.