www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: executing php variables in cvs file

  1. #1
    Join Date
    Jul 2006
    Posts
    373

    executing php variables in cvs file

    Hi,

    im loading the contents of a cvs file with the following code:

    PHP Code:
    $fields fgetcsv($handle1000",");
     
     
     while(
    $data fgetcsv($handle1000",")) {
         
    $detail[] = $data;
     }

     
    $x 0;
     
    $y 0;
        
     foreach(
    $detail as $i) {
         foreach(
    $fields as $z) {
             
    $stock[$x][$z] = $i[$y];
             
    $y++;
         }
         
    $y 0;
         
    $x++;
     } 

    Now i can call whatever row of the file by using this variable: $stock[$v][word] where $v is the number of the row and the second value is the name of the column. That works pretty well. Now, what i want to do is to include variables inside the cvs and have php execute them and print them. However, it wont work because the variables are not parsed as php but as simple text. Is there any way to do this? i tried by encapsulating the variables in <php> and {php} tags but it didnt work either. Please help.

    Thank you.

  2. #2
    Join Date
    Feb 2012
    Location
    Tallahassee, FL
    Posts
    280
    You cant. It is opening the file, not parsing it. If yu do a fopen on a phgp file it will not parse it either. I would make something like bb code [var_1] and preg_replace. Like:
    PHP Code:
    <?
    $var_1 
    $_POST['email'];//is some@email.com
    $var_2 $_POST['pass'];//is somepass
    $cell $stock[$v][word];
    //let say that $cell = 'Your email is [var_1] and password is [var_2].';
    $cell preg_replace('/\[var_1\]/'$var_1$cell);
    $cell preg_replace('/\[var_2\]/'$var_2$cell);
    echo 
    $cell;
    //Returns: Your email is some@email.com and password is somepass.
    ?>

  3. #3
    Join Date
    Jul 2006
    Posts
    373
    So in case of a cvs file what could i use instead of fgetcsv? i need an alternative that also allows me to do the function the code at the bottom does.

    Thanks.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    You could include() the file with output buffering on, then dump the buffer into a temporary file that could be used with fgetcsv().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jul 2006
    Posts
    373
    So i first convert the file into php, make the include and then back to csv? how is all that possible?

    Thanks.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    If I'm understanding things correctly, you don't have to really "convert" the CSV file, just insert <?php...?> sections where you need it in that file. Then include and process it:
    PHP Code:
    ob_start();
    include 
    'path/to/file.csv';
    $csvText ob_get_clean();
    $fh fopen('php://temp''r+');
    fputs($fh$csvText)
    rewind($fh);
    while((
    $row fgetcsv($fh)) !== false) {
       
    // do stuff with array $row

    However, from a security standpoint, I'm not crazy about this unless you have complete control of that CSV file and are sure no malicious code could end up in there.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jul 2006
    Posts
    373
    Thanks. That works pretty well. Just one problem. I need to have variables in the cvs that are actually being defined AFTER i call the cvs into the code. Like this:

    PHP Code:
    function buildcat() {
     
    ob_start();
     include 
    'web/yogo.csv';
     
    $csvText ob_get_clean();
     
    $handle fopen('php://temp''r+');
     
    fputs($handle$csvText);
     
    rewind($handle);

     
    $fields fgetcsv($handle1000",");
     
     
     while(
    $data fgetcsv($handle1000",")) {
         
    $detail[] = $data;
     }

     
    $x 0;
     
    $y 0;
        
     foreach(
    $detail as $i) {
         foreach(
    $fields as $z) {
             
    $stock[$x][$z] = $i[$y];
             
    $y++;
         }
         
    $y 0;
         
    $x++;
     }
     
     
     for(
    $v=1$v $num$v++) {
     
    $linkedword[$v]="<a href=\"s=glossary&amp;w={$v}\">{$stock[$v][word]}</a>"; <-- here i define the variable
     

    the variable i need to have within the cvs file is $linkedword, but as you may see, this is defined until many lines below because it cant be defined before doing the while and defining $stock. What should i do here? load the cvs file once, do the while and then load it back maybe? im kinda confused.

    Thanks.
    Last edited by supercain; 04-08-2012 at 10:33 PM.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,227
    Either change the processing logic so the variables get defined first, or else all I can see left to do would be to use eval().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    Jul 2006
    Posts
    373
    Hmmm but i cant define the variables without loading the csv file first. Thats my dilemma. I tried by moving the while part to the top of the csv file and convert it into a php, then having all the comma separated values defined as a variable and use explode instead of fgetcsv. But it didnt work because i have basically to load the php/csv file into itself which results into an endless loop. Also, i really dont understand the use of eval. The page even says that its use is highly discouraged.

    Okay, this is my idea:

    1. Load the csv normally.

    2. Do while and define $stock

    3. Define $linkedword

    4. Write the resulting into a temporary cvs with all the $linkedword variables already parsed.

    5. Load the temporary cvs file and have the rest of the code execute as normal (from function buildcat())

    What do you think? i know it may be not the best way to do it but i cant think of anything else.
    Last edited by supercain; 04-09-2012 at 09:19 PM.

  10. #10
    Join Date
    Apr 2010
    Posts
    227
    I would like to step in and help but I'm not sure what you are trying to do.

    In these sort of situtations the fault generally lies with the logic set of the php author. (no offense)

    What are you exactly trying to do?

    csv is one way of storing data permantly.

    variables are for storing temporary data.

    Are you trying to store temp data inside of a CSV file?

    Or are you trying to put placeholders within the csv file that is then updated/changed by php?
    Say a CSV template.

    ?

  11. #11
    Join Date
    Jul 2006
    Posts
    373
    Hi,

    its a dictionary. I store the values in a csv file instead of a database because its more practical that way, or at least thats what i think. The code above produces something like this:

    Comestibles - Fruits - Orange: a fruit from an orange tree.
    Comestibles - Fruits - Apple: a fruit from an apple tree
    Comestibles - Meat - Chicken: meat from chicken.

    the code is supposed to sort the words by category and subcategory. It works pretty well. My problem was that in the definition i want to include links to other words in the same dictionary if necessary. Thats why i defined $linkedword that will automatically produce the word and the link to that word in particular.

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by supercain View Post
    Hi,

    its a dictionary. I store the values in a csv file instead of a database because its more practical that way, or at least thats what i think. The code above produces something like this:

    Comestibles - Fruits - Orange: a fruit from an orange tree.
    Comestibles - Fruits - Apple: a fruit from an apple tree
    Comestibles - Meat - Chicken: meat from chicken.

    the code is supposed to sort the words by category and subcategory. It works pretty well. My problem was that in the definition i want to include links to other words in the same dictionary if necessary. Thats why i defined $linkedword that will automatically produce the word and the link to that word in particular.
    This would be very trivial if you were using a properly designed database structure...a csv file is not the best solution for relational data, a relational database is.

  13. #13
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by criterion9 View Post
    This would be very trivial if you were using a properly designed database structure...a csv file is not the best solution for relational data, a relational database is.
    But, if you are running your script on Windows then it's a very easy thing to use CSV files as if they were a database and access them with SQL.
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

  14. #14
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Charles View Post
    But, if you are running your script on Windows then it's a very easy thing to use CSV files as if they were a database and access them with SQL.
    Easy from a scripting perspective maybe...but not very computationally efficient. There are still many loops through the data for complex queries (such as what would be needed for the above) where with a properly designed and indexed database the read-write to disk (as well as raw memory usage) is greatly reduced.

  15. #15
    Join Date
    Nov 2002
    Location
    Baltimore, Maryland
    Posts
    12,270
    Quote Originally Posted by criterion9 View Post
    Easy from a scripting perspective maybe...but not very computationally efficient. There are still many loops through the data for complex queries (such as what would be needed for the above) where with a properly designed and indexed database the read-write to disk (as well as raw memory usage) is greatly reduced.
    True enough, but the premise of the question is "Since I'm not going to do this the right way how do I proceed?"
    “The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
    —Tim Berners-Lee, W3C Director and inventor of the World Wide Web

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
  •  
HTML5 Development Center



Recent Articles