Jump to content

Joining Variables?


will_1990

Recommended Posts

Hello!

 

I am trying to send a variable that is updated 'x' amount of times depending on the number of check boxes that are selected.

 

eg a someone can selected two items and the unique id will be posted twice here:

 

<?php
if(!($lines = file('php_ffdb.txt'))) 
{echo 'ERROR: Unable to open file! </body></html>'; exit;} 
foreach ($_POST as $ID2 => $price) {
foreach($lines as $theline) {
list($ID, $title, $author, $description, $price, $image) = split('\|',$theline); 
if($ID==$ID2) 
{ 
   echo " 
   <tr>
    <td>$ID</td>
   <td>$price</td>
	</tr>

 

However, i want to post these ids off to a text file and it only picks up the final one selected.

 

currently i am using this code:

 

  <input type=hidden name=ID value=$ID2> 

 

But if i echo $ID2 it gives me ids, 1, 2, 7 for example but it would only print 7 in the text file.

 

Here is what i am using to write to a text file:

 

<?php 
$myFile = "purchases.txt";
$fh = fopen($myFile, 'a') or die("can't open file\n");
if(is_readable($myFile)) { echo "<p>File is readable </p>";}
else { echo "<p>File is Not readable</p>"; }
if( is_writable($myFile)) {echo "<p>File is writable </p>";}
else { echo "<p>File is Not writable</p>";}

$Name=$_POST['Name'];
$Email=$_POST['Email'];
$Price=$_POST['Price'];
$ID=$_POST['ID'];

$content =$Name."|".$Email."|". $Price."|".$ID."\n";

if(!(fwrite($fh , $content)))
{  echo "<p>Cannot write to file ($myFile)\n</p>";  exit; } 
else {  echo "<p>Data written</p> 
          <a href='purchases.php' >Return to Purchases</a>";
         fclose($fh);
      }
?>

 

I have no idea how to force this to either store each ID seperately so i can then export them separtely or store them all togeather to export them togeather...

 

ANY hints or tips would be much appreciated!!!

 

Sorry if i have not explain this well, im a bit of a newbie!!  ;)

 

Many thanks in advance

 

Will

Link to comment
Share on other sites

Where you write the file I only see $ID; I don't see $ID2.

 

I applaud you for attempting error-checking in your file handing code, but you've gone a bit overkill with the is_readable() and is_writable() IMO.  In addition your code is calling fclose() only if it couldn't write to the file.  You need to call fclose() when the file handle is valid and you are done with it.

 

A typical file writing block might look like:

<?php
$fp = fopen( 'the_file.txt', 'a' ); // or use mode 'w'
if( $fp ) {
    fwrite( $fp, 'line 1' );
    fwrite( $fp, 'line 2' );
    // or write in a loop
    for( $i = 0; $i < 5; $i += 1 ) {
        fwrite( $fp, 'loop: ' . $i );
    }
    fclose( $fp ); unset( $fp ); // finally close and lose the file handle
}
?>

 

You'll notice I didn't do much error checking during the fwrite() calls.  If you want to be more robust and check for errors but don't want a bunch of nested if...else, then you can use a nifty trick with do...while.  You create a nifty loop like so:

<?php
$success = false;
do {
  // some stuff

  $success = true;
} while( false );
?>

 

The loop only executes one time, but it has the advantage of supporting the break statement.  This means we can very easily drop out of the loop at any point in time.  By setting $success to false at the beginning of the loop, if we drop out early, $success will still be false at the end of the loop.  However if the code makes it to the very end of the loop (the line above the while( false )), then we assume all operations within the loop were successful so we set $success to true.

 

So here's a better example:

<?php
    
$fp = fopen( 'the_file.txt', 'a' ); // or use mode 'w'
if( $fp ) {
    $success = false; // Assume failure
    do {
        if( ! fwrite( $fp, 'line 1' ) ) break; // if one write fails, end the loop
        if( ! fwrite( $fp, 'line 2' ) ) break; // if one write fails, end the loop
        // or write in a loop
        $success = true; // Now we assume success
        for( $i = 0; $success && $i < 5; $i += 1 ) {
            if( ! fwrite( $fp, 'loop: ' . $i ) ) $success = false;
        }
        // Since we assumed success=true right before the loop, if success is
        // now false we know a write failed, so we end the loop again
        if( ! $success ) break;
        
        $success = true; // if the code made it this far in the do...while,
                         // then we know all of the writes were successful
    } while( false );
    if( $success ) echo "All file writes were made correctly ";
    else echo "Not all file writes were done correctly ";
    
    // finally we close the handle
    if( fclose( $fp ) ) echo "and the file handle was closed.";
    else echo "and there was a problem closing the file handle.";
    
    unset( $fp ); // it's a good idea to "lose" the file handle, an old C habit
}    

?>

 

(edit) I almost forgot, you can also forgo the fopen(), fwrite(), fclose() nonsense and do it all with file_put_contents().  The availability of that function depends on your version of PHP and it may not support file appending, although you could do something like:

<?php
$file = '/some/file.txt';
$append = 'some content' . PHP_EOL;
file_put_contents( $file, (is_file( $file ) ? file_get_contents( $file ) : '') . $append );
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.