Page 1 of 2

Replacing text with smilies! Getting some error!

Posted: Mon Jul 09, 2012 7:46 am
by pHp_n0ob
Hi,I'm a php n0ob want to be a great programmer,thats why doin practice :D

I'm tryin to replace text with smiles bt getting error,check this and plz guide me...

Code: Select all

<?php
$name=$_GET['name'];
$msz=$_GET['msz'];
$submit=$_GET['submit'];
$smilyChar=array(':p',':(',':D',':X');
$smilyName=array('smily/1.gif','smily/2.gif','smily/3.gif','smily/4.gif');
for($i=0;$i<=count($smilyChar);$i++)
$msz=str_replace($smilyChar[$i],'<img src="$smilyName[$i]" alt="$smilyChar[$i]">',$msz);
$msz=str_replace(':p','<img src="2.gif" alt=":p">',$msz);
echo $msz;?>

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 09, 2012 7:58 am
by Celauran
Might be helpful if you actually mentioned what the error says...

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 09, 2012 8:21 am
by pHp_n0ob
Nothing err0r...bt its n0t showing image....check this http://bdhitz.zerve.in/gb/main.php

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 09, 2012 10:16 am
by pickle
variables aren't evaluated in single quotes.

Re: Replacing text with smilies! Getting some error!

Posted: Wed Jul 11, 2012 3:50 am
by TildeHash
There were a few problems with it, but it should work as expected now:

Code: Select all

<form method="GET">
        <input type="text" name="name" value="Name" /><br />
        <input type="text" name="msz" value="Message" />
        <input type="submit" name="submit" value="Submit" /><br /><br />
<form>
<?php
$name = (isset($_GET['name'])) ? $_GET['name'] : 'Anonymous';
$msz = (isset($_GET['msz'])) ? $_GET['msz'] : 'Enter a Message';

$smilyChar = array(':p', ':(', ':D', ':X');
$smilyName = array('1.gif', '2.gif', '3.gif', '4.gif');

if (isset($_GET['msz'])) {
        for ($i = 0; $i != count($smilyChar); $i++) {
                $msz = str_replace($smilyChar[$i], '<img src="smily/' . $smilyName[$i] . '" alt="' . $smilyChar[$i] . '">', $msz);
        }
}
echo $msz . ' - ' . $name;
?>
EDIT: Oh "and guide you". First you shouldn't use "$_GET" nor "$_POST" etc, without checking to make sure they are set first. Variables can't be used within single quotes, use double quotes or break out of the single quotes like I do in the script. You didn't need to put the "smily/" in each of the array values, just in the final "$msz" variable. I noticed the unnecessary "$submit" variable, so I removed it. You also had an unnecessary second "$msz" variable, which I also removed. And you didn't make use of the "$name" variable, so I did. Other than that your method was excellent. :D

Re: Replacing text with smilies! Getting some error!

Posted: Thu Jul 12, 2012 12:53 am
by pHp_n0ob
There were a few problems with it, but it should work as expected now:
thanx...bro,i got everything except the one about for loop

why u used !== in for lo0p...can u plz explain...I know it means not equal to bt cant undrstnd in the code ...and thanks for the guidance also bro :D

Re: Replacing text with smilies! Getting some error!

Posted: Thu Jul 12, 2012 4:05 am
by TildeHash
pHp_n0ob wrote:
There were a few problems with it, but it should work as expected now:
thanx...bro,i got everything except the one about for loop

why u used !== in for lo0p...can u plz explain...I know it means not equal to bt cant undrstnd in the code ...and thanks for the guidance also bro :D
There are three parts to a "for loop", the first is defining a variable, the second is stating a condition under which the loop runs, and the third is the action that is to be performed while the condition isn't met. So while "$i" is not equal to the number of array values in "$smilyChar", the loop increases the value of "$i" until it is equal to the number of array values in "$smilyChar". In this case there are four array values, so the loop will run until "$i" is equal to "4", which is four times. There isn't a need to set the condition to "if greater than" or "if lesser than" nor "if lesser than or equal to" because the "$i" variable will never go past the number of array values in "$smilyChar".

Hope I explained it clearly enough.

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 1:01 am
by pHp_n0ob
Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 4:38 am
by TildeHash
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 6:20 am
by pHp_n0ob
@samrem296 i didnt undrst0od what u said :D simply,i'm using free reseller web hosting cuz i'm learning n0w :D

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 6:23 am
by pHp_n0ob
TildeHash wrote:There were a few problems with it, but it should work as expected now:

Code: Select all

<form method="GET">
        <input type="text" name="name" value="Name" /><br />
        <input type="text" name="msz" value="Message" />
        <input type="submit" name="submit" value="Submit" /><br /><br />
<form>
<?php
$name = (isset($_GET['name'])) ? $_GET['name'] : 'Anonymous';
$msz = (isset($_GET['msz'])) ? $_GET['msz'] : 'Enter a Message';

$smilyChar = array(':p', ':(', ':D', ':X');
$smilyName = array('1.gif', '2.gif', '3.gif', '4.gif');

if (isset($_GET['msz'])) {
        for ($i = 0; $i != count($smilyChar); $i++) {
                $msz = str_replace($smilyChar[$i], '<img src="smily/' . $smilyName[$i] . '" alt="' . $smilyChar[$i] . '">', $msz);
        }
}
echo $msz . ' - ' . $name;
?>
EDIT: Oh "and guide you". First you shouldn't use "$_GET" nor "$_POST" etc, without checking to make sure they are set first. Variables can't be used within single quotes, use double quotes or break out of the single quotes like I do in the script. You didn't need to put the "smily/" in each of the array values, just in the final "$msz" variable. I noticed the unnecessary "$submit" variable, so I removed it. You also had an unnecessary second "$msz" variable, which I also removed. And you didn't make use of the "$name" variable, so I did. Other than that your method was excellent. :D

plz also xplain ? and striplashes...thanx for ur help anyway :)

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 6:31 am
by pHp_n0ob
TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead :)

Re: Replacing text with smilies! Getting some error!

Posted: Mon Jul 16, 2012 6:36 am
by pHp_n0ob
TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead

Re: Replacing text with smilies! Getting some error!

Posted: Wed Jul 18, 2012 11:23 pm
by TildeHash
pHp_n0ob wrote:
TildeHash wrote:
pHp_n0ob wrote:Oh...thanx :p plz tell smthing ab0ut mysql...i'm gettin error...http://topnet.nstop.in/user_db.php
Sorry, I don't use databases (especially not MySQL.) 8)
8O
then how without databasess??? Do u use flat files instead :)
If it's one's own content for one's own site, then any form of separated file storage isn't called for. I do use flat files, but I don't store other people's content or information very often so there is no need for me to use a database nor flat files very often, either. :)

Re: Replacing text with smilies! Getting some error!

Posted: Thu Jul 19, 2012 6:32 am
by pHp_n0ob
TildeHash" wrote:
TildeHash wrote: Sorry, I don't use databases (especially not MySQL.) 8)
8O
If it's one's own content for one's own site, then any form of separated file storage isn't called for. I do use flat files, but I don't store other people's content or information very often so there is no need for me to use a database nor flat files very often, either. :)
and what if you'l have to store data...say u have to make a forum...then what? Where you'l store the data?