Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    My code always gets total 0 on the text file.

    I just want to input numbers on the form and then total them and file it on the txt file. But the txt always says 0.
    Here's my code. Please teach me where I got wrong.

    PHP Code:
    <?php
    $centralFile 
    'central.txt'
    $message1 $message5 ""
    $last2 =$digit3$pick3 =$last3 ""
    $err 1;

    if(isset(
    $_POST["submit"])){
        
    $last2 $_POST["last2"];
        
    $last2array = array();
            
    $arrlength count($last2array);
            for (
    $x=0$x<$arrlength$x++){
                
    $last2array[$x] = $_POST["last2"];}
        if((
    $last2  == '') || !(ctype_digit($last2))){ 
            
    $last2=""
            
    $err++; 
            
    $message1 " Enter a valid number";}
        if(
    $err 1){ 
            
    $message5 "Please correct the errors";} 

    else {
        
        
    $fp fopen($centralFile'a+') or die ('ERROR: Cannot open configuration file for writing');
        
    flock($fpLOCK_EX) or die ('ERROR: Cannot lock configuration file for writing'); 
            
    $total 0;    
            foreach (
    $last2array as $value) {
                
    $total $value $total;}
                
            
    fwrite($fp"LAST 2 total is : $total\n\n");    
            
    flock($fpLOCK_UN) or die ('ERROR: Cannot unlock file'); 
            
    fclose($fp); 
            
    $message1 $message5 ""
            
    $last2 "";}
    }
    ?>
    <html> 
    <head> 
    <title>CENTRALIZE</title> 
    </head> 
    <body> 
    <h2>PATAD</h2> 

    <form method="post" action="8.php"> 
    <p>LAST 2 <input type="text" size="10" name="last2" value="<?php echo $last2?>"><span id="last2" style="color:red;"><?php echo $message1?></span></p> 
    <input type="submit" name="submit" value="Submit" /> 
    </form> 
    <div id="tot"><?php echo $message5?></div> 
    </body> 
    </html>

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,081
    Thanks
    23
    Thanked 592 Times in 591 Posts
    Please do not double post. This has been answered. DO NOT USE FOREACH()! It is for arrays and you do not have an array.
    Reference \n not working on wamp php 5.4.16
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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