2

I'm building a CMS and I'm stuck on this problem. I need to write a simple php file for every page and I need to pass an ID and the include function. Here is my code to write the file:

 $filename=mysqli_fetch_array($query))['pagename'];
    $fw=fopen("../".$filename,'w',true);
    fwrite( "<php 
                \$id = $id;
                include('admin/renderpage.php');
            ?>");
    fclose($fw);

and this is the result, which looks like what I need, only that the variable $id is not an actual variable and include function doesn't work either.

<php 
            $id = 2;
            include('admin/renderpage.php');
            ?>
9
  • Not an answer, but why are you doing this? What value do these nearly identical files provide, over something like url rewritting? Commented Nov 16, 2015 at 15:35
  • because all my html is structured in a database, the render page outputs the html and it needs the id of the menu to get started Commented Nov 16, 2015 at 15:39
  • yeah, sure. But thats exactly what url rewriting is for. You could add a rules to direct /my-awsome-page/ to admin/renderpage.php?id=2 so you have the same functionality without all the duplicated files Commented Nov 16, 2015 at 15:46
  • yeah, sure I could do that, but I would like my urls not to be only, renderpage.php?id=... , but I want them to be somewhat like the name of the menu... Commented Nov 16, 2015 at 15:48
  • Again, that is exactly what url rewritting does. Its not a redirect, the user seeing the url mycoolwebsite.com/my-awsome-page/ Internally this is routed to mycoolwebsite.com/admin/renderpage.php?id=2 Commented Nov 16, 2015 at 15:49

1 Answer 1

2

Try:

$filename=mysqli_fetch_array($query))['pagename'];
$fw=fopen("../".$filename,'w',true);
fwrite( $fw, "<?php 
            \$id = $id;
            include('admin/renderpage.php');
        ?>");
fclose($fw);

The PHP open tag is: <?php not <php. PHP tags

Sign up to request clarification or add additional context in comments.

2 Comments

Omg, I'm so dumb! Thank you!, by the way you forgot $fw in fwrite($fw, ...).
Sorry, i have only seen the tag. Edit! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.