Riseykins Posted June 5, 2008 Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/ Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 Would I need a database for this? Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558481 Share on other sites More sharing options...
discomatt Posted June 5, 2008 Share Posted June 5, 2008 I'm not following... could you give me some pseudo-code? Example if text contains the word 'happy' display image 'happy.jpg' otherwise display a default image Need a few more specifics Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558484 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558487 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 Code entered: <option value='battlejubjub'>Battle JubJub</option> <option value='mibeach'>A Day at the Beach</option> PHP Code(ish): if battlejubjub echo http://images.neopets.com/neoboards/avatars/battlejubjub.gif if mibeach echo http://images.neopets.com/neoboards/avatars/mibeach.gif 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 Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558494 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 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 />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558497 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558500 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 I updated my post above. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558504 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 I'm not having any luck with that. It seems to be doing nothing. *pokes siggy* Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558526 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 Woops my mistake, change: $words_array = explode('_', $words_text); to $words_array = explode(' ', $words_text); Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558530 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 Still nothing. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558533 Share on other sites More sharing options...
discomatt Posted June 5, 2008 Share Posted June 5, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558535 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 <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. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558539 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 Why didn't you tell us that before? Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558542 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 I did say so before. EDIT: I guess it just wasn't clear that where I wrote "Code entered" before quoting I meant that that was the code a person would enter into a text box. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558544 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558547 Share on other sites More sharing options...
Riseykins Posted June 5, 2008 Author Share Posted June 5, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558551 Share on other sites More sharing options...
wildteen88 Posted June 5, 2008 Share Posted June 5, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558557 Share on other sites More sharing options...
discomatt Posted June 5, 2008 Share Posted June 5, 2008 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 />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-558602 Share on other sites More sharing options...
Riseykins Posted June 6, 2008 Author Share Posted June 6, 2008 Still not really having any luck. Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-559028 Share on other sites More sharing options...
wildteen88 Posted June 7, 2008 Share Posted June 7, 2008 Who's code you trying? Quote Link to comment https://forums.phpfreaks.com/topic/108875-ifechomaybe/#findComment-559748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.