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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    \n not working on wamp php 5.4.16

    here's my code:

    <?php
    $x = 10;
    $y = 5;
    $z = 3;
    $sum = $x + $y;
    echo "$x + $y = $sum\n\n\n";
    $diff = $x - $y;
    echo "$x - $y = $diff";
    ?>

    output:

    10 + 5 = 15 10 - 5 = 5

    \n not working on wamp php 5.4.16-php.jpg

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    \n will add a line break to the source code output. You can see it if you click view source. Use <br> for a HTML line break.
    Evolution - The non-random survival of random variants.

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

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you sunfighter. I'm doing a project just to help my mother total(sum) some numbers. I'm done with my HTML codes (maybe) but I'm having trouble on how to post the numbers on a locked txt file. After that it should automatically total the numbers once submitted and returns to the original page to accept more numbers. I copied this code from a book but of course I've made changes which I think is suited for my project:

    <html>
    <head>
    <title>CENTRALIZE</title>
    </head>
    <body>
    <h2>PATAD</h2>
    <?php
    $centralFile = 'central.ini';
    if (isset($_POST['submit'])) {
    $data = array();
    $data['last2'] = null;
    $data['3digit'] = null;
    $data['pick3'] = null;
    $data['last3'] = null;
    if (file_exists($centralFile)){
    $lines = file($centralFile);
    foreach ($lines as $line){
    $i = count($arr) - 1;$data[$arr[0]] = $arr[$i];
    }
    }
    ?>
    <form method="post" action="central.php">
    <input type="text" size="10" name="data[last2]" value="<?php
    echo ''; ?>">LAST 2
    <p>
    <input type="text" size="10" name="data[3digit]" value="<?php
    echo ''; ?>">3 DIGIT
    <p>
    <input type="text" size="10" name="data[pick3]" value="<?php
    echo ''; ?>">PICK 3
    <p>
    <input type="text" size="10" name="data[last3]" value="<?php
    echo ''; ?>">LAST 3
    <p>
    <input type="submit" name="submit" value="Submit" />
    </form>
    <?php
    }
    else {
    $central = $_POST['data'];
    if ((trim($central['last2']) != '' && (int)$central['last2'] <= 0) ||
    (trim($central['3digit']) != '' && (int)$central['3digit'] <= 0) ||
    (trim($central['pick3']) != '' && (int)$central['pick3'] <= 0) ||
    (trim($central['last3']) != '' && (int)$central['last3'] <= 0)){
    die ('ERROR: Please enter a valid number');
    }
    $fp = fopen($centralFile, 'w+') or die ('ERROR: Cannot open configuration file for writing');
    flock($fp, LOCK_EX) or die ('ERROR: Cannot lock configuration file for writing');
    foreach ($central as $key => $value){
    if (trim($vlaue) != ''){
    fwrite($fp, "$key=$value\n") or die('ERROR: Cannot write [$key] to configuration file');
    }
    }
    flock($fp, LOCK_UN) or die ('ERROR: Cannot unlock file');
    fclose($fp);
    echo 'Configuration data successfully written to file.';

    }
    ?>
    </body>
    </html>

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    sugarray
    In the above code you have a selling error at line 50 -> if (trim($vlaue) != ''){

    The value= section doesn't echo anything and prevents it from ever echoing, so it's a useless part of your code.
    The use of flock() is only needed if more then this script can alter the file. Don't think it's needed, but left it in.
    Don't know why your trying to use arrays to do this. I give you a code to study that uses PHP to examine the form and when it's correct; write the output to a file. I used $fp = fopen($centralFile, 'a+') to append to the file.
    PHP Code:
    <?php
    $centralFile 
    'central.ini';
    $message1 $message2 $message3 $message4 $message5 "";
    $last2 =$digit3$pick3 =$last3 "";
    $err 1;
    if(isset(
    $_POST["submit"]))
    {
        
    $last2 $_POST["last2"];
        
    $digit3 $_POST["digit3"];
        
    $pick3 $_POST["pick3"];
        
    $last3 $_POST["last3"];
        if((
    $last2  == '') || !(ctype_digit($last2))){
            
    $last2="";
            
    $err++;
            
    $message1 " Enter a valid number";
        }
        if((
    $digit3  == '') || !(ctype_digit($digit3))){
            
    $digit3="";
            
    $err++;
            
    $message2 " Enter a valid number";
        }
        if((
    $pick3  == '') || !(ctype_digit($pick3))){
            
    $pick3="";
            
    $err++;
            
    $message3 " Enter a valid number";
        }
        if((
    $last3  == '') || !(ctype_digit($last3))){
            
    $last3="";
            
    $err++;
            
    $message4 " Enter a valid number";
        }
        if(
    $err 1){
            
    $message5 "Please correct the errors";
        }else{
            
    $final $last2 $digit3 $pick3 $last3;
            
    $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');

            
    fwrite($fp"LAST 2 : $last2\n");
            
    fwrite($fp"3 DIGIT : $digit3\n");
            
    fwrite($fp"PICK 3 : $pick3\n");
            
    fwrite($fp"LAST 3: $last3\n");
            
    fwrite($fp"This total is : $final\n");

            
    flock($fpLOCK_UN) or die ('ERROR: Cannot unlock file');
            
    fclose($fp);
            
    $message1 $message2 $message3 $message4 $message5 "";
            
    $last2 =$digit3$pick3 =$last3 "";
        }
    }
    ?>
    <html>
    <head>
    <title>CENTRALIZE</title>
    </head>
    <body>
    <h2>PATAD</h2>

    <form method="post" action="index.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>
    <p>3 DIGIT <input type="text" size="10" name="digit3" value="<?php echo $digit3?>"><span id="last2" style="color:red;"><?php echo $message2?></span></p>
    <p>PICK 3 <input type="text" size="10" name="pick3" value="<?php echo $pick3?>"><span id="last2" style="color:red;"><?php echo $message3?></span></p>
    <p>LAST 3 <input type="text" size="10" name="last3" value="<?php echo $last3?>"><span id="last2" style="color:red;"><?php echo $message4?></span></p>
    <input type="submit" name="submit" value="Submit" />
    </form>
    <div id="tot"><?php echo $message5?></div>
    </body>
    </html>
    You can get a grand total of this file with:
    PHP Code:
    <?php
    $timp 
    '';
    $fp fopen('central.ini''r+') or die ('ERROR: Cannot open configuration file for writing');
    while (!
    feof($fp)) {
        for(
    $z=0$z<4$z++){
            
    fgets($fp);
        }
        
    $sub fgets($fp);
        
    $timp += trim(substr($sub,16));
    }
    echo 
    $timp;
    fclose($fp);
    ?>
    Ask any questions this brings up.
    Evolution - The non-random survival of random variants.

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

  • #5
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for this sunfighter, I greatly appreciate your help. Don't get me wrong here but I'm more confused than before.
    Sorry for confusing you. I'm someone newer than the word newbie on php but I'm still very eager to learn. Going back to my project, I now understand that copying that code and replacing values was totally incorrect.

    May I explain to you what I'm trying to achieve. Right now I'm doing it very slowly coz I really get confused most of the time.

    I am creating a form which just asks for numbers and store it into a file. After accepting the input from the form php will store it into an array then takes the sum. It's really simple idea but I'm getting very confused with what codes to use. As of this moment, I've started again from the beginning and came up with this form and php code.

    this is for the form:

    <html>
    <body>
    <form method="post" action="8.php">
    <input type="text" size="5" name="last2" value=""/>LAST 2
    <input type="submit" name="submit" value="ADD" />
    </form>
    </body>
    </html>

    and my php code (which is not yet working) is this:

    <?php
    $last2n = (int)$_POST["last2"];
    $items = array();
    $count = 0;
    foreach ($last2n as $i => $number) {
    $items[$count++] = $number;
    }

    ?>

    An error occured saying "Invalid argument supplied for foreach()."

    I do understand that I could somehow put the code you've just written later on. Thank you again for your time.

  • #6
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi sunfighter. I was totally an idiot. I did not run your code first. It works fine. I think I can just work from here.

    OK back to your code. Right now, I'm trying to change it to make each form work independently. I mean, I only want each text field to be filled once at a time. I understand that on your code when I don't numbers on the other fields it display an error. I think I got you confused there. So each field will have a total of every entries it receives.

    For example:

    Attachment 12752

    an entry will be made and then the user clicks add. php will file it then totals the sum.

    Also for the .ini file. Is there a way for me to arrange the entries. I'm planning on using <br> or <p> but I don't know where to put it.

  • #7
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    Your attachment didn't work.
    Lets say you are trying to display
    LAST 2 <input type="text" size="10" name="last2" value="<?php echo $last2; ?>">
    And people have inputed 23 and 72 before that in LAST 2.
    What does this input field look like? Does it show 95 and ask for your input?


    As for your use of foreach(). They pertain to arrays and do not have an array. $last2n is a variable, a string.
    Look here to learn about foreach loops PHP: foreach - Manual
    Evolution - The non-random survival of random variants.

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

  • #8
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi sunfighter. I'm trying to get the total of the entries. Is my code correct and why does it not write the total on the txt file.

    PHP Code:
    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'); 
        
    $last2array = array();
        foreach (
    $last2array as $k=>$subArray) {
            foreach (
    $subArray as $id=>$value) {
            
    $last2array[$id]+=$value;
            
    $total $last2array[$id];}
        
        }
            
    fwrite($fp"LAST 2 total is : $total\n\n");    
            
    flock($fpLOCK_UN) or die ('ERROR: Cannot unlock file'); 
            
    fclose($fp); 
            
    $message1 $message5 ""
            
    $last2 "";} 

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    Look at these two lines
    PHP Code:
    [COLOR="#FF0000"]$last2array[/COLOR] = array();
        foreach ([
    COLOR="#FF0000"]$last2array[/COLOR] as $k=>$subArray) { 
    You declare $last2array as an array, but do not populate it so the foreach fails. DON'T USE THE FOREACH!

    Why don't you use the code I gave you? What is missing from the code?
    Evolution - The non-random survival of random variants.

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

  • #10
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry for the double post. It won't happen again and I am using your code. It's just that I'm trying to add something so as to make myself able to learn from it.


  •  

    Posting Permissions

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