Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.