Needing help with a code

Hi everyone

Im working on a site and need some help with some coding.

I want to add a user interface with an input box that a user can type a word in and submit, then underneath depending on what word they put in shows a series of pictures representing each letter of that word.

eg if the user puts in the word HELLO it outputs a picture of the letter H then a picture of the letter E ect ect.

I have seen this on a couple of websites so I know it can be done just not to sure how to do it myself.

Please could someone give me a hint or push in the right direction to solve this one.

Hi there,

The first thing that codes to mind is the following code. It’s not complete, but you’ll get the hint on what’s going on.

[php]<?php

// Put the image files in an array with the text
$images = array(“a” => “a.jpg”, “b” => “b.jpg”, “c” => “c.jpg”, …); // Add all the letters, spaces, and symbols you want
// Take the text inputted in the search field and split it character by character
$search = str_split($_POST[‘search’]);

foreach($search as $outtervalue)
{
foreach($images as $innerkey => $innervalue)
{
if($outtervalue == $innerkey)
{
$display .= “”;
}
}
}

echo $display;[/php]

The code above is straightforward. Depending on what character was found in the search string part, it will tell the HTML to display the image file corresponding to the letter (a.jpg, b.jpg, c.jpg, etc). Haven’t tested this code but this should do what you needed to do. To add, you need to put this in the page that your form is pointing to with the method of the form as POST.

Hope this helps.

Thanks for that I will give it a go. Its a good starting point

I think that this line
[php]
$display .= “”;
[/php]
should be
[php]
$display .= “”;
[/php]
and you forget to create $display as an empty string (which results in the concatenation not working)

but a slightly easier version would be
[php]
$str = $_POST[‘seach’];
$images = array(… same array as above …);
$display="";
for($i = 0; $i < strlen($str)l $i++){
if(in_array($str[$i], $array_keys($images)){//if the letter is a key in the images array
$display .= “”;
}
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service