Welcome to Welcome to DNF.com™ - Domain Sales, Domain Forum, Domain Appraisals, Domain Registrars

If you are new to domains and looking to buy, sell and learn about domains then you have come to the right place. DNForum is the largest domain name community on the internet and continues to grow every day. There are over 105,000 domainers on DNForum doing everything from buying domains, selling domains, learning about domains and discussing domains. Take a minute and Register.

Register Today on DNForum IT'S FREE!

Results 1 to 7 of 7
  1. #1
    DNF Member

    Join Date
    Oct 2004
    Location
    Sahuarita, Arizona, USA
    Posts
    212
    DNF$
    4,908
    Bank
    0
    Total DNF$
    4,908
    Donate  

    [PHP] problem writing to a flat-text file

    I'm tweaking an existing script that allows me to input information like a URL and TITLE to a flat-file but when I attempt to customize the script to write a DESC (description) to the file the field gets filled with a value called "Array".


    Code:
    $lfile = fopen("data.dat", "w");
     foreach ($site as $num=>$href) {
    if($href!= "" && $text[$num]!= "" && $desc!= "") {
     $daysstart = "";
     $daysremaining = "";
     if(trim($sdays[$num])!= "") { $daysstart = formatdays($sdays[$num]); }
     if(trim($edays[$num])!= "") { $daysremaining = formatdays($edays[$num]); }
     fwrite($lfile, $href . "¦¦¦" . $text[$num] . "¦¦¦" . $desc . "¦¦¦" . $daysstart . "¦¦¦" . $daysremaining . "\n");
    }
     }
     fclose($lfile);
    Any ideas?
    ...thanks in advance for any assistance.

  2. #2
    Jedi Master
    Bob's Avatar
    Join Date
    Apr 2002
    Posts
    3,138
    DNF$
    49,928
    Bank
    0
    Total DNF$
    49,928
    Donate  

    Re: [PHP] problem writing to a flat-text file

    Where and how is $desc being defined/initialized? The code thinks $desc is an array.

    Post those line(s) of code where $desc is initialized.

    Alternatively, you can check to see what is in $desc[0]. $desc[1]. $desc[2], etc, and see if what you need is in one of those elements.

    -Bob
    . . .

  3. #3
    Mr Flippy Returns..
    Beachie's Avatar
    Join Date
    Aug 2002
    Posts
    2,096
    DNF$
    9,606
    Bank
    0
    Total DNF$
    9,606
    Donate  

    Re: [PHP] problem writing to a flat-text file

    Bob is bang on the money there, and if $desc is an array, the if (...$desc!= "") won't be working quite how you intend either..

    Band.com | Surfer.com | Everybody.com | Late.com

  4. #4
    Jedi Master
    Bob's Avatar
    Join Date
    Apr 2002
    Posts
    3,138
    DNF$
    49,928
    Bank
    0
    Total DNF$
    49,928
    Donate  

    Re: [PHP] problem writing to a flat-text file

    Quote Originally Posted by Beachie
    and if $desc is an array, the if (...$desc!= "") won't be working quite how you intend either..
    Beachie is bang on the money too.

    You will need to do something like:

    . . . if($desc[0] != "") . . .

    -Bob
    . . .

  5. #5
    DNF Member

    Join Date
    Oct 2004
    Location
    Sahuarita, Arizona, USA
    Posts
    212
    DNF$
    4,908
    Bank
    0
    Total DNF$
    4,908
    Donate  

    Re: [PHP] problem writing to a flat-text file

    First, thanks to bob and beachie for their replies.

    Here is the rest of the code:
    Code:
    <?php
    
     session_start();
     if(isset($_POST['pass'])) { $_SESSION['admin'] = $_POST['pass']; }
    
     if($_SESSION['admin'] != "newpass") {
       login();
       return 0;
     }
    
    
     if($_POST['dowhat'] == "write") {
       $site = $_POST['site'];
       $text = $_POST['text'];
       $desc = $_POST['desc'];
       $sdays = $_POST['sdays'];
       $edays = $_POST['edays'];
    
       $lfile = fopen("data.dat", "w");
       foreach ($site as $num=>$href) {
    	if($href != "" && $text[$num] != "" && $desc != "") {
    	  $daysstart = "";
    	  $daysremaining = "";
    	  if(trim($sdays[$num]) != "") { $daysstart = formatdays($sdays[$num]); }
    	  if(trim($edays[$num]) != "") { $daysremaining = formatdays($edays[$num]); }
    	  fwrite($lfile, $href . "|||" . $text[$num] . "|||" . $desc  . "|||" . $daysstart . "|||" . $daysremaining . "\n");
    	}
       }
       fclose($lfile);
     } 
    ?>
     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
       <title>Title Here</title>
      </head>
    
      <body><br />
        <form method="post">
    
      <div align="center">
        <div id="manager" align="center">
         <table border="0" cellspacing="5" cellpadding="5">
          <tr>
    	<td width="20%"><strong>URL</strong></td>
    	<td width="20%"><strong>Title</strong></td>
    	<td width="30%"><strong>Description</strong></td>
    	<td width="15%"><strong>Start</strong></td>
    	<td width="15%"><strong>Remain</strong></td>
          </tr>
    <?php
     $lines = file('links.cgi');
     $num = 0;
     foreach ($lines as $line) {
      $data = explode("|||", $line);
      $syear = substr($data[3],0,4);
      $sday = substr($data[3],4);
      $eyear = substr($data[4],0,4);
      $eday = substr($data[4],4);
      $daysstart = "";
      $daysremaining = "";
      if(trim($data[3]) != "") { $daysstart = formatcode($syear,$sday); }
      if(trim($data[4]) != "") { $daysremaining = formatcode($eyear,$eday); }
    
      echo "<tr>\n<td><input type=\"text\" name=\"site[]\" value=\"$data[0]\"></td>";
      echo "<td><input type=\"text\" name=\"text[]\" value=\"$data[1]\"></td>";
      echo "<td><input type=\"text\" name=\"desc[]\" value=\"$data[2]\"></td>";
      echo "<td><input type=\"text\" name=\"sdays[]\" id=\"s" . $num . "\" value=\"" . $daysstart . "\" size=\"2\"></td>";
      echo "<td><input type=\"text\" name=\"edays[]\" id=\"e" . $num . "\" value=\"" . $daysremaining . "\" size=\"2\"></td>\n</tr>";
      $num++;
     }
    ?>
           <tr>
    	<td><input type="text" name="site[]" value=""></td>
    	<td><input type="text" name="text[]" value=""></td>
    	<td><input type="text" name="desc[]" value=""></td>
    	<td><input type="text" name="sdays[]" id="s<?php echo $num ?>" value="" size="2"></td>
    	<td><input type="text" name="edays[]" id="e<?php echo $num ?>" value="" size="2"></td>
          </tr>
         </table>
         <input type="hidden" name="dowhat" value="write">
         <input type="submit" name="submit" value="Submit">
         </form>
       </div>
       </div>
      </body>
    </html>
    
    
    
    <?php
    
    function login() {
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
       <title>Link Manager Login</title>
       <link rel="stylesheet" type="text/css" href="layout.css" />
       <link rel="stylesheet" type="text/css" href="presentation.css" />
      </head>
    
    
    <body>
    <br /><br /><br />
    <div style="text-align: center">
    <div id="links">
    <form method="post">
    Please Login: <input type="text" name="pass">
    <input type="submit" value="Login">
    </form>
    </div>
    </div>
    </body>
    </html>
    </html>
    
    <?php
    }
    
    function formatdays($daystogo) {
      $ryear = date("Y");
      $rday = date("z");
    
      $rday += $daystogo;
    
      while($rday > 364) {
        $rday -= 365;
        $ryear++;
      }
    
      return ($ryear . $rday);
    }
    
    function formatcode($year, $day) {
      $ryear = date("Y");
      $rday = date("z");
      $daysleft = "";
    
      if($ryear == $year) { $daysleft =  $day - $rday; }
      else { 
    
    	$rday = 364 - $rday;
    	$ryear++;
    
    	while($ryear < $year) {
    	   $rday = 365 + $rday;
    	   $ryear++;
    	}
    	$daysleft = ($rday + $day) + 1;
      }
    
      return ($daysleft);
    }
    ?>
    Thank you all for your assistance, I really don't understand a lot of this stuff but generarally I can play around with the code enough to get it to do what I want. In this case I added all the "desc" stuff and formatted it exactly like the existing code and it works fine except when writing the data to the file. I can manually insert the data into "data.dat" and the PHP file reads it correctly; however the minute I save the data all the "desc" values in the "data.dat" file get overwritten with the value "Array"...

  6. #6
    Jedi Master
    Bob's Avatar
    Join Date
    Apr 2002
    Posts
    3,138
    DNF$
    49,928
    Bank
    0
    Total DNF$
    49,928
    Donate  

    Re: [PHP] problem writing to a flat-text file

    The error is caused by this line:

    PHP Code:
    echo "<td><input type=\"text\" name=\"desc[]\" value=\"$data[2]\"></td>"
    The problem are the brackets in the name field: name=\"desc[]\"

    When you use brackets like this, the information is passed back to the PHP script as an array.

    There are two ways to fix this:

    1) Remove the [] after "desc" and make the line:

    PHP Code:
    echo "<td><input type=\"text\" name=\"desc\" value=\"$data[2]\"></td>"
    2) You can simply extract the data from the 0th element of the array and write the extracted value to the text file (or simply write the 0th element to the text file).

    example:
    PHP Code:
    $siteDescription $data[0];
    fwrite($lfile$href "|||" $text[$num] . "|||" $siteDescription  "|||" $daysstart "|||" $daysremaining "\n"); 
    or more simply:

    PHP Code:
    fwrite($lfile$href "|||" $text[$num] . "|||" $desc[0]  . "|||" $daysstart "|||" $daysremaining "\n"); 
    Personally, I would approach it from #2. You really should not mess with the HTML names in case something else is going on somewhere where the PHP is looking for that array name. You run the risk of breaking it in another place.

    Cheers!

    -Bob
    Last edited by Bob; 03-12-2006 at 02:33 PM.
    . . .

  7. #7
    DNF Member

    Join Date
    Oct 2004
    Location
    Sahuarita, Arizona, USA
    Posts
    212
    DNF$
    4,908
    Bank
    0
    Total DNF$
    4,908
    Donate  

    Re: [PHP] problem writing to a flat-text file

    Thanks Bob...

    I'll keep banging away at it until I get it to work

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
  •  

Domain name forum recommended by Domaining.com