Posted in php
4:23 am, August 26, 2021

creating embedded php code from a database field

i actually dont know why i didnt think of doing this earlier, i already have the php code field in these code articles.

I thought why not embed this code into an actual file and then run it as a test or in an iframe.

Now i can do this! so every code post that contains php will add a working (or non working) demo file, so you can see if the code actually works, and all of this is automated. 

i have had to comment out the code for this demo as it references variables that are not available in the standalone demo. 

Update: so i had this working on the view page, so every time the page is viewed this is re-creating and writing the file, so i guess this is not a great idea as it only really needs to be updated when the page is saved, So i will move the file writing code from the view page to the update page. 


// add sources to template
	// can add an auto index to jump to sections.

	$sources = "";
	$html_safe = "";
	$html_raw = "";
  if($class->html > "") {
    $html_safe = htmlentities($class->html);
		$html_raw = $class->html;
    $sources .= "<h2 id='html'>HTML</h2><pre><code class='html'>$html_safe</code></pre>";
	$css_safe = "";
	$css_raw = "";
  if($class->css > "") {
    $css_safe = htmlentities($class->css);
		$css_raw = $class->css;
    $sources .= "<h2 id='css'>CSS</h2><pre><code class='css'>$css_safe</code></pre>";
	$scripts_safe = "";
	$scripts_raw = "";
  if($class->scripts > "") {
    $scripts_safe = htmlentities($class->scripts);
    $scripts_raw = $class->scripts;
    $sources .= "<h2 id='scripts'>Scripts</h2><pre><code class='html'>$scripts_safe</code></pre>";
	$javascript_safe = "";
	$javascript_raw = "";
  if($class->javascript > "") {
    $javascript_safe = htmlentities($class->javascript);
    $javascript_raw = $class->javascript;
    $sources .= "<h2 id='javascript'>Javascript</h2><pre><code class='javascript'>$javascript_safe</code></pre>";
	$php_safe = "";
	$php_raw = "";
  if($class->php > "") {
    $php_safe = htmlentities($class->php);
    $php_raw = $class->php;
    $sources .= "<h2 id='php'>PHP</h2><pre><code class='php'>$php_safe</code></pre>";

	// if there is php then write it to a test php file, like cssbundle.
	// add a test link maybe an iframe embed for it.
	$template->set("php_iframe", "");
	if($class->php > "") {

		$bundle_title = htmlentities($class->title);
		$nice_title_uid = $functions->encode_title_uid($class->title);
		$nice_title_uid_safe = htmlentities($nice_title_uid);

		$php_raw = $class->php;

		$bundle_template = "<!DOCTYPE html>
		<html lang='en' dir='ltr'>
		    <meta charset='utf-8'>
				<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
				<link rel='preconnect' href=''>
				<link href=';700&family=Nunito:wght@400;700&display=swap' rel='stylesheet'>
				body {
					font-family:'Nunito', sans-serif;
				h1,h2,h3,h4,h5,h6 {
					font-family:'Montserrat', sans-serif;
				* {
					box-sizing: border-box;
					transition: all 0.2s;
				html,body {
				.cssbundle-header a {
				.cssbundle-header {
				    width: 100%;
				    padding: 10px;
				    font-family: sans-serif;
				    text-align: center;
				    background: #111;
				.main-content {
				<div class='cssbundle-header'>
					<a href='' target='_blank'></a>
					<a href='$class->uid/$nice_title_uid_safe/' target='_blank'>$bundle_title</a>
					<a href='$nice_title_uid_safe/index.php' target='_blank'>Demo</a>
				<div class='main-content'>
		// make a new folder with the uid or title as a uid...
		// using the title might be nicer.
		$current_working_directory = getcwd();
		$directory_to_create = $current_working_directory . "/bundle/" . $nice_title_uid;
		if(!is_dir($directory_to_create)) {
		// mkdir("directory_name");

		$file_to_save_bundle = $directory_to_create . "/index.php";

		file_put_contents ( $file_to_save_bundle , $bundle_template );

		// echo $file_to_save_bundle;

		$demo_bundle_file_link = "/bundle/" . $nice_title_uid . "/index.php";

		// file to save to.
		// file_put_contents ( "my_file_name.txt" , $my_string );

		$template->set("php_iframe", "<iframe src='$demo_bundle_file_link' style='width:100%;height:600px;'></iframe>");



View Statistics
This Week
This Month
This Year

No Items Found.

Add Comment
Type in a Nick Name here
Other Items in php
How to record your own page views with PHP, and make them into weekly monthly and yearly charts load from fields array php class function get the current week as a number with php creating embedded php code from a database field test php bundle write check if a file exists with php check if the file is a directory or check if the directory exists in php get the current working directory in php create directory with php create folder with php check if a product already exists by its md5 load random videos module using template PHP/SQLite - Load Random Item split a string into links using the comma extract youtube image from video url simple php ip blocker preg replace clean a string only allow a-zA-Z0-9 characters get last month as a number with php verify the google recapture server php rtrim strip white space or strings from the end of a string get the current month as a number show the difference between two dates in years, months, days, hours and seconds counting the occurrence of words in a multidimensional array write string contents to a file with php unable to access the $_FILES when submitting a form php check column exists in table sqlite show all methods or functions in a class (class function version) show all methods (functions) in a class adding an item to an array with php passing in arrays to your functions rather than using variables check server https or http value in php using $_SERVER Remove background image php with image magic get current url with php list a functions variables or arguments php list all the functions in a class php print an array nicely add a toast alert when logging in with half moon dont use md5 for password hashing using password_hash and password_verify php basic page router checking for spam comments function calculate a percentage (dec) difference based on two numbers explode a string into an array using gzdeflate on a string php include tinymce only if a user is logged into the site Quick tool to convert time into decimal generating all css colours with php? php countries array to dropdown form element Fix for Fatal error: Uncaught Error: Call to undefined function file_get_dom() simplehtmldom simple_html_dom Fixing PHP SQLite database is locked warning - Unable to execute statement: database is locked simple usage for php password_hash change string to capital words ucwords
Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!


Random Quote
For a long time it had seemed to me that life was about to begin - real life. But there was always some obstacle in the way, something to be gotten through first, some unfinished business, time still to be served, or a debt to be paid. Then life would begin. At last it dawned on me that these obstacles were my life.
Alfred D. Souza