1

I want to make a selection display like the following in html:

<div name="option_a">
    <p>result for option_a</p>
    <img src="option_a_image"></img>
</div>
<div name="option_b">
    <p>result for option_b</p>
    <img src="option_b_image"></img>
</div>
<div name="option_c">
    <p>result for option_c</p>
    <img src="option_c_image"></img>
</div>
<div name="option_d">
    <p>result for option_d</p>
    <img src="option_d_image"></img>
</div>

using this array :

Array
(
    [0] => stdClass Object
        (
            [option_a] => its option a
            [option_a_image] => its an image for option a
            [option_b] => its option b
            [option_b_image] => its an image for option b
            [option_c] => its option c
            [option_c_image] => its an image for option c
            [option_d] => its option d
            [option_d_image] => its an image for option d
    )
    [1] => stdClass Object
        (
            [option_a] => its option a
            [option_a_image] => its an image for option a
            [option_b] => its option b
            [option_b_image] => its an image for option b
            [option_c] => its option c
            [option_c_image] => its an image for option c
            [option_d] => its option d
            [option_d_image] => its an image for option d
    )
    [2] => stdClass Object
        (
            [option_a] => its option a
            [option_a_image] => its an image for option a
            [option_b] => its option b
            [option_b_image] => its an image for option b
            [option_c] => its option c
            [option_c_image] => its an image for option c
            [option_d] => its option d
            [option_d_image] => its an image for option d
    )
)

but I wanted to make it more concise using loops. I have tried the following:

<?php foreach($array[0] as $type=>$value ){
    if(strpos($type,'option_') !== false){ ?>           
    <div class="card mb-3" style="width: 98%; margin: auto">
    <div class="card-body">
                                            
    <label ><input type='radio' name='r1' tabindex='5'> <span><?php echo $value ?></span></label> <br>        
                                            
    </div>
    </div>
<?php } } ?>

but loops like this can't produce the view I want to create. So how do I get the loop to form the result I want to produce?

Thank you

1 Answer 1

1

it should work for you:

$array[0] = [
    "option_a"       => "its option a",
    "option_a_image" => "its an image for option a",
    "option_b"       => "its option b",
    "option_b_image" => "its an image for option b",
    "option_c"       => "its option c",
    "option_c_image" => "its an image for option c",
    "option_d"       => "its option d",
    "option_d_image" => "its an image for option d"
];

$thisElementIsOption = true;
$option = [];

foreach($array[0] as $type => $value) {
    if(strpos($type, 'option_') !== false) {
        if ($thisElementIsOption) {
            $option['name'] = $type;
        } else {
            $option['image'] = $type;

            $name = $option['name'];
            $image = $option['image'];
            echo "<div name='$name'><p>result for $name</p><img src='$image'></img></div>".PHP_EOL;

            $option = [];
        }

        $thisElementIsOption = !$thisElementIsOption;
    }
}

Output

<div name='option_a'><p>result for option_a</p><img src='option_a_image'></img></div>
<div name='option_b'><p>result for option_b</p><img src='option_b_image'></img></div>
<div name='option_c'><p>result for option_c</p><img src='option_c_image'></img></div>
<div name='option_d'><p>result for option_d</p><img src='option_d_image'></img></div>

In this solution I use $thisElementIsOption as a flag variable and make an array $option and make the html output decussate

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

3 Comments

what is the use of $thisElementIsOption = !$thisElementIsOption; after if else ?
When I want to make an html like this <div name='option_a'><p>result for option_a</p><img src='option_a_image'></img></div> I need to iterate two rows , because i need the option data and option image data so I use this flag variable to do this
I see. Thank you so much. its very usefull

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.