简体   繁体   中英

Not able to Concatenate the HTML Code in PHP

This is my function display_image in which i am checking if the variable $card1 is 0 , then i can set image as 1_AC.png else redback.png for Card 1 and then i am checking if $card2 is 0 then set image as 2_AD.png for Card 2 else redback.png and so on..

What i want to do is i want to concatenate the <img src> of $card1 with <img src> of $card2 with <img scr> of $card3 and so on based on the values of $card1, $card2 , etc

i am little confused, how can i achieve this ??

if($card1=='0'){  
$data = '
    <html>
        <head></head>
        <body>
            <img src = "https://www.stoningtonsoccerinternational.com/wp-content/uploads/2019/02/1_AC.png" height="5" width="5" >
        </body>
    </html>';   
}else{
    $data = '
    <html>
        <head></head>
        <body>
            <img src = "https://www.stoningtonsoccerinternational.com/wp-content/uploads/2019/02/red_back.png" height="5" width="5" >
        </body>
    </html>';
}

//card2 setting and fixing of front and back

if($card2=='0'){
    $data = '
    <html>
        <head></head>
        <body>
            <img src = "https://www.stoningtonsoccerinternational.com/wp-content/uploads/2019/02/2_AD.png" height="5" width="5" >
        </body>
    </html>';

}else{

    $data = '
    <html>
        <head></head>
        <body>
            <img src = "https://www.stoningtonsoccerinternational.com/wp-content/uploads/2019/02/red_back.png" height="5" width="5" >
        </body>
    </html>';
}
    return $data;
    }

Concatenation is simple in PHP.

  • First, move the <html> and <head> and <body> tags out side your the individual card.
  • Then change the assignment of all $data the cards to .= instead of =

Like this:

 $data = "<html><head></head> <body>";

 if($card1=='0'){
    $data .= '<img src = "url 1" height="5" width="5" >';
 }else{
    $data . = '<img src = "url 2" height="5" width="5" >
  }

 // ...and so on...

If they're all just 2 options for each card, you could even use a ternary:

$data .= $card1=='0' ? '<img src = "url 1" height="5" width="5" >' : 
                       '<img src = "url 2" height="5" width="5" >' ;

Then once your done with the conditionals for each card, append the closing <body> and <html> tags:

$data .= "</body></html>"

Here's a reference to the docs to concatentation: http://php.net/manual/en/language.operators.string.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM