sharke Posted December 7, 2009 Share Posted December 7, 2009 Hi guys, i have created simple class for head tags... head_tags.php <?php class head_tags { var $tags; // add css - javascript function add_head($tag) { if(!empty($tag) && !stristr($this->tags, $tag)){ for($i=0; $i < strlen($tag); $i++){ $this->tags = $tag."\n"; } } } // write new css - js code function print_head() { echo $this->tags; } } ?> header.php <html><head> and other html tags .... <?php $head_tags->print_head(); ?> </head></html> index.php <?php require_once("head_tags.php"); $head_tags = new head_tags(); $head_tags->add_head("<script type=\"text/javascript\" src=\"js/jquery.js\"></script>"); (this working propertly) if i use this second code (<script type=\"text/javascript\" src=\"js/func.js\"></script>) is not inserted in head tags. $head_tags->add_head("<script type=\"text/javascript\" src=\"js/jquery.js\"></script>"); $head_tags->add_head("<script type=\"text/javascript\" src=\"js/func.js\"></script>"); I must use this for add more javascript or css tags. $head_tags->add_head(" <script type=\"text/javascript\" src=\"js/jquery.js\"></script> <script type=\"text/javascript\" src=\"js/func.js\"></script>"); ?> i need a solution for using more $head_tags->add_head(*****); Link to comment https://forums.phpfreaks.com/topic/184323-head-tags/ Share on other sites More sharing options...
premiso Posted December 7, 2009 Share Posted December 7, 2009 I do not understand this portion: for($i=0; $i < strlen($tag); $i++){ $this->tags = $tag."\n"; } Why are you using a for loop here and why are you writing it character by character? This would be much easier: $this->tags .= $tag."\n"; So your code should look like: function add_head($tag) { if(!empty($tag) && !stristr($this->tags, $tag)){ $this->tags .= $tag."\n"; } The reason for the .= is that is appending the new information to the end of $this->tags string, the way you had it before was overwriting the string each time, I am actually surprised it worked for the first item. Link to comment https://forums.phpfreaks.com/topic/184323-head-tags/#findComment-973114 Share on other sites More sharing options...
sharke Posted December 7, 2009 Author Share Posted December 7, 2009 yes this is correctly ($this->tags .= $tag."\n" Thank you premiso! Resolved! Link to comment https://forums.phpfreaks.com/topic/184323-head-tags/#findComment-973117 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.