so i have a page called addpages with the following

mysql_query("INSERT INTO pages 
	(name, title, description, keywords, content, updated) VALUES('$name', '$title', '$description', '$keywords', '$content', '$updated' ) ") 
	or die(mysql_error());
	
	// Get Last ID
	
	$lastid = mysql_insert_id();
	
	// Start Create php page file
	
	$filename = "$name.php";
	$template = file_get_contents("templates/default.php");
	$filehandle = fopen($filename, 'w') or die("can't open file");
	$add = fopen($filename, 'a') or die("can't open file");
	$stringData = "data goes here...";

	fwrite($filehandle, $template);
	fwrite($add, $stringData);
	fclose($filehandle);
	
	// End Create php page file

when the above script has been posted a .php file will be created with the contents of templates/default.php in it, the following is the template:

<?php
include 'core/config.php';
include 'core/opendb.php';

$query = "SELECT name, title, description, keywords, content, updated ". "FROM pages ". "WHERE id = ''";
$result = mysql_query($query) or die('Error : ' . mysql_error());
list($name, $title, $description, $keywords, $content) = mysql_fetch_array($result, MYSQL_NUM);

include 'core/closedb.php';
?>

is there any way to get the $lasid from the first code into the template once its creates the page pls ?

Member Avatar for diafol

I assume you need to get the $lastid variable to the following clause:

$query = "SELECT name, title, description, keywords, content, updated ". "FROM pages ". "WHERE id = ''";

so like this:

$query = "SELECT name, title, description, keywords, content, updated ". "FROM pages ". "WHERE id = '{$lastid}'";

If so, use sprintf.

1. Change template file to:

$query = "SELECT name, title, description, keywords, content, updated ". "FROM pages ". "WHERE id = '%s'";

if id is a string, if it in an autonumber/integer, change it to %d:

$query = "SELECT name, title, description, keywords, content, updated ". "FROM pages ". "WHERE id = '%d'";

2. Place template file contents into a string variable and replace 'placeholder' with variable value:

$cnt = file_get_contents('default.php');
$cnt = sprintf($cnt,$lastid);

3. Place string into new file and save.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.