Search code examples
phpimagecodeigniterif-statement

error while using if statement inside image in php


i have a codeigniter php website, in the image tag I have given an if statement to determine the source, the code is like below:

<img class="st_img" <?php if(str_replace(' ', '',$val['unique_tblkey'])=='entertainment'){?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'s/'.$val['image'][0]['btp_image'];?>"<?php} else { ?> src=" <?php echo ADMIN_IMG.strtolower(str_replace(' ', '',$val['unique_tblkey'])).'/'.$val['image'][0]['btp_image'];?> " <?php } ?>/>
                

I am getting

unexpected '}'

this error is coming although there isn't any unwanted brackets, can anyone please tell me what is wrong in my code, thanks in advance


Solution

  • OK, I finished rewriting it. Not completely sure if it will work, because I cannot test it.

    <?php 
    
    $tableKey = strtolower(str_replace(' ', '', $val['unique_tblkey']));
    $url = ADMIN_IMG . 
           $tableKey . 
           ($tableKey == 'entertainment' ? 's/' : '/') . 
           $val['image'][0]['btp_image'];
    echo '<img class="st_img" src="'.$url.'" />';
    

    There's no repetition, multiple lines, and no mixing of PHP and HTML. This all makes for more readable code.