Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 16
  1. #1
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    hello,

    i dont know much php and i have a problem.

    let me explain.

    there is display.php which GETs a variable ?n=pol2
    it opens a file (hijibiji,txt) and searches for 'pol2' in it.

    the file is arranged thus:
    pol1|politics1.jpg|mallick
    pol2|politics2.jpg|bose
    phy1|physics1.jpg|nag
    and so on and so forth
    the separator is the 'pipe'

    when it finds 'pol2', i want a variable to contain 'politics2.jpg' and another variable to contain 'bose'

    i can do every thing else, but this searching and putting the correct data into the variables has stumped me.

    please help.
    thanking you in advance,

    Arkantos.
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Depending on how your text file is organized, you could do something like the below code example.

    In the example, I have just constructed a variable "$var" that would look like the variable produced by reading a text file.

    If the info in your text file is organized in such a way that each new entry is on a new line (as your example indicates), then you can use the new-line symbol "\n" as a break point when constructing the array. The array "$a" holds all information from string "$var", but each new line is a seperate key in the "$a" array.

    The remaining code snippet searches for a match in the array, and produces a new array that will hold all the content of the matching line, in the array "$finalArray".
    Each block of information in the matching line "$var", will have a separate array key.

    I've added the "print_r" command to show you the resulting array.
    The loop breaks when it hits the first match, meaning it will not continue searching hundreds of sub-keys if it finds a match on first attempt. (you can remove the "break" and see what happens)
    PHP Code:
    $var "asd|asd|asd\nsfg|dfg|dfg\nsfadf|adf|adf\nadf|sfadf|adf|qef";
    $a explode("\n"$var);

    for(
    $i=0$i<count($a); $i++)
    {
        if(
    stristr($a[$i], "sfadf"))
        {
            
    $var $a[$i];
            
    $finalArray explode("|"$var);
            
    print_r($finalArray);
            break;
        }

    S. Rosland

  4. #3
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    thank you rosland.
    i shall try to use this in the code, and get back with the results.

    questions:

    do i use fopen() to open the file?

    how do i break $finalarray into 2 separate variables? i know the way but not the exact command to do it.
    something like this:

    PHP Code:
    $filename $finalarray[1]
    $surname $finalarray[2
    but i have the funny feeling that '1' & '2' will not do it.

    thank you once again,
    Arkantos.
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  5. #4
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    i changed the file as necessary.
    right now, it should display the filename and the persons name when the $search matches.
    but it aint doing it.


    PHP Code:
    <?php
    $search 
    $_GET['n'];

    $var fopen("hijibiji.txt""r");
    $a explode("\n"$var); 

    for(
    $i=0$i<count($a); $i++) 

        if(
    stristr($a[$i], $search)) 
        { 
            
    $var $a[$i]; 
            
    $finalArray explode("|"$var); 
            
    print_r($finalArray); 
            break; 
        } 
    }
    ?>
    and one more question, how do i break up the $finalarray into $filename and $surname?

    Arkantos.

    the flatfile table is attached.....
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  6. #5
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Hi Arkantos, the trick to writing any code is debugging....

    start with a small cut down example file...
    echo the variables you are putting into the if statement...
    echo the values you are seeing inside the if statement....


    use the list function to get the values form the array IE:

    list($whatever, $filename, $surname) = split("|", $var);
    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  7. #6
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    hi Hagen!

    i did as you said, but echoing the $var as in "$var = fopen("hijibiji.txt", "r");"
    is outputting the words "resource id#2"

    whats that??

    this is the code as it is now:

    PHP Code:
    <?php
    // $search = $_GET['n'];
    $search "g01";

    $mainfile fopen("view.txt""r");
    $a explode("\n"$mainfile);

    echo 
    $search;
    echo 
    $mainfile;


    for(
    $i=0$i<count($a); $i++) 

        if(
    stristr($a[$i], $search)) 
        { 
            
    $var $a[$i]; 
            
    $finalarray explode("|"$var); 
            
    print_r($finalarray); 
            break; 
        } 
    }

    // i believe $finalarray[0] is = $search
    $filename $finalarray[1];
    $name $finalarray[2]

    ?>
    should i use fopen() or is there a better alternative
    i also think there is something with the txt file format (PC / UNIX / blah). is that an issue??
    please help.

    Arkantos.
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  8. #7
    Senior Member hagen's Avatar
    Join Date
    Aug 2005
    Posts
    408
    Member #
    10882
    Liked
    1 times
    Here is a more complete script that should work... :-)
    You need to escape the pipe...
    I recomend using more meaningful names... Have a thought for any poor soul that may inherit your work! :-)

    -Hagen

    PHP Code:
    $search $_GET['n'];

    $input_filename 'hijibiji.txt';
    $fp fopen($input_filename"r");
    $var fread($fpfilesize($input_filename));

    $lines split("\n"$var); 

    for(
    $i=0$i<count($lines); $i++) 

        
    $line $lines[$i];
        echo 
    $line "<br>";
        
        if(
    stristr($line$search)) 
        { 
            list(
    $whatever$filename$surname) = split("\|"$line); 
            echo 
    "whatever:" $whatever " filename:"$filename " surname:" $surname
            break; 
        } 

    Hagen Rose: hagen(at)jxwd(dot)co(dot)uk
    JX Web Development, Bournemouth, Dorset...JXWD.co.uk

  9. #8
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Quote Originally Posted by Arkantos
    i can do every thing else, but this searching and putting the correct data into the variables has stumped me.
    I didn't include any filereading code in my first post, as I assumed you had solved that part based on the above quote from your first post.

    --------------

    Anyway, the reason you get a Resource id#2 "error", is because that's what it is. It's just a resource pointer for further processing.

    The fopen() command, is just a filepointer for one of the many filereading commands available in PHP.

    This is what you have to do:

    PHP Code:
    $filename "view.txt";
    $pointerfopen($filename"r");
    $mainfile fread($pointerfilesize($filename)); 
    Edit:
    Seems Hagen posted his answer while I was writing this.
    (you can see the fread() in his code)

    Edit 2:
    I see Hagen is using 'split' instead of 'explode'. Split invokes the overhead of the regular expression engine. If you don't require the power of regular expressions, 'explode' is faster.
    S. Rosland

  10. #9
    Member Arkantos's Avatar
    Join Date
    Jul 2005
    Posts
    66
    Member #
    10732
    it works!

    hagen you are a genious!
    rosland, you are a genious too!

    i changed the 'split' to 'explode' and the page processing time has reduced, as you said it would.

    it has been integrated to the code and is working very nicely.

    Quote Originally Posted by "hagen
    I recomend using more meaningful names... Have a thought for any poor soul that may inherit your work!
    i understand and really agree on this matter, but what about commercial work?

    i'll post the finished work tomorrow, as the server will be booked tomorrow.

    a large thanks to both of you -to rosland, for clearing my concepts, and to hagen for showing the way.

    Arkantos.
    No bastard ever won a war by dying for his country, he won it by making the other poor dumb bastard dying for his country.
    General George S. Patton (US Army)

  11. #10
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    What about commercial work? What changes if the work is commercial?


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com