Jump to content

If...Echo...Maybe?


Riseykins

Recommended Posts

Okay, so what I want to know how to do is create a script where if you enter in some text, and certain things are included, images corresponding  to the specific text will be displayed.

 

There are about 350 images in total, all with corresponding text.

 

I want not just one image to show, but every image to be displayed for as many corresponding pieces of text that have been entered.

 

Also, if any text that does not have a corresponding image is entered, it would need to be ignored completely.

 

Does this even make sense? xD

Link to comment
Share on other sites

How would you know whether an image corresponds to a piece of text? You'll need to post some example data.

 

As for whether to use a database it is up to you. A flat file database may be best. But I cant really suggest a solution until I understand your question further.

Link to comment
Share on other sites

Code entered:

<option value='battlejubjub'>Battle JubJub</option>

<option value='mibeach'>A Day at the Beach</option>

 

PHP Code(ish):

 

Everything else there will cause nothing extra to be displayed, so just the two images next to each other would be shown.

 

		$word = $_POST['word'];
	$x = strlen($word);

	for ($i=0; $i<$x; $i++) {
		$c = $word[$i];
		if ($c == ' ') {
			echo '<br />';
		} else {
			// echo "$c";
			echo "<img src='http://images.neopets.com/neoboards/avatars/$c.gif' />";
		}
	}

 

What in the above code is making the script turn each individual letter into it's own image? xD

Link to comment
Share on other sites

Is the code part of an HTML form which is a pull down menu? The user selects an image name from the menu, submits the form and whaterver was selected from the menu is displayed?

 

EDIT: I see what you're doing to now. You should use explode to separate each word. eg:

$words_text = $_POST['word'];

$words_array = explode('_', $words_text);

foreach($words_array as $word)
{
    $image = 'http://images.neopets.com/neoboards/avatars/' . $word . '.gif';

    if(file_exists($image))
    {
         echo '<img src="'.$image.'" /><br />';
    }

}

Link to comment
Share on other sites

No, it's not. The person will paste some code into a box and then the script will pick out certain image names from the list and give a list of these images.

 

Hopefully it will give a list of the images that are not present (out of the total list of images). That's the next step. xD

Link to comment
Share on other sites

Well, first things first, the user can throw in anything he wants in post data, so you want to specify as results that could possibly be returned. It's not very dangerous in this situation, but it's good practise, and a good habit to get in to.

 

$expected = array(
      'battlejubjub',
      'mibeach',
      'anotherImage',
      'etc'
)

 

You have to format your select box like so

 

<select name="img[]" multiple="true" size="3">
<option value="battlejubjub">Battle Jub Jub</option>
<option value="mibeach">MI Beach</option>
<option value="anotherImage">Another Image</option>
<option value="etc">Etcetera</option>
</select>

 

The [] after the select name is key... it tells PHP to expect multiple results from that element, and populate it as an array. With that, and the above code, you can use this:

 

<?php

foreach( $_POST['img'] as $img )
# Verify it's a legit request
if (  in_array( $img, $expected ) && file_exists( '/path/to/img/'. $img .'.jpg' )  )
	echo '<img src="img/'. $img .'.jpg" alt="" /><br />';

?>

 

Ta daaaaaa

Link to comment
Share on other sites

<select name="img[]" multiple="true" size="3">
<option value="battlejubjub">Battle Jub Jub</option>
<option value="mibeach">MI Beach</option>
<option value="anotherImage">Another Image</option>
<option value="etc">Etcetera</option>
</select>

 

This wouldn't work because people would need to paste a source code into a text box, and the script finds the image names in the code and echoes the images corresponding.

 

EDIT: Sorry, maybe I didn't make it clear. Above where I posted part of a dropdown box, I was just posting part of a source code containing the image names in question and there would not be a dropdown list involved in the script I'm trying to get going.

Link to comment
Share on other sites

No, it's not. The person will paste some code into a box and then the script will pick out certain image names from the list and give a list of these images.

 

Hopefully it will give a list of the images that are not present (out of the total list of images). That's the next step. xD

Sorry I didn't see this post.

Link to comment
Share on other sites

Eh. Sorry! It's pretty hard to explain anyways.

 

I don't know any technical words. I've been working with HTML and whatnot for five years but because I'm totally self taught, I don't know what the name for anything is!

Link to comment
Share on other sites

OK done a little testing.

<?php

if(isset($_POST['submit']))
{
    $word_list = $_POST['word'];

    preg_match_all('|(\s+)<option value="([a-z]+)">([a-z ]+)</option>|is', $word_list, $matches);

    foreach($matches[2] as $image_name)
    {
        $image = 'http://images.neopets.com/neoboards/avatars/' . $image_name . '.gif';

        if(file_exists($image))
        {
            echo '<img src="'.$image.'" /><br />';
        }
        else
        {
            echo $image . ' does not exist<br />';
        }
    }

    echo '<br />';
}

?>
<form method="post">
  Code:<br />
  <textarea name="word" cols="60" rows="10"><?php echo @$_POST['word']; ?></textarea><br />
  <input type="submit" name="submit" value="Get Images" />
</form>

Link to comment
Share on other sites

Oh, then you want THISSS

 

<?php

$string = <<<HEREDOC

This is a bunch of text. I've thrown in a bit of punctuation too,
so you can see how the code below reacts to it; You could've used
a preg_split, but I like doing it this way.

HEREDOC;

if (  preg_match_all( '/([\w]++)/', $string, $words ) == 0  )
exit( 'No word characters found in string, or regular expression failed (doubt it!)' );

foreach ( $words[1] as $word ) {
$img = 'path/to/img/' . strtolower($word) . '.jpg';
if (  file_exists( $img )  )
	echo '<img src="' . $img . '" alt="" /><br />';
else
	# For debugging only
	echo $img . ' doesn\'t exists! Not gonna output anything (except this)<br />';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.