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');
            ?>
Cr1xus
  • 427
  • 3
  • 20
  • Not an answer, but why are you doing this? What value do these nearly identical files provide, over something like url rewritting? – Steve Nov 16 '15 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 – Cr1xus Nov 16 '15 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 – Steve Nov 16 '15 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... – Cr1xus Nov 16 '15 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` – Steve Nov 16 '15 at 15:49
  • I thought about your suggestion too (only that I didn't know the terminology), it cuts the redundancy, but I don't think my clients care about that. – Cr1xus Nov 16 '15 at 15:50
  • Oh, didn't know I could do that, then that's great, I'll use it, thanks! – Cr1xus Nov 16 '15 at 15:51
  • No problem - `Reinventing the wheel` is a very common problem for us programmers, ive done it so many times. – Steve Nov 16 '15 at 15:54
  • Thanks for caring, Steve, your suggestion is actually really helpful since I'm a perfectionist with coding. I had a feeling that something was not right with this, but couldn't see another way. Really thank you :) – Cr1xus Nov 16 '15 at 15:58

1 Answers1

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

Tobias
  • 653
  • 4
  • 12