NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page Cleaning up SMARTY data

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 05-21-2007, 12:17 PM THREAD STARTER               #1 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



Cleaning up SMARTY data


RESOLVED
Last edited by DylanButler; 05-21-2007 at 02:53 PM.
DylanButler is offline  
Old 05-21-2007, 02:03 PM   #2 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by DylanButler
Hi,
I am building a 'comment section' for my music video web site. It uses SMARTY display templates and I need to clean up the data returned from the DB before parsing it to HTML.

Here is my insert code:
Code:
$name = addslashes($name);
$message = addslashes($message);
$query = mysql_query("INSERT INTO comments (name, message, item_id, date_added) values ('$name', '$message', $item_id, '$date')") or die('Could not insert');
item.php:
Code:
$comments =& DBI::getAll(
    'SELECT c.name, c.message, c.date_added ' .
    'FROM comments c ' .
    'WHERE c.item_id = ' . DBI::quote($id)
    );

Template::set('comments', $comments);

//Tried this and does not work.
Template::set('comments.name', stripslashes($comments['name']));
and the display template:
Code:
{foreach from=$comments item=comment}
<div class="comment">
Name: {$comment.name}<br />
Message: {$comment.message}<br />
<small>Posted on: {$comment.date_added}</small>
</div>
{/foreach}
Is there a way to clean up $comment.name and $comment.message? Do I have to do it from inside the loop? I can't figure out how to make PHP run in the display template.
????: NamePros.com http://www.namepros.com/programming/330362-cleaning-up-smarty-data.html
????: NamePros.com http://www.namepros.com/showthread.php?t=330362

EDIT: Also, how can I strip out malicious code in both fields? Thanks!

Help Appreciated
You could try this, as your template code:
Code:
{foreach from=$comments item=comment}
<div class="comment">
Name: {$comment.name|strip_tags|replace:'\\'':''}<br />
Message: {$comment.message|strip_tags|replace:'\\'':''}<br />
<small>Posted on: {$comment.date_added|strip_tags|replace:'\\'':''}</small>
</div>
{/foreach}
or...
PHP Code:
$comments =& DBI::getAll(
    
'SELECT c.name, c.message, c.date_added ' .
    
'FROM comments c ' .
    
'WHERE c.item_id = ' DBI::quote($id)
    );
$comments array_map('stripslashes'$comments);

Template::set('comments'$comments); 
Eric is offline  
Old 05-21-2007, 02:14 PM THREAD STARTER               #3 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



Originally Posted by SecondVersion
You could try this, as your template code:
Code:
{foreach from=$comments item=comment}
<div class="comment">
Name: {$comment.name|strip_tags|replace:'\\'':''}<br />
Message: {$comment.message|strip_tags|replace:'\\'':''}<br />
<small>Posted on: {$comment.date_added|strip_tags|replace:'\\'':''}</small>
</div>
{/foreach}
or...
PHP Code:
$comments =& DBI::getAll(
    
'SELECT c.name, c.message, c.date_added ' .
    
'FROM comments c ' .
    
'WHERE c.item_id = ' DBI::quote($id)
    );
$comments array_map('stripslashes'$comments);
????: NamePros.com http://www.namepros.com/showthread.php?t=330362

Template::set('comments'$comments); 
Cool cool, but each doesn't quite work.

First one returns this error:
Fatal error: Smarty error: [in item.tpl line 83]: syntax error: unrecognized tag: $comment.name|strip_tags|replace:'\\'':'' (Smarty_Compiler.class.php, line 415) in Smarty.class.php on line 1084

Second one (I'd prefer to do it this way) returns the letter 'A' for all of the results for some reason.

Also, is there anything anyone recommends as far as inserting the data?
Last edited by DylanButler; 05-21-2007 at 02:17 PM.
DylanButler is offline  
Old 05-21-2007, 02:24 PM   #4 (permalink)
Senior Member
 
Eric's Avatar
Join Date: Mar 2005
Posts: 4,948
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Ok, try this then
PHP Code:

function clean(&$value)
{
    if (
is_array($value))
    {
        foreach (
$value AS $key => $val)
        {
            if (
is_string($val))
            {
????: NamePros.com http://www.namepros.com/showthread.php?t=330362
                
$value["$key"] = trim(stripslashes($val));
            }
            else if (
is_array($val))
            {
                
clean($value["$key"]);
            }
        }
    }
}

$comments =& DBI::getAll(
    
'SELECT c.name, c.message, c.date_added ' .
    
'FROM comments c ' .
    
'WHERE c.item_id = ' DBI::quote($id)
????: NamePros.com http://www.namepros.com/showthread.php?t=330362
    );
clean($comments);

Template::set('comments'$comments); 
Edit: also, I'd suggest using www.php.net/mysql_real_escape_string or www.php.net/mysql_escape_string over addslashes when inserting.
Eric is offline  
Old 05-21-2007, 02:53 PM THREAD STARTER               #5 (permalink)
NamePros Regular
 
DylanButler's Avatar
Join Date: Jan 2006
Location: San Diego, CA
Posts: 735
DylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to beholdDylanButler is a splendid one to behold
 



Nice! It worked thanks a lot Eric.

I threw in some strip_tags() in there and we are set.


--RESOLVED--
DylanButler is offline  
Old 05-21-2007, 03:08 PM   #6 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
Join Date: Feb 2006
Posts: 2,796
Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future
 


Autism Autism Autism Autism Autism Autism Autism
I've never used SMARTY or whatever it is, and so that seems kind of pointless to me. It looks like all it's doing is kind of putting code into an HTML file without using PHP. You might as well just use a PHP file and run the code, so it's more flexible. (I'm probably way off on that, but that's how it looks to me.)

I've tried using Django (Python) which uses template files. It seems like a cool idea, but it's easier for me in PHP to just use the dynamic code throughout the template.
Dan is offline  
Old 05-24-2007, 03:15 PM   #7 (permalink)
NamePros Regular
 
abdussamad's Avatar
Join Date: Jul 2006
Location: Karachi
Posts: 791
abdussamad is a glorious beacon of lightabdussamad is a glorious beacon of lightabdussamad is a glorious beacon of lightabdussamad is a glorious beacon of lightabdussamad is a glorious beacon of light
 



Originally Posted by Dan
I've never used SMARTY or whatever it is, and so that seems kind of pointless to me. It looks like all it's doing is kind of putting code into an HTML file without using PHP. You might as well just use a PHP file and run the code, so it's more flexible. (I'm probably way off on that, but that's how it looks to me.)
????: NamePros.com http://www.namepros.com/showthread.php?t=330362

I've tried using Django (Python) which uses template files. It seems like a cool idea, but it's easier for me in PHP to just use the dynamic code throughout the template.
Smarty is easier for non-coders aka designers to learn. The syntax is less strict and it its also more forgiving of mistakes. For example you don't need to end every line with a semi colon and if you refer to a variable that doesn't exist smarty won't choke.

The other advantage is that it allows you to separate the programming logic from the presentation. If you're using PHP for your templates you're allowing each and every designer access to your core code.

The disadvantage, as you pointed out, is that it is harder for the programmer to integrate smarty into his script.
__________________
site unblocker - Computer Hardware
Last edited by abdussamad; 05-24-2007 at 03:55 PM.
abdussamad is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 10:06 PM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger