0

I need to know if there's a quickest and efficient way to display one or more images if $value == $string

For example: I have an cell which only contains 3 single string: 'r o g', if user put ro it will output <img src="red.gif"> and <img src="orange.gif"> So it could be random if user insert gr then it will display <img src="green.gif"> and <img src="red.gif">

Right now I can only think something like...

<?php $red = "<img src="red.gif">";
$orange = "<img src="orange.gif">";
if( $cell1 == $red ){ echo $red;}
if( $cell1 == $red && $orange ){ echo $orange.$red;}
etc...

This method might works but has to provide too many possibility and I believe there's a shorter and efficient to do that, but I haven't got any idea because still I'm learning PHP

5
  • User can put only two of the three characters right? Commented Jan 26, 2016 at 22:03
  • can be 0 and max 3 characters for now Commented Jan 26, 2016 at 22:04
  • How it should respond if they put nothing? Commented Jan 26, 2016 at 22:05
  • It will response nothing @PradeepSambandam Commented Jan 26, 2016 at 22:10
  • Simply don't concatenate your output and have 3 if statements, one for each color. if (strpos($cell1, "r") !== false) { echo "<img src=\"red.gif\">"; } etc... Commented Jan 26, 2016 at 22:10

3 Answers 3

2

How about this approach?

<?php

//define all your images here
$images = [
    'r' => 'red.png',
    'g' => 'green.png',
    'b' => 'blue.png',
    'y' => 'yellow.png',
    'o' => 'orange.png'
];

function output($input, $images) {
    $parts = str_split($input);
    foreach ($parts as $part) {
        if (isset($images[$part]))
            echo '<img src="' . $images[$part] . '">';
    }
}

echo "rgb: \n";
output('rgb', $images);

echo "\n\nyor: \n";
output('yor', $images);

echo "\n\nxxy: \n";
output('xxy', $images);

Output:

rgb: 
<img src="red.png"><img src="green.png"><img src="blue.png">

yor: 
<img src="yellow.png"><img src="orange.png"><img src="red.png">

xxy: 
<img src="yellow.png">
Sign up to request clarification or add additional context in comments.

1 Comment

this is clean I like it
1

Try this approach:

for( $i = 0; $i <= strlen( $cell1 ); $i++ ) {
    $char = substr( $cell1, $i, 1 );
    switch ($char) {
        case "r":
            echo '<img src="red.gif">';
            break;
        case "o":
            echo '<img src="orange.gif">';
            break;
        case "g":
            echo '<img src="green.gif">';
            break;
        default:
            break;
    }
}

Comments

1

Here is the code that shows an example for 3 character inputs. $string can be the posted value.

$r = '<img src="red.gif">';
$y = '<img src="yellow.gif">';
$o = '<img src="orange.gif">';
$g = '<img src="green.gif">';
$string = 'ryo';
$length = strlen($string);

for ($i=0; $i<$length; $i++) {
    echo ${''.$string[$i]};
}

1 Comment

I dont think you need to check the length, if its 0 then there won't be any iteration

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.