Posted in php
14941
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
546
This Month
2640
This Year
4580

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in php
php redirect echo the page id from wordpress sqlite escape string Deprecated: parse_str(): Calling parse_str() without the result argument is deprecated getcwd usage in php to get the current working directory for file includes write to file php save string to file php using preg_match to extract text from elements encode spaces in items passed to url to %20 not + php function load as table - load_as_table turn on or off error reporting php get json with php from a url and display it php convert number with k on the end to full number extract variables from url string php No 'Access-Control-Allow-Origin' header is present on the requested resource. Fatal error: Uncaught Exception: Required extension GD is not loaded. php check if a class exists Uncaught Error: Call to undefined function mb_convert_encoding() get the subdomain name from the domain string in php - sub domain 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
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 here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
The happiest People don't have the best of everything, they just make the best of everything.
Unknown
Random CSS Property

border-top-left-radius

The border-top-left-radius CSS property rounds the top-left corner of an element by specifying the radius (or the radius of the semi-major and semi-minor axes) of the ellipse defining the curvature of the corner.
border-top-left-radius css reference