Results 1 to 7 of 7

Thread: Insert data to a variable permanently via forms (PHP)

  1. #1
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)

    Insert data in to a variable permanently via forms (PHP)

    This is probably a simple thing to do, but my mind has gone blank.

    Basically I have a php include file called config.inc.php. This script simply holds a list of key variables used thoughout the website. For example it has variables called $copyright and $siteTitle which hold data corresponing to the copyright information and the information between the <title> tags. At the momemt im going into the script and changing the values of the variables manually. However i need to be able to edit their content via forms.

    So for example I might have an input box which displays the current copyright information but when I change the information and submit the form the $copyright variable is updated and the new information is displayed in the input box. If I were do go into the config.inc.php scrip and look at the variable $copyright it would hold the updated information. How might this be done? Ive seen it done on a lot of CMS, mambo/Joomla in particular have huge config files, but I cant work out how they get the data into the variables.
    Last edited by Dorza; 31-03-2006 at 10:14 PM.

  2. #2
    Senior Member
    Join Date
    Aug 2005
    Location
    Leeds
    Posts
    267
    Thanks
    8
    Thanked
    16 times in 16 posts
    Not an expert by any means, but I think that the files are generated and saved to disk - so you'd have to set up some kind of file saving functions.

    The other option would be to hold the variables in a database and insert them on the fly. WordPress and others do this, the only data file you edit offline is one that gives the database details.

  3. #3
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    Generally editing files accessable by the webserver carry's considerable risk, and is somewhat of a pain in terms of portability, even if that's not much of a problem for you having to pharse variables on a file is a pain in the bollocks quite frankly.

    As bobbyzero mentioned, the best course of action would be to just store MySQL database info in a config.php file and read/write variables to and fro MySQL, its faster, cleaner, safer, easier.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  4. #4
    Bigger than Jesus Norky's Avatar
    Join Date
    Feb 2005
    Posts
    1,579
    Thanks
    1
    Thanked
    8 times in 8 posts
    Use the fwrite function http://uk2.php.net/manual/en/function.fwrite.php

    You'd probably need a loop something like

    PHP Code:
    foreach($_POST as $field) {
    $key key($field);
    $content '$config[\''.$key.'\'] = "'.$field.'";\n';
    fwrite($handle$content);

    Not a clue if that will work though, but the basic jist is there

  5. #5
    Flak Monkey! Dorza's Avatar
    Join Date
    Jul 2003
    Location
    UK - South Wales
    Posts
    1,762
    Thanks
    34
    Thanked
    17 times in 15 posts
    • Dorza's system
      • Motherboard:
      • Asus P5B Deluxe - WiFi
      • CPU:
      • Q6600 @ 3.06Ghz
      • Memory:
      • 2GB Crucial
      • Storage:
      • 500GB Samsung SpinPoint
      • Graphics card(s):
      • Geforce 9600GT
      • PSU:
      • Cosair HX520W
      • Case:
      • LianLi something something or other
      • Monitor(s):
      • Eizo FlexScan S1910 (1280*1024)
      • Internet:
      • 2mb Virgin (when they want to give me that: else 1mb)
    Thanks for your replies guys, much appreciated. I tried the frwrite example on php.net and it works fine but it always adds the new content after the ?> tag which is no good. I think I might just stick all this info into the database like bobby suggested, I’m not familiar enough with the fwrite function to use it properly at this moment in time. I'll keep it in mind for a later date though.

    The only reason I wanted to stick these variables into a config file was because I wanted to limit unnecessary strain/load on the database/server, because of all the other queries I have going on.

    One final thing, in my first post I queried about the methods in which page impressions are registered to a database, however after thinking it through I came up with a solution and then edited out the question, but now I’m queering it again.

    Basically I have the below code which registers a page impression. What I'd like to know is; is this the best way to do such a thing and is it considered best practice? I haven't been able to find much on this around the net so I have no references to go by. All the tutorial sites I’ve found so far are only concerted with how to make click counters.

    Here’s the code:


    PHP Code:
    <?php
    if (isset($_SERVER['PHP_SELF'])) {
        
        
    mysql_query("UPDATE page_counter SET impression = impression + 1 WHERE count_id = 1");
    //Note: WHERE clause will eventually point to the page name
            
        
    }
        

    ?>

  6. #6
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable
    You can prioritise SELECT (and other) queries:

    Quote Originally Posted by dev.mysql.com
    SELECT
    [ALL | DISTINCT | DISTINCTROW ]
    [HIGH_PRIORITY]
    [STRAIGHT_JOIN]
    [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
    [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr, ...
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
    [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
    [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
    | INTO DUMPFILE 'file_name']
    [FOR UPDATE | LOCK IN SHARE MODE]]
    so if you use something like this:
    Code:
    SELECT HIGH_PRIORITY * from foo.bar;
    The query will be given higher priority than 'normal' queries qued by MySQL..
    In all likelyhood the end result will be significantly faster than flatfile pharsing.

    It's actually pretty easy to optimise MySQL queries given a little time and head scratching

    Your code snippit looks fine btw.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  7. #7
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Quote Originally Posted by Dorza
    I tried the frwrite example on php.net and it works fine but it always adds the new content after the ?> tag which is no good.
    You quite obviously need to specify the start point for file writing. You would need to do somthing like a line by line read of the file to ascertain where to put the data. All of the file functions are on the site, they are all well documented.
    To err is human. To really foul things up ... you need a computer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •