renfley Posted June 15, 2013 Share Posted June 15, 2013 Hey guys i was wondering is there is any solutions for what i wanna do. Something Like this function Tag(){ $title="Site Name"; } I want to do this.... <title>{$title}</title> Does anyone know what this method is called and if this even uses php or maybe even javascript? Quote Link to comment Share on other sites More sharing options...
requinix Posted June 15, 2013 Share Posted June 15, 2013 (edited) It's templating. Heard of Smarty? That, and others like it. Basically it works by having some PHP code that "evaluates" the template by doing things like "find all instances of {var} and replace them with the value of var". And potentially more advanced stuff. It's web development for people who can't write PHP. Edited June 15, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
renfley Posted June 15, 2013 Author Share Posted June 15, 2013 Thanks you... that answers my question Quote Link to comment Share on other sites More sharing options...
Irate Posted June 15, 2013 Share Posted June 15, 2013 (edited) $title = "Site Name"; echo "<title>$title</title>"; would be easiest, no need to use a function. If you want to fire a dynamic title change event, use JavaScript. This function, for example. function setTitle(title) { document.title = title; } And later on, you can register the function to various event handlers, for example, the .click() jQuery method. Example below. jQuery('#title_button').click(function(e){ e.preventDefault(); // prevent eventual predefined click events, if it is a link or so setTitle(jQuery(this).html()); // title now becomes the button's innerHTML, // but you can set it to any string value you want to, // and consider using the toString() methods }); Edited June 15, 2013 by Irate Quote Link to comment Share on other sites More sharing options...
renfley Posted June 15, 2013 Author Share Posted June 15, 2013 ive decided to save the static informaiton in an array which i have created but havent figured out how to print it, $config = array( "mysql" => array( "DB_NAME" => "dbname", "DB_USER" => "dbuser", "DB_PASS" => "dbpass", "DB_HOST" => "dbhost", ), "paths" => array( "resources" => "/public_html/", "images" => array( "content" => $_SERVER["DOCUMENT_ROOT"] . "/images/content", "layout" => $_SERVER["DOCUMENT_ROOT"] . "/images/layout" ) ), "title" => array( "title" => "Sitename", ) ); So you can see ive save the title in the title array which will work for the task at hand. My issue is when i normally echo out a array echo $i['user']; but in this case the array is in the $config var, How would i go about echoing out the title? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 15, 2013 Share Posted June 15, 2013 Why the second dimension for the title? You could just as easily have just assigned it to the first dimension, seeing as there is only one element in there. That said, what you have there is a two-dimensional array. How to access element in the second (or n'th) dimension is nicely explained in the PHP manual (see example #6). Quote Link to comment Share on other sites More sharing options...
Solution renfley Posted June 15, 2013 Author Solution Share Posted June 15, 2013 I am currently writing a framework and am experimenting with different array configurations, which is why tried the second Dim... I've pretty much gone back to my original method which runs smoothly. //Here is the database information for use throughout the site. //You can use these anywhere -- echo $DB_USER $mysql = array('DB_NAME' => 'dbname' , 'DB_USER' => 'dbuser', 'DB_PASS' => 'dbpass', 'DB_HOST' => 'dbhost' ); //this will take all the keys in the array and allow you to parse them as a variable. // echo $DB_USER; foreach($mysql as $key=>$value) { $$key = $value; } I can then echo any of the keys are variables, <?php echo $DB_USER; ?> which will output dbuser. This has always been my go to method of doing it but wanted to see other methods and realized there aren't really any other ones that are as simple and as effective. Thanks Guys for all the help! Quote Link to comment Share on other sites More sharing options...
Christian F. Posted June 15, 2013 Share Posted June 15, 2013 Using variable variables is bad form regardless of the case, and using variables for configuration values (which tend to be constants) is not recommended either. What I do recommend you to do, is to define constants for the configuration settings. That way you can access them wherever you like, and you get them clearly defined as something different from runtime data. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.