Can someone tune this up pls?

Discussion in 'PHP' started by le007, Jun 10, 2008.

  1. #1
    I need a loop in here somewhere and for some reason its just writing the word 'array' to the file and not whats in my input boxes....

    also I need another page that can produce this data in individual <td> on a table - any ideas please?

    <?php
      $saving = $_REQUEST['saving'];
      if ($saving == 1) { 
        $data = $_POST["customer"] ;
    	$file = "test2.txt"; 
     
        $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); 
        fwrite($fp, $data) or die("Couldn't write values to file!"); 
     
        fclose($fp); 
        echo "Saved to $file successfully!";
      }
    ?>
    
    <form name="form1" method="post" action="http://www.website.com/test2.php?saving=1">
     
    
    <input type="text" name="customer[1][datefrom]">
    <input type="text" name="customer[1][dateto]">
    <input type="text" name="customer[1][availability]">
    <input type="text" name="customer[1][prince]">
    
    <input type="text" name="customer[2][datefrom]">
    <input type="text" name="customer[2][dateto]">
    <input type="text" name="customer[2][availability]">
    <input type="text" name="customer[2][prince]">
    
    <input type="text" name="customer[3][datefrom]">
    <input type="text" name="customer[3][dateto]">
    <input type="text" name="customer[3][availability]">
    <input type="text" name="customer[3][prince]">
    
    
    
    
    
      <?php
        $file = "test2.txt";
        if (!empty($file)) {  
    	  $file = file_get_contents("$file");
    	  echo $file;  
    	}  
      ?>
    
      <br>
    <center><input type="submit" value="Save"></center>
    </form>
    PHP:

     
    le007, Jun 10, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    I would assume this is because $data is an array (because of the values of the 'name' tag, it'll return an array) whereas fwrite takes a (string) as the second parametre.

    Implode it, or otherwise convert it to a string, otherwise you'll only return 'Array'.

    Best of luck!

    Dan
     
    Danltn, Jun 10, 2008 IP
  3. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #3
    Heh Dan,

    thanks for the reply - can you give me an example please? I'm trying to write the data that's in the 9 input boxes and also how can I reproduce them in a table?????
     
    le007, Jun 10, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    To see what I mean, try print_r($data);

    Then create a string with all the relevant data in.

    E.g.
    echo "<tr><td>{$customer[1]['datefrom']}</td>...";

    Obviously using double quotes and embedded variables isn't the fastest way, but the I'm sure the minutes you save writing it will override the millisecond you lose running it.
     
    Danltn, Jun 10, 2008 IP
  5. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    hahah good joke.....

    I'm a complete php novice trying to learn it as I go.

    Where should I put that code in? THANKS FOR THE HELP :D
     
    le007, Jun 10, 2008 IP
  6. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #6
    ^Check ur PM!
     
    rohan_shenoy, Jun 10, 2008 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Danltn thanks for your help buddy.

    Rohan you're a gent :)
     
    le007, Jun 10, 2008 IP