Jump to content
Larry Ullman's Book Forums

Emilie

Members
  • Posts

    22
  • Joined

  • Last visited

  • Days Won

    9

Posts posted by Emilie

  1. I thought that at first so loaded it directly into Firefox, i.e. 'File/Open File'.

    Hello, Nick,

     

    This is exactly what you should NOT do in order to run a PHP file. Firefox is just a browser. It knows how to open and display the contents of HTML files, but on its own it doesn’t know what to do with a PHP file. For Firefox, a PHP file is just a text file, or a HTML file if your PHP is inside a HTML file, which is why it displays everything in this file, including your PHP code.

     

    For your PHP code to be run as such, the PHP file must be sent to your local server (XAMPP), where the PHP interpreter will parse the code and send it back to Firefox (as HTML), for Firefox to display it.

     

    Which is why Larry told you :

     

    That would suggest that Netbeans isn't executing your PHP, probably because it's not being run through a URL.

     

    This means you cannot just use File/Open in Firefox. If you do that, the address will begin with : file:/// This is the path on your computer. It tells Firefox where the file is, it doesn’t tell it that this file should be sent to XAMPP and the PHP interpreter for processing. For this to happen, the address in the browser must begin with http://. If you kept your htdocs folder inside the xampp folder (the default place for it), the address in the browser will probably begin with: http://localhost/xampp/. For instance, the "Welcome" file to XAMPP runs at this address : http://localhost/xampp/index.php. The index.php file is just inside your htdocs folder. Try to open this file using File/Open in Firefox (the path is probably: file:///C:/xampp/htdocs/index.php, if your xampp folder is right at the first level inside C:)… and see what happens ! Don’t worry. Your XAMPP installation is probably just fine. The problem is only that you opened the PHP file instead of running it through the PHP interpreter.

     

    Try and read the beginning of chapter 1 again. Larry explains it very clearly… although I know it took me a long time before really understanding how things work between the local server, the PHP interpreter and the browser.

     

    I hope this helps,

     

    Emilie

    • Upvote 1
  2.  

    I generally define a $prod variable and set it to true in production and false in my dev environment.

    Thanks for the tip. I’ll try this in my own projects!

     

     

    I much prefer absolute paths for includes over relative paths.

    In theory, I agree. But I don’t know why, absolute paths usually don’t work for me for URIs. Absolute URLs work fine, absolute URIs don’t, and I really can’t understand why.

  3. Hello, hwadler,

     

    Paths are one of the real pains of web programming! Your best bet is to create constants for the different paths you will need for your projects. Let’s say my files are organised like this:

    root_folder
        index.php
        includes // I actually use another, less obvious name
            config.inc.php
            header.php
            navigation.php
            footer.php
        css
            styles.css
        images
        js
        pages_folder
            some_page.php
    
            sub_folder
                some_other_page.php
    

     I define the following paths :

    if ($_SERVER['HTTP_HOST'] == 'localhost:8888') {
        define('LIVE', FALSE);
        define('BDD', '/Users/my_account/htdocs/bdd_connect.php');
    
        define('BASE', 'http://localhost:8888/root_folder/');
        define('BASE_PAGES', BASE . 'pages_folder/');
        define('BASE_SUB', BASE_PAGES . 'sub_folder/');
    } else {
        define('LIVE', TRUE);
        define('BDD', '/home/my_account/bdd_connect.php');
    
        define('BASE', 'http://www.domain.com/root_folder/'); // this folder
            // is probably called www or public_html
        define('BASE_PAGES', BASE . 'pages_folder/');
        define('BASE_SUB', BASE_PAGES . 'sub_folder/');
    }
    
    

    Then I create constants for the different paths that vary according to the folder I’m in when calling them:

    if (dirname($_SERVER['PHP_SELF']) == '/root_folder') {
        define('BASE_CSS', 'css/');
        define('BASE_JS', 'js/');
        define('BASE_IMAGES', 'images/');
        define('BASE_INCLUDES', 'includes/');
    } elseif (dirname($_SERVER['PHP_SELF']) == '/root_folder/pages_folder') {
        define('INDEX', '../index.php');
        define('BASE_CSS', '../css/');
        define('BASE_JS', '../js/');
        define('BASE_IMAGES', '../images/');
        define('BASE_INCLUDES', '../includes/');
    } elseif (dirname($_SERVER['PHP_SELF']) == '/root_folder/pages_folder/sub_folder') {
        define('INDEX', '../../index.php');
        define('BASE_CSS', '../../css/');
        define('BASE_JS', '../../js/');
        define('BASE_IMAGES', '../../images/');
        define('BASE_INCLUDES', '../../includes/');
    } else {
        define('BASE_CSS', NULL);
        define('BASE_JS', NULL);
        define('BASE_IMAGES', NULL);
        define('BASE_INCLUDES', NULL);
    }
    

    In the header.php file, I call the CSS files like this :

        <link rel="stylesheet" href="<?php print BASE_CSS . 'styles.css'; ?>">
    

    This will work whether the header.php file is in index.php or in some_page.php or in some_other_page.php.

     

    In the different files inside the pages_folder, I call the configuration file like this:

    require_once('../includes/config.inc.php');
    

    I call the header, navigation and footer files like this :

    include(BASE_INCLUDES.'header.php');
    include(BASE_INCLUDES.'navigation.php');
    include(BASE_INCLUDES.'footer.php’);

    In the different files inside the sub_folder, the call for the configuration file is different since the constants are not defined until the configuration file has been included:

    require_once('../../includes/config.inc.php');
    

    On the other hand, the header, navigation and footer files are called everywhere the same way since the constants can be used:

    include(BASE_INCLUDES.'header.php');

    I’m sure programmers better than I am manage things more efficiently, but this has solved all my problems with paths !

     

    I hope this helps,

     

    Emilie

    • Upvote 2
  4. Hello,

     

    There's a typo in the line you highlighted in red: you forgot the concatenation dots around $image[3]:

     

     

    echo "<div align=\"center\"><img src=\"images?image=$pid&name=" . urlencode($row['image_name']) . "\" . $image[3] . alt=\"{$row['print_name']}\" /></div>\n";
     
    I hope this helps,
     
    Emilie
  5. I see what you mean. I hadn't thought of that, because I'm not concerned with the book being viewed from the web, at least for the time being. I only needed some means of opening those huge HTML files and editing them. I'm just an amateur programmer, so I first tried to open this 70 MB file with my usual text editor (BBEdit), and although I've been able to edit a 350 MB file with BBEdit (although very, very slowly), I couldn't open this 70 MB file with BBEdit.

     

    I was going through Larry Ullman's "Advanced PHP" book at the time, and through chapter 12 more specifically, so I just thought I would have a go with PHP CLI. It worked (with help from Antonio Conte!), and it is so much quicker than using a text editor that I now use it to edit huge files.

     

    But I'll keep in mind your idea of storing the text in a DB and generating the HTML on the fly if I ever need these books to be viewed from the web. 

  6. Hello,

     

    What is "wrong", according to you?

     

    The registration date is the same for all users because you entered all of them at one go into the database, and therefore the timestamp corresponding to NOW() is the same for everyone. Because of that, ordering the results by registration_date DESC has no real meaning.

     

    I hope this helps,

     

    Emilie

    • Upvote 3
  7. Hello,

     

    I would very much appreciate it if you could break the script down for me & explain it for me. The parts like this are really confusing me - I don't understand what they do for the rest of the script: 

    <option value="30" <?php if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30')) echo ' selected="selected"'; ?>>Very Good</option>
    	
    	
    ?php if (isset($_POST['gallon_price']) && ($_POST['gallon_price'] == '3.50')) echo 'checked="checked" '; ?>/> 3.50
    

     

    These lines make the dropdown menu and the radio buttons sticky. For $_POST['efficiency'] to be set, the form must have been sent and the user must have chosen one option in the dropdown menu. So, if the form has already been sent but the user forgot something (either 'distance' or 'gallon_price') and the partially completed form is printed again...

    if (isset($_POST['efficiency']) && ($_POST['efficiency'] == '30'))

    ... first checks that the user chose one option in the 'efficiency' dropdown menu (isset($_POST['efficiency']), and if the chosen value is '30' the PHP script will add the 'selected="selected"' attribute inside this option element. On screen, the 'Very Good' option of the dropdown menu will be preselected, so that the user doesn't have to select it again. And if you choose "View source" in your browser, you will see that the previously selected option shows the 'selected="selected"' attribute inside the option element:

    <option value="30" selected="selected">Very Good</option>

    If the user chose '20' in the dropdown menu, this option element will get the 'selected="selected"' attribute instead, and so on.

     

    If the user forgot to choose one of the options in the dropdown menu, the default option (10/Terrible) will get the 'selected="selected"' attribute.

     

    The radio buttons work in the same way. If the user didn't completely fill in the form, the radio button that had been chosen first time round will get the  'checked="checked"' attribute. On the other hand, if the user forgot to choose one of the radio buttons, none of them will get the 'checked="checked"' attribute.

     

    If you want to better understand how the $_POST array works, you can print its contents just after the closing </form> tag:

    </form>
    <?php
    echo '<p>Contents of POST <em>before</em> the form was submitted:</p>
    <pre>' . print_r ($_POST, 1) . '</pre>';
    ?>

    and again just after the line checking for form submission: 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    echo '<p>Contents of POST <em>after</em> the form was submitted:</p>
    <pre>' . print_r ($_POST, 1) . '</pre>';

    And test your script with data in 1, 2 or 3 form fields to see the consequences on the $_POST array.

     

     

     

     

    I hope this helps,

     

    Emilie

    • Upvote 2
  8. I only need that much memory to start with because these HTML files have been compacted and made into 1 line only.

     

    Once I've introduced line breaks, I can use file() to create an array of lines, and then use a foreach loop.

     

    This 70 Mo HTML file is also buggy, which is probably why BBEdit can do nothing with it, since it previously managed to introduce line breaks into a 350 Mo HTML file. But it took something like 20 minutes, whereas it only takes 1 or 2 minutes from the command-line!

  9. Thank you for your answer, Larry.

     

    The PHP script is only 4 KB, but the HTML file is indeed 70 MB (it's a HTML file with lots of unnecessary styling inside). And I can't go through it a line at a time because it's one huge line! That's part of what I need to change. My text editor (BBEdit) managed to add new line breaks in a 300 MB file, but this 70 MB HTML file is buggy, so BBEdit cannot do anything with it. That's why I'm trying to find other ways of editing it. I can open it and read it without any problems in the command-line,  I just need more memory to be able to add new line breaks and then delete all this unnecessary styling.

     

    Do you know where can i find the php.ini file for PHP CLI? Or where I could look for more information on PHP CLI more generally? I'll temporarily use ini_set as recommended by Antonio Conte, but as I have quite a few huge HTML files to edit, modifying the php.ini file could be useful.

     

    With thanks for your help,

     

    Emilie

  10. Hello,

     

    I'm new on this forum. I'm an amateur and intermediate web developer. English is not my mother tongue, so I apologize for all the mistakes I'll make in my messages!

     

    I've been reading chapter 12, 'PHP's Command-Line Interface' and have had no problem reproducing the different scripts. But now I'm trying to apply what I've learnt to other PHP scripts, and I've run into a fatal error:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 70517676 bytes) in some_file.php on line 7
    

    Line 7 is the following:

    $clean_file = str_replace('some_string', '', $file);
    

    I first tested my script on a small html file, so I know the script in itself works.

    The html file I'm now using is quite big (70 Mo)… and there's worse to come (300 Mo)!

     

    Should i modify the configuration file called php-config in /usr/bin in order to avoid this fatal error? If so, how can I change it? I tried to open it in my text editor, but it appears as a Unix executable file.

     

    Or is this one of the cases where I should change the php.ini behavior with the -d option? If so, what change should I make?

     

    With thanks for your help,

     

    Emilie

×
×
  • Create New...