+ Reply to Thread
Results 1 to 5 of 5

Thread: PHP variable variables array problem

  1. #1
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    PHP variable variables array problem

    Hello,

    I have a problem with my flat file database...

    they are built like this:

    Code:
    title:BLALBL
    brief:Cool thing
    thing:asdf
    image[0]:http://....
    image[1]:http://....
    With the code I want the strings before the : shall be variable names and the string after shall be the value. THen I can use it like this:

    PHP Code:

    <?php

    echo $title;
    echo 
    "<p>$brief</p>";
    echo
    "<img src='$image[0]' />";
    echo
    "<img src='$image[1]' />";
    ?>
    Do you understand?? Here is my code that is nearly working::

    PHP Code:
    <?php
    $file 
    file("../../txt/datafile.txt");


    foreach (
    $file as $line_num => $line){

    ${
    "t".$line_num}=explode(":",$file[$line_num]);
    // creating arrays ($t0,$t1,$t2 ...)

    $${"t".$line_num}[0] = ${"t".$line_num}[1];

    ?>
    The only problem is that the $image array (or any other array) will not work!! I can figure out what I do wrong. Thanks for any help
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  2. #2
    gomarc's Avatar
    gomarc is offline x10 Elder gomarc is an unknown quantity at this point
    Join Date
    Oct 2007
    Location
    USA
    Posts
    511

    Re: PHP variable variables array problem

    Hi gaptrast,

    Try limiting “explode” to 2 substrings.

    Code:
    ... = explode(":",$file[$line_num], 2);
    In your code, the colon (: ) after 'http' is also a delimiter, so you are getting at least 3 strings instead of two back.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP variable variables array problem

    Rather than using a custom format, why not use something PHP already supports, such as ini,csv or a native PHP format, such as an array (whether stored as a PHP script or serialized)?
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  4. #4
    gaptrast's Avatar
    gaptrast is offline x10 Sophmore gaptrast is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    117

    Re: PHP variable variables array problem

    ok I have now changed to ini. but I have a problem:
    Note: There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, no and false results in "", yes and true results in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value
    Do I have to change all of theese to special html characters?? like on = &#111&#110
    LOOK RIGHT
    LOOK DOWN
    Questions you never knew you wanted answered - http://Wonderabout.info
    [New site!] Collection of fun computer pranks! - http://thefakevirus.com

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: PHP variable variables array problem

    Encoding them (whatever the scheme) is one option. However, an ampersand ('&') is one of the special characters, so you won't be able to use HTML entities.

    Under PHP 5.2, you can enclose the value in double quotes (which you would need to do anyway if the string had any non-alphanumeric characters). Under 5.3, you'd use single quotes, or escape the characters with a backslash, or set the scanner mode to INI_SCANNER_RAW or (in some cases) do nothing: a dollar sign ('$'), for example, is only special when followed by a squiggly bracket ('{').

    Another possibility is to use another format instead of PHP's configuration format, one where you won't have to escape or encode any characters, such as a PHP array stored in a script. If you don't care about having your data store be editable by text editors, you could use a Berkeley DB style database.

    You left off closing semicolons in your sample HTML entities.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Problem inserting serialized array
    By learning_brain in forum Programming Help
    Replies: 7
    Last Post: 11-01-2010, 01:39 AM
  2. Replies: 4
    Last Post: 04-23-2010, 02:38 AM
  3. PHPMailer / MySQL Array Problem
    By stevet70 in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 06-02-2009, 11:37 AM
  4. PHP Global Variables Problem
    By jmg498 in forum Programming Help
    Replies: 2
    Last Post: 02-10-2009, 11:45 PM
  5. Recursive PHP array to Javascript array/object
    By Veridis in forum Programming Help
    Replies: 0
    Last Post: 04-16-2008, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers