Create variables from text file lines.

Discussion in 'PHP' started by MrLeN, Dec 4, 2012.

  1. #1
    I have a text file that looks like this:

    bill
    ben
    bob
    bruce
    barney

    I want to use php to look at the text file and make a link out of each line:

    <a href="/bill.html">bill</a>
    <a href="/ben.html">ben</a>
    <a href="/bob.html">bob</a>
    <a href="/bruce.html">bruce</a>
    <a href="/barney.html">barney</a>

    How can I do this?

    I think I have to make some kind of array and a loop or something. I don't really know how (exactly).

    If someone could help me, I will appreciate it :)
     
    Solved! View solution.
    MrLeN, Dec 4, 2012 IP
  2. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #2
    something like that

    text.txt
    script.php
    <?php
    $file = 'text.txt';
    $ar = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($ar as $item) {
        $item=trim($item);
        print "<a href=\"/$item.html\">$item</a>\n";
    }
    ?>
    PHP:
     
    Sano000, Dec 4, 2012 IP
  3. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Hey thanks mate. I had actually got what I needed on another forum, but I was just trying to figure out how to get rid of the extra line that's at the end (which is causing an error).

    Then, in your example, I see: FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES

    That might be what I need. I'll see if I can include that now!
     
    MrLeN, Dec 4, 2012 IP
  4. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #4
    wow, that worked heaps better than the other solution. Thanks heaps mate!

    I initially tried to insert FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES into the code I had, but it wouldn't work.

    So, I just rewrote the whole section using your example.

    PERFECT! Thanks mate!

    Can I ask you another question?

    ...

    
    <?php
    if (!isset($_POST['email'])) {
      exit;
    }
    $file = 'members/' . $_POST['email'] . '.php';
    if (!file_exists($file)) {
      echo "There is no record of that email.";
    	exit;
    }
    $ar = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($ar as $item) {
        $item=trim($item);
        echo '<a href="http://pages.xxxxx.com/?page='.$item.'">' . $item . '</a><br />';
    		include_once 'pages/' . trim($item) . '.php';
    		echo $key . '<br />';
    		echo '<a href="http://pages.xxxxx.com/edit.php?page='.$item.'&key='.$key.'">Edit</a><br />';
    }
    /*
    $pages = fopen($file, "r");
    while (!feof($pages) ) {
        $current_line = fgets($pages);
        echo '<a href="http://pages.xxxxx.com/?page='.$current_line.'">' . $current_line . '</a><br />';
    		include_once 'pages/' . trim($current_line) . '.php';
    		echo $key . '<br />';
    		echo '<a href="http://pages.xxxxx.com/edit.php?page='.$current_line.'&key='.$key.'">Edit</a><br />';
    }
    fclose($pages);
    */
    
    exit;
    
    ?>
    <?php
    // multiple recipients
    $to  = $_POST['email'] . ', '; // note the comma
    $to .= 'xxxxx@gmail.com';
    
    // subject
    $subject = 'xxxxx Key Reminder';
    
    // message
    $message = '
    <html>
    <head>
      <title>xxxxx Pages Key</title>
    </head>
    <body>
    
    </body>
    </html>
    ';
    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Additional headers
    /*
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    */
    $headers .= 'From: xxxxx Pages <no-reply@xxxxx.com>' . "\r\n";
    /*
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
    */
    
    // Mail it
    mail($to, $subject, $message, $headers);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>xxxxx Pages</title>
    	<?php include_once 'inc/meta.php'; ?>
    </head>
    
    <body>
    <div class="wrap">
    <div class="content" style="margin-top: 20%">
    <h3>Lost Key Reminder</h3>
    <ul>
    <li>The key(s) associated with this email address have been emailed.</li>
    <li>If you do not see the email, check your spam folder.</li>
    </ul>
    </div></div>
    
    </body>
    </html>
    
    Code (markup):
    In the above code, I want to output the loop section into the email $message = ''; in between <body></body> of the email message,

    I have been trying to figure out how to get it in there for hours, but I have had no luck.

    I've tried to put the loop inside a fuction, but that didn't work.

    I've tried to put the while loop in the message area, but that didn't work.

    And I tried a heap of other wird stuff, but I can't get it to work.
     
    MrLeN, Dec 4, 2012 IP
  5. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #5
    ie:

    I want to put this:

    
    $ar = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach($ar as $item) {
        $item=trim($item);
        echo '<a href="http://pages.xxxxx.com/?page='.$item.'">' . $item . '</a><br />';
    		include_once 'pages/' . trim($item) . '.php';
    		echo $key . '<br />';
    		echo '<a href="http://pages.xxxxx.com/edit.php?page='.$item.'&key='.$key.'">Edit</a><br />';
    }
    
    Code (markup):
    In...

    
    $message = '
    <html>
    <head>
      <title>xxxxx Pages Key</title>
    </head>
    <body>
    HERE
    HERE
    HERE
    HERE
    HERE
    HERE
    HERE
    HERE
    HERE
    HERE
    </body>
    </html>
    ';
    
    Code (markup):
     
    MrLeN, Dec 4, 2012 IP
  6. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #6
    Try this

    
    deleted, look below
    
    PHP:
    It could be some problem in include_once 'pages/' . trim($item) . '.php';. Write me PM if will not work
     
    Last edited: Dec 4, 2012
    Sano000, Dec 4, 2012 IP
  7. #7
    ups, that was wrong variant, try this

    <?php
    function get_message_body() {
        if (!isset($_POST['email'])) {
          return false;
        }
        $file = 'members/' . $_POST['email'] . '.php';
        if (!file_exists($file)) {
            //echo "There is no record of that email.";
            return false;
        }
        $content = '';
        $ar = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach($ar as $item) {
            $item=trim($item);
            $content .=  '<a href="http://pages.xxxxx.com/?page='.$item.'">' . $item . '</a><br />'."\n";
            include_once 'pages/' . trim($item) . '.php';
            $content .= $key . '<br />';
            $content .= '<a href="http://pages.xxxxx.com/edit.php?page='.$item.'&key='.$key.'">Edit</a><br />';
        }
        return $content;
    }
    /*
    $pages = fopen($file, "r");
    while (!feof($pages) ) {
        $current_line = fgets($pages);
        echo '<a href="http://pages.xxxxx.com/?page='.$current_line.'">' . $current_line . '</a><br />';
            include_once 'pages/' . trim($current_line) . '.php';
            echo $key . '<br />';
            echo '<a href="http://pages.xxxxx.com/edit.php?page='.$current_line.'&key='.$key.'">Edit</a><br />';
    }
    fclose($pages);
    */
    
    
    ?>
    <?php
    $body = get_message_body();
    if(!$body) {
        echo "There is no record of that email.";
        exit;
    }
    
    // multiple recipients
    $to  = $_POST['email'] . ', '; // note the comma
    $to .= 'xxxxx@gmail.com';
    
    // subject
    $subject = 'xxxxx Key Reminder';
    
    
    // message
    $message = '
    <html>
    <head>
      <title>xxxxx Pages Key</title>
    </head>
    <body>
    '.$body.'
    </body>
    </html>
    ';
    
    // To send HTML mail, the Content-type header must be set
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    
    // Additional headers
    /*
    $headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
    */
    $headers .= 'From: xxxxx Pages <no-reply@xxxxx.com>' . "\r\n";
    /*
    $headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
    $headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
    */
    
    // Mail it
    mail($to, $subject, $message, $headers);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>xxxxx Pages</title>
        <?php include_once 'inc/meta.php'; ?>
    </head>
    
    <body>
    <div class="wrap">
    <div class="content" style="margin-top: 20%">
    <h3>Lost Key Reminder</h3>
    <ul>
    <li>The key(s) associated with this email address have been emailed.</li>
    <li>If you do not see the email, check your spam folder.</li>
    </ul>
    </div></div>
    
    </body>
    </html>
    
    
    PHP:
     
    Sano000, Dec 4, 2012 IP
  8. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #8
    wow Sano000, you rock!

    You got it right the first time too!

    You're an expert coder.

    I only wish I was as good as you - serious.

    Thanks heaps mate.

    You just ended hours of me scratching my head (and secretly wanting to cry).

    I appreciate your help heaps.
     
    MrLeN, Dec 4, 2012 IP
  9. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #9
    thanks, but use second code
     
    Sano000, Dec 4, 2012 IP
  10. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #10
    okies thanks -- what is the difference between the codes?

    The first one you gave works.

    I will change it, but I am just wondering what the difference is?
     
    MrLeN, Dec 4, 2012 IP
  11. Sano000

    Sano000 Active Member

    Messages:
    52
    Likes Received:
    4
    Best Answers:
    5
    Trophy Points:
    53
    #11
    First one could show warning messages in some situations. Variable $file was not declared correctly
     
    Sano000, Dec 4, 2012 IP
  12. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #12
    okies, thanks mate. I am very happy now. My script is finished :D

    That was the last problem.

    Test it out: http://pages.triorbit.com

    It allows you to craete web pages.

    Do you liek it?

    The code we were just working on allows you to get your page and key if you lost it.
     
    MrLeN, Dec 4, 2012 IP
  13. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #13
    Actually, I just realised I am experfiencing difficulties with the script lol

    It was working before...

    ...*looking*

    P.S. Not the part you helped me with -- but with a file creation.

    I'll fix it. *looking*
     
    MrLeN, Dec 4, 2012 IP
  14. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #14
    Hmmmm, for some reason $myFile is creating the file but not writing the content inside the file..

    
    $myFile = 'pages/'.$randomPage.'.php';
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "<?php\n";
    fwrite($fh, $stringData);
    $stringData = "\$email = \"".$_POST['email']."\";\n";
    fwrite($fh, $stringData);
    $stringData = "\$key = \"".$randomKey."\";\n";
    fwrite($fh, $stringData);
    $stringData = "?>";
    fwrite($fh, $stringData);
    fclose($fh);
    
    $myFile2 = 'members/'.$_POST['email'].'.php';
    $fh = fopen($myFile2, 'a') or die("can't open file");
    $stringData = "".$randomPage."\n";
    fwrite($fh, $stringData);
    fclose($fh);
    
    Code (markup):
    I can't work out why, yet...
     
    MrLeN, Dec 4, 2012 IP
  15. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #15
    Ok, I don't know what's going on..

    ..sometimes it works, sometimes it doesn't.

    I just did a heap of testing, and sometimes it creates the file with all the text in the file, sometimes it just creates a blank file.

    I think it might be a server problem.
     
    MrLeN, Dec 4, 2012 IP
  16. MrLeN

    MrLeN Well-Known Member

    Messages:
    406
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    120
    #16
    I got it working.

    The problem was caused by an error log that was tremendously large.

    I deleted it and everything started working normally.

    I suspect that the error log became so large because I accidently made a recurring loop that never ends, and I made a gynormous error file.

    :) <--- hack, lol
     
    MrLeN, Dec 4, 2012 IP