IF statement

Discussion in 'PHP' started by redneckheaven, Jan 14, 2010.

  1. #1
    I am working on a guestbook in flash with php. I have got it where when a person clicks on a smiley/emoction it puts in a symbol. Now what i want to know is can I use and if statement to change the symbol to an img? I am not using a databased guestbook but use a text file where info is stored then put into guestbook. Not sure if this would work

    If ($Comments = :banana)
    { img src = "images/banana.gif"
    }

    Hopefully someone can lend a helping hand. Only thing left to get guestbook done.


    Thanks In Advance

    Kevin
     
    redneckheaven, Jan 14, 2010 IP
  2. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #2
    Can be done but just AFTER form submission (PHP is server side, so the changes will be visible AFTER the page is reloaded)...

    I'd try something like :
    
    <?
    if ($comments = "banana") {
    $img_src = "images/banana.gif";
    }
    // end of PHP here, HTML coming
    ?>
    <img src=<?=$img_src;?> />
    
    Code (markup):
     
    dsignresponder, Jan 14, 2010 IP
  3. redneckheaven

    redneckheaven Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I tried that but didnt work. When I put in the if statement only thing that showed up in guestbook was the word banana no other text with it. Here is the php code I am using
    
    <?php
    $Submit 	= $_POST["Submit"];
    $Name 	        = $_POST["Name"];
    $Email 	        = $_POST["Email"];
    $Website 	= $_POST["Website"];
    $Comments 	= $_POST["Comments"];
    $State          = $_POST["State"];
    $NumLow 	= $_REQUEST["NumLow"];
    $NumHigh 	= $_REQUEST["NumHigh"];
    
    $Name 	= ereg_replace("[^A-Za-z0-9 ]", "", $Name);
    $Email 	= ereg_replace("[^A-Za-z0-9 \@\.\-\/\']", "", $Email);
    $Website 	= eregi_replace("http://", "", $Website);
    $Website 	= ereg_replace("[^A-Za-z0-9 \@\.\-\/\'\~\:]", "", $Website);
    $State 	= ereg_replace("[^A-Za-z0-9 ]", "", $State);
    
    $Comments = eregi_replace('&', ' and ',$Comments);
    $Comments = eregi_replace('<', '*',$Comments);
    $Comments = eregi_replace('>', '*',$Comments);
    $Comments = eregi_replace('fk', '****',$Comments);
    $Comments = eregi_replace('st', '****',$Comments);
    $Comments = eregi_replace('bih', '****',$Comments);
    $Comments = eregi_replace('bad', '****',$Comments);
    $Comments = eregi_replace('ct', '****',$Comments);
    $Comments = eregi_replace('slt', '****',$Comments);
    $Comments = eregi_replace('cm', '****',$Comments);
    $Comments = eregi_replace('whe', '****',$Comments);
    $Comments = eregi_replace('nir', '****',$Comments);
    $Comments = eregi_replace('gl', '@#*!#%',$Comments);
    $Comments = eregi_replace('sk', '****',$Comments);
    $Comments = eregi_replace('se', '$@*%!#',$Comments);
    $Comments = eregi_replace('dk', '****',$Comments);
    $Comments = eregi_replace('py', '****',$Comments);
    $Comments = eregi_replace('dd', '$@*%!#',$Comments);
    $Comments = eregi_replace('ps', '****',$Comments);
    $Comments = eregi_replace('we', '****',$Comments);
    $Comments = eregi_replace('nr', '#@%!$',$Comments);
    
    
    $Name 	= stripslashes($Name);
    $Email 	= stripslashes($Email);
    $Website 	= stripslashes($Website);
    $State 	= stripslashes($State);
    $Comments 	= stripslashes($Comments);
    
    
    
    if ($Submit == "Yes") {
    
    	$filename 	= "GuestBook.txt";
    
    // Opens up the file declared above for reading 
    
    	$fp 		= fopen( $filename,"r"); 
    	$OldData 	= fread($fp, 80000); 
    	fclose( $fp ); 
    
    // Gets the current Date of when the entry was submitted
    	$Today 		= (date ("l F dS Y ( h:i:s A )",time()));
    
    // Puts the recently added data into html format that can be read into the Flash Movie.
    // You can change this up and add additional html formating to this area.  For a complete listing of all html tags
    // you can use in flash - visit: http://www.macromedia.com/support/flash/ts/documents/htmltext.htm
    
    	$Input = "Name: <b>$Name</b>\nEmail: <b><u><a href=\"mailto:$Email\">$Email</a></u></b>\nWebsite: <b><u><a href=\"http://$Website\" target=\"_blank\">$Website</a></u></b>\nState: <b>$State</b>\nComments: <b>$Comments</b>\n <i><font size=\"-1\">\nDate: $Today</font>.:::.\n\n";
    
    /* This Line adds the '&GuestBook=' part to the front of the data that is stored in the text file.  This is important because without this the Flash movie would not be able to assign the variable 'GuestBook' to the value that is located in this text file  */
    
    	$New = "$Input$OldData";
    
    // Opens and writes the file.
    
    	$fp = fopen( $filename,"w"); 
    	if(!$fp) die("&GuestBook=cannot write $filename ......&");
    	fwrite($fp, $New, 800000); 
    	fclose( $fp ); 
    }
    
    // ###################################################################################
    // ######### Formatting and Printing the Data from the Guestbook to the Flash Movie ##
    
    
    
    // Next line tells the script which Text file to open.
    	$filename = "GuestBook.txt";
    
    // Opens up the file declared above for reading 
    
    	$fp 	= fopen( $filename,"r"); 
    	$Data 	= fread($fp, 800000); 
    	fclose( $fp );
    
    // Splits the Old data into an array anytime it finds the pattern .:::.
    	$DataArray = split (".:::.", $Data);
    
    // Counts the Number of entries in the GuestBook
    	$NumEntries = count($DataArray) - 1;
    
    	print "&TotalEntries=$NumEntries&NumLow=$NumLow&NumHigh=$NumHigh&GuestBook=";
    	for ($n = $NumLow; $n < $NumHigh; $n++) {
    	print $DataArray[$n];
    		if (!$DataArray[$n]) {
    			Print "<br><br><b>No More entries</b>";
    		exit;
    		}
    	}
    ?>
    
    PHP:
    Now this works just fine posting to guestbook which is in flash. Just trying to figure out how to get the image to show up in the guestbook from a word like :banana to img src = "images/banana.gif. Have tried to use
    $Comments = eregi_replace(':banana', "<img src='images/banana.gif'>", $Comments); Also tried $Comments = str_replace(':banana', "<img src='images/banana.gif'>", $Comments);

    Using either one of those the text shows up minus the word :banana but no image. For instance if I typed in the Comments box Test Test (click on banana smiley) shows :banana in Comments box then typed Test Test Only Test Test Test Test showed up in guestbook.
     
    redneckheaven, Jan 15, 2010 IP
  4. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #4
    I'm really sorry, there's a typing error in my previous post...... the correct if statement should be :
    
    <?
    if ($comments == "banana") {
    $img_src = "images/banana.gif";
    }
    // end of PHP here, HTML coming
    ?>
    <img src=<?=$img_src;?> />
    
    Code (markup):
    the only change is :
    
    $comments == "banana"
    
    Code (markup):
    Sorry about that.....
    Let me know if you still have problems....
    Regards
     
    dsignresponder, Jan 15, 2010 IP
  5. redneckheaven

    redneckheaven Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok tried that and the image didnt show up. The image is in the images folder.
    All text showed up along with banana. What I typed Test Test (clicked on smiley and banana was inputed) Test Test What showed up in guestbook was Test Test banana Test Test
     
    redneckheaven, Jan 15, 2010 IP
  6. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #6
    And what about the rest of the replacements? Are they working correctly? Do you have some more replacements with images or the only image is for banana? Can I see this example online?
     
    dsignresponder, Jan 16, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Just a side note, eregi is deprecated as of PHP 5.3.0 and completely removed in PHP6 so you should consider using preg instead. better still use arrays and just pass the two arrays to str_ireplace().
     
    JAY6390, Jan 16, 2010 IP
  8. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    Not sure if I understand your question completely correct but here's a possible solution to how I've interpreted it.

    I'm thinking you want to convert the emotes back into image representations when displayed in a browser?

    I've created a function inside an include file to do this for me - here's a small portion of what it looks like, this will convert things like :) ;) :lol: into small images contained within the images/emotes folder...

    function bbc2html($data) {
      $data=preg_replace('¬\[b](.*?)\[/b]¬is','<b>\\1</b>',$data);
      $data=preg_replace('¬\[i](.*?)\[/i]¬is','<i>\\1</i>',$data);
      $data=preg_replace('¬\[u](.*?)\[/u]¬is','<u>\\1</u>',$data);
      $data=eregi_replace(":)",'<img src="images/emotes/smile.gif">',$data);
      $data=eregi_replace(":lol:",'<img src="images/emotes/lol.gif">',$data);
      $data=eregi_replace(":@",'<img src="images/emotes/mad.gif">',$data);
      return $data;
    }
    PHP:
    You'll notice there are 3 lines to support bold, italics and underline there as well.

    Use it something like this:
    echo bbc2html($comment);
    PHP:
     
    Yesideez, Jan 16, 2010 IP
  9. redneckheaven

    redneckheaven Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Right now I am only trying to get one smiley to show up before I do the rest. I have like 20 smileys/emoctions on the sign GB page. I will go and make the GB live. Everything is complete except for the smileys sliding away when click send so do pay any attention to them staying in place after submit. If you click on any smiley it will put in the Comments box the word/code for the smiley. The add more smiley link isnt done yet either. You can go to redneckheaven dot net to check it out.
     
    redneckheaven, Jan 16, 2010 IP
  10. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #10
    @yesideez

    On a side note, consider using preg_replace() instead of eregi_replace()

     
    danx10, Jan 16, 2010 IP
  11. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #11
    If I were You, I would FIRST try to see if my script recognize the "banana" input.
    I.E. :
    
    if ($Comments == :banana)
    { 
    echo "You typed BANANA!";
    }
    else
    {
    echo "There is NO BANANA!";
    }
    
    Code (markup):
    If that code behaves as it is supposed to, it's a piece of cake to include the banana.gif
    However, I think your problem is somewhere in the "$Comments = eregi_replace..." section.... Have you tried with preg_replace() ?
     
    dsignresponder, Jan 18, 2010 IP
  12. redneckheaven

    redneckheaven Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I tried the code with echo and with no " " around :banana nothing shows up in guestbook, no new entries or previous entries. With the " " around :banana entry gets put in guestbook but with :banana. Yes I have tried preg_replace. Such as-- $Comments = preg_replace(':banana', '/images/banana.gif',$Comments); When I get in this evening will try again.
     
    redneckheaven, Jan 18, 2010 IP
  13. redneckheaven

    redneckheaven Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ok tried $Comments = preg_replace(":banana",'<img src="images/banana.gif">' ,$Comments); No Comments showed up at all, Typed in Test then clciked smiley for banana Then Test.
    Tried $Comments = eregi_replace(":banana",'<img src="images/banana.gif">' ,$Comments); Typed in Test then clicked on smiley for banana and Only Test showed up in guestbook.
    Went and check Txt file this is what showed up:

    Name: <b>Kevin</b>
    Email: <b><u><a href="mailto:qq@aol.com">qq@aol.com</a></u></b>
    Website: <b><u><a href="http://" target="_blank"></a></u></b>
    State: <b>WV</b>
    Comments: <b>Test <img src="images/banana.gif"></b>
    <i><font size="-1">
    Date: Monday 18th 2010f January 2010 ( 04:18:45 PM )</font>.:::.
     
    Last edited: Jan 18, 2010
    redneckheaven, Jan 18, 2010 IP
  14. dsignresponder

    dsignresponder Greenhorn

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #14
    Well.... As I'm not a flash programmer/designer, I solved your issue in PHP with some JavaScript. Hopefully it will help you to locate and remove your bug...

    Here's the preview (very basic, without validating and formatting, just to demonstrate the function) :
    Redneck Guestbook

    Here's the JavaScript (insert it into the head section of your page)
    
    <script type="text/javascript">
            function insertAtCursor(myField, myValue) {
                if (document.selection) {//IE support
                    myField.focus();
                    sel = document.selection.createRange();
                    sel.text = myValue;
                }
                else if (myField.selectionStart || myField.selectionStart == '0') {//MOZILLA/NETSCAPE support
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
                } else {
                    myField.value += myValue;
                }
                return false;
            }
        </script>
    
    Code (markup):
    and the PHP :
    
    <?
    
    $new_name = $_POST['name'];
    $new_email = $_POST['email'];
    $new_website = $_POST['website'];
    $new_location = $_POST['location'];
    $new_comment = $_POST['comment'];
    
    if (isset($_POST['send_comment'])) { 
    
    $add_comment = "\r\n" .$new_name. '; ' .$new_email. '; '.$new_website. '; ' .$new_location. '; ' .$new_comment;
    
    $fp = @fopen('comments.csv','a') or error('Can\'t write to the Comments file! CHMOD it to 666 on LINUX machines!');
    flock($fp, LOCK_EX);
    fputs($fp, $add_comment);
    flock($fp, LOCK_UN);
    fclose($fp);
    
    }
    ?>
    
    <table width="80%" cellpadding="4" cellspacing="2" border="1px">
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="comments_form">
    <tr>
    	<td colspan="2">
    		<p><b>* Required info</b></p>
    	</td>
    	<td>
    		<p><b>Comments</b></p>
    	</td>
    </tr>
    <tr>
    </tr>
    <tr>
    	<td>
    		<p style="display:inline"><b>Name * : </b></p>
    	</td>
    	<td>
    		<input type="text" name="name" size="20" />
    	</td>
    	<td rowspan="7">
    <?
    $comments_data = file('comments.csv') or die('Could not read the file!');
    
    for ($i = 0; $i < sizeof($comments_data); $i++) {
    
    	$line      = trim($comments_data[$i]);
    	$line_data = explode("; ", $line);
    	
    	$name     = $line_data[0];
    	$email    = $line_data[1];
    	$website  = $line_data[2];
    	$location = $line_data[3];
    	$comment  = $line_data[4];
    	
    	$patterns = array();
    	$patterns[0] = '/banana/';
    	$replacements = array();
    	$replacements[0] = '<img src="images/banana.gif" width="30px" />';
    	
    	$comment = preg_replace($patterns, $replacements, $comment);
    	
    	echo "<b>Name : </b>".$name."<br />";
    	echo "<b>Email : </b>".$email."<br />";
    	echo "<b>Website : </b>".$website."<br />";
    	echo "<b>Location/State : </b>".$location."<br />";
    	echo "<b>Comment : </b>".$comment."<br />";
    	echo "<hr />";
    	
    	}
    ?>
    	
    	</td>
    	
    </tr>
    <tr>
    	<td>
    		<p style="display:inline"><b>E-mail * : </b></p>
    	</td>
    	<td>
    		<input type="text" name="email" size="20" />
    	</td>
    </tr>
    <tr>
    	<td>
    		<p style="display:inline"><b>Website : </b></p>
    	</td>
    	<td>
    		<input type="text" name="website" size="20" />
    	</td>
    </tr>
    <tr>
    	<td>
    		<p style="display:inline"><b>State/Location : </b></p>
    	</td>
    	<td>
    		<input type="text" name="location" size="20" />
    	</td>
    </tr>
    <tr>
    	<td>
    		<p style="display:inline"><b>Comments * : </b></p>
    	</td>
    	<td>
    		<textarea name="comment" rows="4" cols="30" wrap="virtual" OnChange="setCursorPos()" OnClivk="setCursorPos()"></textarea>
    	</td>
    </tr>
    <tr>
    	<td colspan="2">
    		<a href="javascript:;" onclick="insertAtCursor(document.comments_form.comment, 'banana');"><img src="images/banana.gif" width="30px" border="0px" /></a>
    	</td>
    </tr>
    <tr>
    	<td colspan="2">
    		<input type="submit" value="Send" name="send_comment" />
    	</td>
    </tr>
    	</form>
    </table>
    
    PHP:
    Hope it will help you out! Let me know if you need help with that! ;)
     
    dsignresponder, Jan 19, 2010 IP