Jump to content

Head tags


sharke

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.