Results 1 to 11 of 11

Thread: [resolved] How to read a specific line from a file? using php..

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    [resolved] How to read a specific line from a file? using php..

    If i have a .HTML or .INI or .PHP file on my server, how can i make a .php site that reads a single specific line from that file..

    ex:

    i have the file INFO.INI containing:

    NAME: somename somelastname
    ADRESS: somewheres adress
    CONTRY: somecountry
    MOBILE: somemobilenumber

    and then i want a file called ex: READ.PHP

    that can read this INFO.INI and pull the info from ex MOBILE into a string called $MOBILE and then create a HTML code containing this info..
    ex:
    <?php
    /some code to get the the MOBILE info from the INFO.INI
    echo "<font color=red>" . $MOBILE . "</font>" ;
    ?>

    something like this.. can anyone help me out a little..
    hope you guys get what i mean, kinda hard to explain..
    Thanks in advance...for any help regarding this.,..

    (Cause i have a few domains like www.hfa.no and www.forbruksartikler.no where im to remake a webshops preview system into something quite simpler, but i cant really figure out how to make PHP read from a file this way, but i know that it is possible,cause i've seen it be done other places...)
    Last edited by alexdata; Jun 11th, 2005 at 10:12 PM. Reason: Adding Info..
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  2. #2
    Hyperactive Member alacritous's Avatar
    Join Date
    Aug 2003
    Posts
    464

    Re: How to read a specific line from a file? using php..

    If you know the line number, you can use this:

    Code:
    <?
    $lines = file('   ');
    $l_count = count($lines);
    for($x = 0; $x< $l_count; $x++)
    {
    }
    
    echo "<font color=red>" . $lines[0] . "</font>" ;
    ?>
    0 being line 1
    1 being line 2
    etc.

  3. #3
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: How to read a specific line from a file? using php..

    hi, im not having a go at you or anything but i asked this kinf of question a few days ago and why put "using php.." at the end of your title when it is in the PHP forum. here is my thread i asked a few days ago http://www.vbforums.com/showthread.php?t=342531. it should help. you could change the code to use math to do with the number of lines and all the other things in between it or something.
    If there is only one perfect person in the universe, does that make them imperfect?

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    www.alexdata.com
    Posts
    484

    Talking Re: How to read a specific line from a file? using php..

    Thanks, alacritous.. Your code worked exellent!!

    I modified it to my needs and now i can have it "build" the content of a website from a single textfile.. so i can edit the textfile easily, and keep the site's design intact without ruinin anything.. very handy...
    Thanks alot,..

    DanDono.. I read your thread, but that didnt really suit my needs.. so therefore i asked the question again.. in a slightly different way, and i wrote USING PHP, because i know many would tend to answer in any other languages they know.. happened to me in the VB part of this forum, that i got answers using java, or c, or php... so thats why... hehe, but thanks for your thread, gave out some other handy info...

    I uploaded two files for people who wants to try this out right away.. its a PHP and a TXT file.. upload both to your server and open the PHP one.. now it will build a HTML site from the text in the InFO.txt.. edit the txt file and your HTML site will be easily reddesigned in the same style..
    PS: rename the "AutoFiller.php.txt" to something like "AutoFill.php" or "index.php"
    Attached Files Attached Files
    Last edited by alexdata; Jun 11th, 2005 at 10:23 PM.
    ***************
    Please use [highlight=vb] ..your code.. [/highlight] when posting code!

    When you have received the working answer to your question,
    please mark it as *SOLVED* + Your Questions Title ...using your Thread's Tool menu.


    Also try to point out what answer made it work for you, or edit your first post to contain a quote of the correct answer...

    Please Answer All Questions With Working Code Examples...


    My Unfinished Projects and My working Programs
    ***************

  5. #5
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: [resolved] How to read a specific line from a file? using php..

    I found you work and love it, now I'm trying to implement it on a project I have but just don’t seem to make it work... here’s the problem I have:

    1 loader file:

    <?php $dic2010= "some url";?>
    <?PHP
    $notas = file('2010/ligas.txt');
    count($notas);
    include ("$dic2010"."$notas[0]");
    ?>

    2. ligas.txt (urls)

    58expoliciasdetenidosenjuliosaldranfloresesquerro.html
    60monrcasnoquieren comer.html

    The problem I have is that if I want to include the second url using the same method, nothing works.

    I Get a message that says:

    [function.include]: failed to open stream: Invalid argument in etc. etc.

    But if I only put one line in the text file and only reference on file in the incudes, it works...

    What I want to do is to be able to load multiple includes in different parts of my main index.php

    Can you help me? Please.

  6. #6
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: [resolved] How to read a specific line from a file? using php..

    Forgot to mention if I put 5 url's in the text file it only reads de las on the list

  7. #7
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: [resolved] How to read a specific line from a file? using php..

    Also used this and same result:

    <?PHP
    $lines = file('2010/ligas.txt');
    $l_count = count($lines);
    for($x = 0; $x< $l_count; $x++)
    {
    }
    include ("$dic2010"."$lines[2]");
    ?>
    where $lines[2] is the 3th line on the ligas.txt file... and that’s the online it reads, if I add a 4th line in the ligas file it will read the 4th line as long as it's the last in the list and called bye $lines last line.

  8. #8
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [resolved] How to read a specific line from a file? using php..

    first, that code you have there doesn't make much sense. you have a for loop that loops through the lines in the file, but you're not doing anything in the for loop. you need to change the code to the following:
    PHP Code:
    <?php
      $lines 
    file('2010/ligas.txt');
      
    $l_count count($lines);
      for(
    $i 0$i$l_count$i++)
      {
        include(
    $dic2010 $lines[$i]);
      }
    ?>
    if you see what I've changed, now instead of referencing $lines[2], I'm referencing $lines[$i], which is the current line index of the loop. if you're getting errors still about files not existing, then the file doesn't exist. PHP uses a relative path from the file being run for includes, so if your path goes to /includes/somefile.php, then the /includes/ folder needs to exist in the same directory that the PHP file being run exists in (presumably index.php).

    let me know if you have anymore questions.
    Like Archer? Check out some Sterling Archer quotes.

  9. #9
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: [resolved] How to read a specific line from a file? using php..

    Didn't work... sorry

  10. #10
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: [resolved] How to read a specific line from a file? using php..

    vb Code:
    1. <?php
    2.   $lines = file('2010/ligas.txt');
    3.   $l_count = count($lines);
    4.   for($i = 0; $i< $l_count; $i++)
    5.   {
    6.     include($dic2010 . $lines[$i]);
    7.   }
    8. ?>
    how do I separate files in my final output?? I don't get it

  11. #11
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [resolved] How to read a specific line from a file? using php..

    you said it didn't work -- now, does it work? there isn't anything wrong with the code I posted. you have to make sure the files you reference in ligas.txt exist and $dic2010 is defined somewhere.

    what do you mean, how do you separate files? as soon as the file is included, it will be executed. if the file is simply printing content out, then that content will be printed out as if it existed in the current file. if you want something else to be output every time you include a file, you would do that inside of the for loop:
    PHP Code:
    for( ... ){
      
      
    // Place a horizontal rule
      
    echo '<hr />';

      include( ... );

      
    // Print a message
      
    echo 'That was file #' . ($i 1);


    in this example, a horizontal rule will be placed before each file's contents. then, after the content, it will display a message saying which file was just printed (starting at 1).
    Like Archer? Check out some Sterling Archer quotes.

Posting Permissions

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



Click Here to Expand Forum to Full Width