-3

I understand that the title is a bit confusing but I couldn't find a better way to title this question!

I will try my best to explain it here:

I am creating a page with an extension of .php using another php file. works fine. This php file has only some HTML codes in it. so it is a .PHP file with .HTML codes. this works fine too.

Now, I am trying to put some PHP code at the top of this created PHP file so I can connect to mysql database.

But when I place the PHP code for connecting to the database at the top of the page, and when the page is created, it will only create a blank page! and when I view page source in the browser, there is not a single HTML code in the page!

I hope I haven't confused you guys so far...

this code creates a blank page:

$i=1;
while($file = fopen("untitled$i.php", "r")) { fclose($file); $i++; }
if($file = fopen("untitled$i.php", "w")) {
$html ='<?php 
include "config/connect.php"; 
$dynamicList = "";
$sql = "SELECT DISTINCT filename FROM pages";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $category = $row["category"];
             $dynamicList .= "<li><a href="#">' . $filename . '</a></li>";
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

    <head profile="http://gmpg.org/xfn/11">

**rest of the HTML code goes here.....................**

this code creates the page as it should and it works fine:

$i=1;
while($file = fopen("untitled$i.php", "r")) { fclose($file); $i++; }
if($file = fopen("untitled$i.php", "w")) {
$html ='
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

    <head profile="http://gmpg.org/xfn/11">

**rest of the HTML code goes here.....................**

what am I doing wrong?

user3343724
  • 1
  • 1
  • 8
  • 3
    Stop right there. Why are you dynamically generating new PHP files to begin with?! GIANT RED FLAG!! There's likely a much more elegant and sane solution to whatever problem you're trying to solve by creating new PHP files. – deceze Mar 10 '14 at 14:22
  • as @deceze has stated your whole going about this wrong... '; ?> You shouldn't pass at the end of all your php. and you can use it like I hope this helps you better understand php... Keep plugging away at it and you will eventually get it. – Martin E. Mar 10 '14 at 14:24
  • @deceze, I need to create the PHP files dynamically as the Navigation menu and the content on each page is being pulled from the mysql database. so I need it to be php otherwise it would be a simple HTML file. – user3343724 Mar 10 '14 at 14:25
  • @MartinE. I've explained the reason why I need to create php pages. – user3343724 Mar 10 '14 at 14:26
  • @user3343724 You are going to have to use `eval`, most probably, but overall, you'd better reconsider your strategy, 'cause it's a huge security issue you have here. – bredikhin Mar 10 '14 at 14:29
  • Alright, take out the $html = ' and leave the – Martin E. Mar 10 '14 at 14:30
  • 1
    I still see no reason whatsoever to dynamically generate PHP files; navigation, database or whatever. You simply don't do something like this. There are other solutions. Describe what you're actually trying to do in a new question. – deceze Mar 10 '14 at 14:30
  • @deceze it has nothing to do with the OP need to do it but the lack of understanding of PHP. It is clear the understanding of php just not there. – Martin E. Mar 10 '14 at 14:32
  • I will fix your php for you... But please don't just take it for what it is... Try to learn from it abit. – Martin E. Mar 10 '14 at 14:33

3 Answers3

1

You cant pass php tags and variables into a string. The reason you do not see anything is most likely because the browser does not understand anything you passed before

What are you trying to do is a bad practice overall. Are you sure there is not any other way to do this than creating n of php files?

-- Alright as I see your comment Ill try to explain how PHP works

Lets say you have www.website.com and here you have basic main page and a menu

Menu contains following : Cars, Mobiles, Tablets

Using your desing you would just create www.website.com/cars.php, mobiles.php, tablets.php - That's wrong, youre trashing everything that this programming language has to offer.

Instead of this you should use URL variables >

www.website.com/index.php?page=cars www.website.com/index.php?page=mobiles ...

Now in your PHP code you only check if the PAGE is set and then get its value

 if(isset($_GET['page'])){

      if($_GET['page'] == 'cars'){

          // YOUR CARS PAGE
          mysql_query("SELECT * FROM CARS WHERE COLOR = 'Blue'");

      }
      else if($_GET['page'] == 'mobiles'){


          // YOUR MOBILE PAGE
          mysql_query("SELECT * FROM MOBILES WHERE OS = 'Android'");

      }

 }
 else{

     // YOUR MAIN PAGE

 }

This is how you handle PHP.

I stronly suggest you read http://www.w3schools.com/PhP/ - before you go any further.

Foxhoundn
  • 1,766
  • 2
  • 13
  • 19
  • I need to create the PHP files dynamically as the Navigation menu and the content on each page is being pulled from the mysql database. so I need it to be PHP otherwise it would be a simple HTML file. – user3343724 Mar 10 '14 at 14:27
  • Believe it or not that is how I always code my PHP files. and I use htaccess file to rewrite to make it look like the normal page in the BROWSER. but I don't want to use this method anymore. its dirty, ugly and old fashion. this method is not far off `www.website.com/index.php?id=12` etc which is not what i am looking for... – user3343724 Mar 10 '14 at 15:04
  • 1
    @user You are saying that creating new files is preferable to URL query parameters and mod_rewrite URL rewriting!? You are saying the latter is clunky and the former is more elegant?! If so you have an extremely weird idea of what's "dirty". – deceze Mar 10 '14 at 15:08
  • @deceze, yes, in this case yes.... reason? simple.. its simply because of SEO purposes and indexing as many pages as possible in search engines. – user3343724 Mar 10 '14 at 15:10
  • 1
    @user WTF does SEO have to do with this?! Search engines couldn't care less however many files you have on your server! – deceze Mar 10 '14 at 15:11
  • @deceze, OMG.... ofcourse they care mate.... Why do you think you have robot and sitemap files on your server?! – user3343724 Mar 10 '14 at 15:15
  • 1
    @user You are missing one fundamental fact: URLs are not files! You have a web server, which takes HTTP requests for URLs and responds with data. That's all browsers and search engines care about. URLs are not files. There don't need to be physical files on disk for the web server to respond to an HTTP request. Files are a purely organisational tool for you, the programmer. Sitemaps and robot files refer to URLs, not files! ... I hope it's clicking now. – deceze Mar 10 '14 at 15:21
  • @deceze, we could be sitting here and try to convince eachother that our approach is the right one which I am not prepared to do so.. if you are sending me a link then I will send you one back : http://www.mattcutts.com/blog/give-each-store-a-url/ I hope it clicks for you now... so many replies and still not a single solution to what I want in this question.:( – user3343724 Mar 10 '14 at 15:40
  • 1
    @user3343724, search engines have no way to even *know* how many files make up your website. You could have 10K URLs and only a single file, or 10K files and only a single URL. – ChrisGPT was on strike Mar 10 '14 at 15:42
  • @user I don't even need to click on that link. The *URL* of that site clearly says "give each store a ***URL***". URL! Not *file*! If you want to slowly and painfully saw off your own foot, I won't stop you. I understand what you're trying to do and why and I'm telling you it's the wrong approach for the wrong reasons. Take this advice or leave it. – deceze Mar 10 '14 at 15:46
0
    $i=1;
    while($file = fopen("untitled$i.php", "r")) { fclose($file); $i++; }
        if($file = fopen("untitled$i.php", "w")) {
            include "config/connect.php"; 
            $dynamicList = "";
            $sql = "SELECT DISTINCT filename FROM pages";
            $query = mysqli_query($db_conx, $sql);
            $productCount = mysqli_num_rows($query); // count the output amount
            if ($productCount > 0) {
                while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
                         $category = $row["category"];
                         $dynamicList .= "<li><a href="#">' . $filename . '</a></li>";
                }
            }
        }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

        <head profile="http://gmpg.org/xfn/11">
    <?php echo $dynamicList; ?>
Martin E.
  • 267
  • 1
  • 12
  • There is several videos on youtube on PHP and dynamic websites. Also Foxhoundn suggested http://www.w3schools.com/PhP/ is a good starting point too. Amazon as well has some good books on it too.... Don't lose heart though. – Martin E. Mar 10 '14 at 14:38
  • And this code does what exactly? Certainly nothing useful, and it has syntax errors as well. – deceze Mar 10 '14 at 14:53
  • this code would be closer to what he is searching for... I intentionally left smaller errors in there for learning purposes. and the syntax error(s) is very minor... – Martin E. Mar 10 '14 at 14:57
  • 1
    I'm keep going back and forth to understand how your code would create the PHP file as you placed the PHP code above the HTML stuff and closed it with a `?>`. – user3343724 Mar 10 '14 at 14:58
  • ah and I forgot to mention the syntax errors mate... I know the errors have something to do with the single or double quotes but still cannot find them! – user3343724 Mar 10 '14 at 15:00
  • $dynamicList .= "
  • ' . $filename . '
  • "; should be $dynamicList .= "
  • " . $filename . "
  • "; – Martin E. Mar 10 '14 at 15:07