1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Php - Simple php help please

Discussion in 'Programming & Webmastering' started by james888, Jun 12, 2012.

  1. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    I am trying to get update a page text by the variable. I know I am doing this wrong. I am not that good at php but am using php for this project I am on. I am pretty sure I am using the while function wrong. I have tried googling this but can't find an answer because I am not sure what I am doing wrong.

    So the two things I really need help with are how to properly do while (251 < $variable <= 350). This way doesn't work.
    And how to end the while loop so it does not repeat. That endwhile causes and error.

    I know these are pretty simple. I just can't remember how to do it.

    PHP:
    <?php
    $variable 
    50

    $file0 
    file_get_contents("file0.txt");
    $file1 file_get_contents("file1.txt");
    $file2 file_get_contents("file2.txt");


    while (
    $variable <= 250) {
        echo 
    $file0;
        }
    while (
    251 $variable  <= 350) {
        echo 
    $file1;
        }
    while (
    351 $variable  <= 500) {
        echo 
    $file2;
        }
    endwhile;
    ?>
     
    Last edited: Jun 12, 2012
    Crunching for Team TPU
  2. Ev1LrYu

    Ev1LrYu

    Joined:
    Nov 2, 2010
    Messages:
    166 (0.11/day)
    Thanks Received:
    21
    Location:
    Philippines! :D
  3. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    That may be... I will try that too. Right now it isn't liking the second while function. That is really what I am asking about. Thank you for pointing that out though. That is probably another problem I would of came across later.

    Edit:
    This does do a better job than include
     
    Last edited: Jun 12, 2012
    Crunching for Team TPU
  4. Ev1LrYu

    Ev1LrYu

    Joined:
    Nov 2, 2010
    Messages:
    166 (0.11/day)
    Thanks Received:
    21
    Location:
    Philippines! :D
    oh btw, I don't think you are using 'while' correctly. 'while' is used for doing loops, ex:

    //counting to ten
    $i = 1; //start with 1
    while ($i <= 10){ //test if less than or equal to 10
    echo $i."<br />"; //if it is, print it out
    $i++; // increase the counter
    }

    If I understand you correctly then you need to use the If/then/else control structure as documented for php here: http://php.net/manual/en/control-structures.elseif.php
     
  5. W1zzard

    W1zzard Administrator Staff Member

    Joined:
    May 14, 2004
    Messages:
    15,038 (3.88/day)
    Thanks Received:
    11,948
    i'm having a hard time trying to figure out what you are trying to do. maybe reword the question?

    for ($i=251; $i<350; $i++)
    {
    echo "$i\n";
    }

    is that what you are looking for ?
     
  6. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.82/day)
    Thanks Received:
    5,619
    Location:
    Cheeseland (Wisconsin, USA)
    I think he wants to do a logical OR

    while ($variable < 251 || $variable > 350)
    {
    do something;
    }
    endwhile;

    I could be wrong. :/
     
    james888 says thanks.
  7. digibucc

    digibucc

    Joined:
    May 21, 2009
    Messages:
    4,947 (2.42/day)
    Thanks Received:
    1,508
    Easy Rhino says thanks.
  8. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    Thank you all for trying to help. Its been quite a while since I took any coding/php lessons, and I wasn't that great to begin with.

    Thank you kreij, that code got me a bit further.
    I will try to clear up what I am trying to accomplish.

    PHP:
    <?php
    $variable 
    250//I have been using 50, 150, and 250 to test.

    $file0 file_get_contents('./file0.txt'true);
    $file1 file_get_contents('./file1.txt'true);
    $file2 file_get_contents('./file2.txt'true);

    while (
    $variable <= 100//puts file0.txt content onto the page as it should
    {
        echo 
    $file0;
    }
    while (
    $variable 101 || $variable <= 200//puts file1.txt content onto the page as it should

        echo 
    $file1;
    }
    while (
    $variable 201 || $variable <= 300//puts file1.txt content onto the page, it should put file2.txt.

        echo 
    $file2;
    }
    endwhile; /
    Causes errorParse errorsyntax errorunexpected T_ENDWHILE in ...
    ?>
    file0.txt, file1.txt, and file 2.txt all have their names for content. I triple checked. So when the variable is 250 it puts file1 onto the page, but should be putting file2 onto the page.
     
    Last edited: Jun 12, 2012
    Crunching for Team TPU
  9. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,464 (1.29/day)
    Thanks Received:
    650
    Location:
    Geneva, FL, USA
    Setup of a while()
    PHP:
    // Way #1
    while ( evaluation ):
        
    #do something
    endwhile;

    // Way #2
    while ( evaluation ) {
        
    #do something
    }
    But it doesn't seem like you need a while() for this. An if/elseif or switch, like digibucc mentioned is better. And if you're only going to use the contents of one of those files at a time, don't load them all in:
    PHP:
    switch ( $variable ) {
        case 
    50:
            
    $fn 'file0.txt';
        break;
        case 
    150:
            
    $fn 'file1.txt';
        break;
        case 
    250:
            
    $fn 'file2.txt';
        break;

    }

    echo 
    file_get_contents($fn);

    Depending on what you're trying to accomplish, it may be as easy as:
    PHP:
    echo file_get_contents('file'.$variable.'.txt');
    (change the file names to match the variable or change the variables to match the file names)
     
    digibucc says thanks.
  10. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    The variable will be changing a lot, and always going up. This is only a sample of the code I am having problems with. I am looking to see whether the variable is between two numbers. Does that case funtion do that?

    Like
    PHP:
    // Way #1
    while ( evaluatate if between x and ):
        
    #insert content of file
    endwhile;

    // Way #2
    while ( evaluation ) {
        
    #do something
    }
     
    Crunching for Team TPU
  11. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.82/day)
    Thanks Received:
    5,619
    Location:
    Cheeseland (Wisconsin, USA)
    You need an endwhile for each while. But you only need one.

    Code:
    bool AreWeHappy = false;
    
    while (!AreWeHappy) // we're not happy yet.
    {
       if (x < somevalue) do whatever;
       else if (x > somevalue) do whatever;
       else AreWeHappy = true; // condition met, we are now happy
    }
    endwhile;
    
    Adjust for correct syntax, I'm not good at php
     
  12. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    Are you saying like this?

    PHP:
    <?php
    $variable 
    250;

    $file0 file_get_contents('./file0.txt'true);
    $file1 file_get_contents('./file1.txt'true);
    $file2 file_get_contents('./file2.txt'true);

    while (
    $variable <= 100
    {
        echo 
    $file0;
    }
    endwhile;
    while (
    $variable 101 || $variable <= 200)

        echo 
    $file1;
    }
    endwhile;
    while (
    $variable 201 || $variable <= 300)

        echo 
    $file2;
    }
    endwhile;
    ?>
     
    Crunching for Team TPU
  13. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (4.82/day)
    Thanks Received:
    5,619
    Location:
    Cheeseland (Wisconsin, USA)
    No, you don't need three while loops, you just need one that drops out when the condition is met.
    Use if statements in the while loop. If php has a break statement to kill the loop you could do something like ...

    While (true)
    {

    if ($variable <= 100) echo $file0;
    else if ($variable >= 101 && $variable <= 200) echo $file1;
    else if ($variable >= 201 && $variable <= 300) echo $file2;
    else break;

    }
    endwhile;

    In the case you are presenting you need logical AND (&&) not OR (||).
     
    Last edited: Jun 13, 2012
  14. james888

    james888

    Joined:
    Jun 27, 2011
    Messages:
    4,746 (3.72/day)
    Thanks Received:
    1,956
    This looks perfect to me. Can you explain why I would choose "and" or "or." It still doesn't like that endwhile
     
    Crunching for Team TPU
  15. Jizzler

    Jizzler

    Joined:
    Aug 10, 2007
    Messages:
    3,464 (1.29/day)
    Thanks Received:
    650
    Location:
    Geneva, FL, USA
    No endwhile if you use curly brackets.

    Nearly all of the time you'll see the bracket structure used when looking at PHP code (less typing). But it does have an alternative structure for if, while, for, foreach, and switch if you wish to use it.

    http://www.php.net/manual/en/control-structures.alternative-syntax.php


    As for "&&" vs "||", let's set your $variable to 210. What happens? 210 is greater than 101 OR less than 200. This is true and you'll echo $file1 for all variables greater than 100. But is it not greater than 101 AND less than 200.
     
  16. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,869 (6.48/day)
    Thanks Received:
    2,469
    Location:
    Concord, NH
    Because the syntax is incorrect. I recommend reading up on what each control structure is used for before just using them. I was a little horrified when I saw the first post. I know developers when they do a code review to strip out all comments and read just the cost, because good code should be readable, so consider that when you write code. Also I noticed you referred to "while" as a function which is not true, it is a control structure built into PHP.

    Let me try and clean it up a bit.

    PHP:
    <?php
    $variable 
    250;
    $file0 file_get_contents('./file0.txt'true); 
    $file1 file_get_contents('./file1.txt'true); 
    $file2 file_get_contents('./file2.txt'true); 

    while (
    $variable 300) {
        if (
    $variable <= 100) {
            echo 
    $file0;
        } else if (
    $variable >= 101 && $variable <= 200) {
            echo 
    $file1;
        } else {
            echo 
    $file2;
        }
    }
    Will "variable" always be 250 because then you can assume what the result is going to be so all this extra logic isn't needed.

    Could you explain why you're trying to do with the program, not how you're trying to program it? Sometimes understanding the problem will help you design and write the code to do it because I'm blindly editing code without really knowing what you're trying to accomplish.

    I also recommend print_r and var_dump for debugging and `php -l scriptname.php` to check the syntax. I actually have a bash script that checks all .php files in a directory recursively, but you need to be running *nix with the PHP cli tool.

    Code:
    #!/bin/bash
    find . | grep "\(php\|html\)$" | xargs -ifoo php -l foo
    
     

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page