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.
PHP
/*
// 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>";
  }
  $template->set("sources",$sources);
	// 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'>
		  <head>
		    <meta charset='utf-8'>
				<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
		    <title>$bundle_title</title>
				<link rel='preconnect' href='https://fonts.gstatic.com'>
				<link href='https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&family=Nunito:wght@400;700&display=swap' rel='stylesheet'>
				<style>
				body {
					background:#222;
					color:#EEE;
					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 {
					margin:0;padding:0;
				}
				.cssbundle-header a {
					font-weight:bold;
					color:#FFF;
				}
				.cssbundle-header {
				    width: 100%;
				    padding: 10px;
				    font-family: sans-serif;
				    text-align: center;
				    background: #111;
						color:#999;
				}
				.main-content {
					padding:20px;
				}
				</style>
				<style>$css_raw</style>
				$scripts_raw
		  </head>
		  <body>
				<div class='cssbundle-header'>
					<a href='https://kruxor.com' target='_blank'>kruxor.com</a>
					//
					<a href='https://kruxor.com/view/code/$class->uid/$nice_title_uid_safe/' target='_blank'>$bundle_title</a>
					//
					<a href='https://kruxor.com/bundle/$nice_title_uid_safe/index.php' target='_blank'>Demo</a>
				</div>
				<div class='main-content'>
				$html_raw
				<?php
					$php_raw
				?>
				</div>
				<script>$javascript_raw</script>
		  </body>
		</html>
		";
		// 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_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>");
	}
*/
 
  