Forum Moderators: coopster

Message Too Old, No Replies

Replaceing some value with datbase fields or value

Replaceing some value with datbase fields or value

         

vivek avasthi

1:14 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



Replaceing some value with datbase fields or value ,

i have a Text area box for sending mail ..i just want that uwhen i enter text i want to include some field name here then at the time of sending extract these fields name from here take value from database then display value in place of field name..

See the example of text ..The Value with % % sign is field names..Please help me

hi dear %PERS_name%,

how r u , %PERS_name% i am sending u your details : like

company name : %PERS_company% ,

last name : %PERS_last_name%

Address : %PERS_address1%

City : %PERS_city%

Post Code : %PERS_postcode%

eelixduppy

2:32 pm on Jul 18, 2006 (gmt 0)



Maybe something like this?:

$text = $_POST['email_text']; //text from form
$replace = [url=http://us2.php.net/manual/en/function.array.php]array[/url]("%PERS_name%","%PERS_last_name%","%PERS_company%","%PERS_address1%","%PERS_city%","%PERS_postcode%");
//connect to mysql; select db
$query = "select * from table where col = 'something'";
$result = [url=http://us2.php.net/manual/en/function.mysql-query.php]mysql_query[/url]($query) or [url=http://us2.php.net/manual/en/function.die.php]die[/url]([url=http://us2.php.net/manual/en/function.mysql-error.php]mysql_error[/url]);
$row = [url=http://us2.php.net/manual/en/function.mysql-fetch-array.php]mysql_fetch_array[/url]($result); //should be one result
$replace_with = [url=http://us2.php.net/manual/en/function.array.php]array[/url]($row['name'],$row['last_name'],$row['company'],$row['address1'],$row['city'],$row['postcode']);
[url=http://us2.php.net/manual/en/function.mysql-close.php]mysql_close[/url]();
$text = [url=http://us2.php.net/manual/en/function.str-replace.php]str_replace[/url]($replace,$replace_with,$text);
echo $text;

I think this should work as you want, although I haven't tested it. Good luck!

vivek avasthi

2:51 pm on Jul 18, 2006 (gmt 0)

10+ Year Member



Actually this is not the kind of solution i am looking for ..i don't know how many fields will be in text area.it might be 7-8 of it might be 0 tooo.. so i think first i need to get the value with % % sign from the text area . then extract these value from datbase. Can anyone tell me Extact method of these fields from the textarea box .?

eelixduppy

2:54 pm on Jul 18, 2006 (gmt 0)



I'm not sure if I understand what you are getting at. str_replace replace all occurrences of the search string with the replacement string. Meaning, that is doesn't matter how many times the %thingy% is written, because all occurrences will be changed to its database value.

whoisgregg

3:55 pm on Jul 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So long as you include all the fields that could be in the DB, str_replace will effectively "skip" the fields that aren't in the email template.