SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    array_pop function

    This is a comment script i've gotten so far but i wanted to add the array_pop function so It would delete the comments after so many lines that way I wouldn't have to go and edit the file everytime it got to long. Its not working and i'm wondering do i even have it in the right place? Also When writing to a flat file, how would i get it to write to the top instead of the bottom? *In The Learning Phase*

    Here is my code...
    PHP Code:
    <?php
    $site1
    "links.php";   
    $var11 stripslashes$links ); 
    $var12 stripslashes $user  );
    $var13 stripslashes $www  );
    $file "links.txt";                  
    $fileopen fopen "$file"a" );   
    fputs($fileopen"                           
     <br /><a href=\"
    $var13\" >".$var12."<a />  
    :&nbsp;&nbsp; 
    "
    .$var11." <br />
     \n"
    );    
    if (
    count($file)>10
    array_pop($file);
    fclose($fileopen);                             
    header("Location: {$site1}");          
    ?>
    Thanks in advance..

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I see it, $file is just your filename, not the file contents. I think you want to read in the file as an array like this:

    PHP Code:
    <?php
    $filecontents 
    file($file);
    array_unshift($filecontents,$)
     
    # add comment to front of file, not end
    if (count($file)>10
      
    array_pop($filecontents,
    <br /><a href=""\"
    $var13\" >".$var12."<a /> 
    :&nbsp;&nbsp; 
    "
    .$var11." <br />
    \n"
    );
     
    # rewrite file entirely.
     
    ?>

    Also When writing to a flat file, how would i get it to write to the top instead of the bottom? *In The Learning Phase*
    Unfortunately you can't just put something at the top of the file - you have to re-write the file. So read it in as an array (as above) and use array_unshift() to stick something on the front, then write the file. you'll need to open the file for writing (w), which will truncate the file.

    Hope I got the code right.
    Paul Davey
    webmaster for Whitford Church of Christ

  3. #3
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i've gotten an error when trying to use that array_unshift

    PHP Code:
    <?php
    $site1
    "links.php";   
    $var11 stripslashes$links ); 
    $var12 stripslashes $user  );
    $var13 stripslashes $www  );
    $file "links.txt";   
    $filecontents file($file);
    array_unshift($filecontents,$)
    # add comment to front of file, not end
    if (count($file)>10
      
    array_pop($filecontents,
    <br /><a href=""\"
    $var13\" >".$var12."<a /> 
    :&nbsp;&nbsp; 
    "
    .$var11." <br />
    \n" 
    );
    fclose($fileopen);
    header("Location: {$site1}");
    # rewrite file entirely.

    ?>
    Parse error: parse error, unexpected ')', expecting T_VARIABLE or '$' in /home/isolated/public_html/test/updatelinks.php on line 8

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I left off a semi-colon, below it is corrected:

    PHP Code:
    <?php
    $site1
    "links.php";   
    $var11 stripslashes$links );
    $var12 stripslashes $user  );
    $var13 stripslashes $www  );
    $file "links.txt";   
    $filecontents file($file);
    array_unshift($filecontents,"
    <br /><a href=""\"
    $var13\" >".$var12."<a />
    :&nbsp;&nbsp;
    "
    .$var11." <br />
    \n"
    );
    # add comment to front of file, not end
    if (count($file)>10)
      
    array_pop($filecontents);
    fclose($fileopen);
    header("Location: {$site1});
    # rewrite file entirely.

    ?>
    Edited to fix up array_pop and array_unshift
    Paul Davey
    webmaster for Whitford Church of Christ

  5. #5
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This isnt' writing to the file, is the array_unshift replacing the fputs() ? Also I had to remove the fclose in order to get it to run without an error...

    Sorry for your troubles, but thanks for helping..
    Here is what I have now, runs with no errors, but doesn't write to the file.
    PHP Code:
    <?php 
    $site1
    "links.php";    
    $var11 stripslashes$links ); 
    $var12 stripslashes $user  ); 
    $var13 stripslashes $www  ); 
    $file "links.txt";    
    $filecontents file($file); 
    array_unshift($filecontents,
    <br /><a href=\"
    $var13\" >".$var12."<a /> 
    :&nbsp;&nbsp; 
    "
    .$var11." <br /> 
    \n" 
    ); 
    # add comment to front of file, not end 
    if (count($file)>10
      
    array_pop($filecontents); 
    header("Location: {$site1}); 
    # rewrite file entirely. 
    ?>
    With this code I'm getting another parse error, tried editing it, but just get a new error..
    Parse error: parse error, unexpected '\"' in /home/isolated/public_html/test/updatelinks.php on line 9

    PHP Code:
    <?php 
    $site1
    "links.php";    
    $var11 stripslashes$links ); 
    $var12 stripslashes $user  ); 
    $var13 stripslashes $www  ); 
    $file "links.txt";    
    $filecontents file($file); 
    array_unshift($filecontents,
    <br /><a href=""\"
    $var13\" >".$var12."<a /> 
    :&nbsp;&nbsp; 
    "
    .$var11." <br /> 
    \n" 
    ); 
    # add comment to front of file, not end 
    if (count($file)>100
      
    array_pop($filecontents); 
    header("Location: {$site1}); 
    # rewrite file entirely. 
    ?>

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Caged

    no troubles. The code I posted does not write the file, it just does the shifting and popping of the array. To write the file, join the array to a string, and then write this string to the file.

    There was an extra double quote on the line 9 you posted.

    PHP Code:
    <?php
    $site1
    "links.php";    
    $var11 stripslashes$links );
    $var12 stripslashes $user  );
    $var13 stripslashes $www  );
    $file "links.txt";    
    $filecontents file($file);
    # add comment to front of file, not end
    array_unshift($filecontents,'
    <br /><a href="' 
    $var13 " >".$var12."<a />
    :&nbsp;&nbsp;
    "
    .$var11." <br />
    \n" 
    );
    #pop comment off end
    if (count($file)>100)
      
    array_pop($filecontents);
    header("Location: {$site1});
    # rewrite file entirely.
    $filepointer fopen($file,'w');
    fputs($filepointer,join("",$filecontents));
    fclose($filepointer);
    ?>

    Hope it works for you

    Edited to fix up vB's slashes and quotes
    Paul Davey
    webmaster for Whitford Church of Christ

  7. #7
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That code is wrapping my form with the link and isn't printing it to the page, however i can see it when I mouseover my form elements..Gettin close..

    Here is my form code if you would like to try it for yourself.

    PHP Code:
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style>
    }

    .link {
        font-size: xx-small;
        color: #666666;
        text-decoration: none;
    }
    </style>
    <link href="Style2.css" rel="stylesheet" type="text/css" />
    </head>

    <body class="link" bgcolor="#333333">
    <?php
    require("links.txt");
    ?>

    <form method=post action=updatelinks.php class="link">
      Name: <input name="user" type="text" class="textarea" />
      <br />
      Website: <font size="1" face="arial">
      <input name="www" type="text" class="textarea"  value="http://" />
      </font><br />
      <br />
     Comments: <textarea name="links" cols="25" rows="5" class="textarea" style="background-color:#333333; color:#996666;">

    </textarea><br><br>

    <input type="submit" name=" Submit " value="Submit">
      <input type="reset" name=" Reset " value="Reset">
    </form> 
    </font>
    </body>
    </html>
    I'm using that in conjunction with the code you've posted above if you test it you can better see what its doing.

  8. #8
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should have spotted this before (well, I think this is the problem). There are some extra linebreaks in the string you add/write to the file - each line is separated when the file is read in as an array using the file() function, so these would cause extra (blank) array elements. So I have removed the extra linebreaks and just left in the \n. It means the resulting HTML may not look as nice as it would if you typed it up, but you nbeed it like this for the file() function.

    It might have been better to store the link info as delimited text (or even in a database) rather than as formatted HTML since then you would avoid the linebreak problem. So you would output as HTML the string you are currently adding to the front of the file, and just store the variable values in the file. It would make the links.txt file more flexible in the future.

    PHP Code:
    <?php
    $site1
    "links.php";    
    $var11 stripslashes$links );
    $var12 stripslashes $user  );
    $var13 stripslashes $www  );
    $file "links.txt";    
    $filecontents file($file);
    # add comment to front of file, not end
    array_unshift($filecontents,'<br /><a href="' $var13 " >".$var12."<a />:&nbsp;&nbsp;".$var11." <br />\n" );
    #pop comment off end
    if (count($file)>100)
      
    array_pop($filecontents);
    header("Location: {$site1});
    # rewrite file entirely.
    $filepointer fopen($file,'w');
    fputs($filepointer,join("",$filecontents));
    fclose($filepointer);
    ?>
    Paul Davey
    webmaster for Whitford Church of Christ

  9. #9
    SitePoint Zealot Caged's Avatar
    Join Date
    May 2003
    Location
    United States
    Posts
    107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well thanks for all your help bobby, I still couldn't get it to work, it seemed to do the same thing again. I'm not gonna worry you with it any more, maybe attempt another method as you suggested. But as i'm in the learning process the db method might be a little bit more tricky for me. Once again, thanks for all your help


Bookmarks

Posting Permissions

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