Posted in php
10
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. 

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>");


	}

*/

View Statistics
This Week
142
This Month
936
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in php
fixing error Call to undefined function simplexml_load_string get the content of a url get the content of a link with php and test if the result is empty php check if url is valid with filter_var php save csv from array to file php export csv from string to browser download convert json string to a php array how to json encode an array in php how to decode xml in php php get file and save it php function to remove the query string from a url using php parse_url function to remove the query string from a url add every value in the load_array array into the out for testing Fix for PHP Warning Trying to get property '*name*' of non-object write the contents of a url into a file with php sqlite fetchArray into another array php return json header and content list_all_array an early stages of list all function that uses an array to pass in main variables replace singular variable assignment with an array loop and variable variables using the $_SERVER['HTTP_REFERER'] to check referring pages timeline class and function php html template class system views list function for checking what views have been made on the current week load array load all items from an array while in a sqlite load sql loop create a 200 character summary from a longer html string using strlen to check the length of a string and do something about it 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
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

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
therock Somewhere along this crazy road I learned (often times the hard way) the most important things I can do is be authentic, trust my gut, be the hardest worker in the room, celebrate the mistakes, be a grateful man and always remember that it's nice to be important, but it's more important to be nice.
The Rock