I have been using PHP for several years now, and have never run across this problem. On the very last function (pageSortData()) cannot seem to access any of the protected variables in the class (IPTables). I can echo any of the variables with any of the other functions, just not the last one. Any ideas? This is really bugging me.. Thanks in advance!

#include.inc
<?PHP

class IPTables {


  private $SELF="/TEST/index.php";
  private $sqlConnection;
  private $sqlResults;
  private $fieldCount;
  private $userName;
  private $userPasswd;
  private $sqlDatabase;
  private $fileName;
  private $fileSize;
  private $fileData;
  private $fileHandle;



  public function sqlStart(){
    $this->sqlConnection = mysql_connect("<host>","<user>","<pass>");
  }

  private function sqlDatabase($database){
    mysql_select_db($database,$this->sqlConnection);
  }

  private function sqlQuery($query_string){
    $this->sqlResults = mysql_query($query_string);
  }

  private function sqlStop(){
    mysql_close($this->sqlConnection);
  }

  public function fileGetData(){
    $this->fileName = $_FILES['file_upload']['tmp_name'];
    $this->fileSize = $_FILES['file_upload']['size'];
    $this->fileHandle = fopen($this->fileName,"r");
    if(!$this->fileHandle){
      ?>
      <html>
      <head>
      <title>File Upload Error!</title>
      <?include("style.css")?>
      </head>
      <body>
      <div align="center">
      Could Not Upload File!
      <BR>
      <button onClick="history.go(-1)" class="input_button">Go Back</button>
      </body>
      </html>
      <?PHP
    }else{
      $this->fileData = fread($this->fileHandle,$this->fileSize);
      return 1;
    }
  }

  public function pageStartMenu(){
    ?>
    <html>
    <head>
    <title>Main Page</title>
    <?include("style.css")?>
    </head>
    <body>
    <form method="post" action="<?=$this->SELF?>">
    <input class="input_button" type="submit" name="batch" value="Batch">
    <br>
    <input class="input_button" type="submit" name="manual" value="Manual">
    </form>
    </body>
    </html>
    <?PHP
  }

  public function pageBatchEntry(){
    ?>
    <html>
    <head>
    <title>Batch Entry Form</title>
    <?include("style.css")?>
    </head>
    <body>
    <font face="Lucida Console">
    <form method="post" action="<?=$this->SELF?>">
    <table border=0 cellpadding=0 cellspacing=0>
    <tr><td>Number Of Fields&nbsp;&nbsp;&nbsp;&nbsp;</td><td><input class="input_text" type="text" name="field_count"></td></tr>
    <tr><td>Type Of Input</td><td>(Paste<input type="radio" name="input_type" value="paste">)&nbsp;&nbsp;&nbsp;(File<input type="radio" name="input_type" value="file">)</td></tr>
    <tr><td>User</td><td><input class="input_text" type="text" name="user_name"></td></tr>
    <tr><td>Password</td><td><input class="input_text" type="password" name="password"></td></tr>
    <tr><td>Database</td><td><select name="database">
    <?PHP
    $this->sqlStart();
    $this->sqlDatabase("MAIN");
    $this->sqlQuery("SHOW TABLES");
    while($row = mysql_fetch_array($this->sqlResults)){
      ?>
      <option value="<?=$row['Tables_in_MAIN']?>"><?=$row['Tables_in_MAIN']?></option>
      <?PHP
    }
    ?>
    </td></tr>
    <tr><td>&nbsp;</td><td><input class="input_button" type="submit" name="batch_submit" value="Continue"></td></tr>
    </font>
    </form>
    </body>
    </html>
    <?PHP
  }

  function pageFileGet(){
    $this->fieldCount = $_POST['field_count'];
    $this->userName = $_POST['user_name'];
    $this->userPasswd = $_POST['password'];
    echo $this->fieldCount;
    ?>
    <html>
    <head>
    <title>File Upload</title>
    <?include("style.css")?>
    </head>
    <body>
    <font face="Lucida Console">
    <form method="post" action="<?=$this->SELF?>" enctype="multipart/form-data">
    <table border=0 cellpadding=0 cellspacing=0>
    <tr><td>File To Upload&nbsp;&nbsp;&nbsp;</td><td><input class="input_file" type="file" name="file_upload"></td></tr>
    <tr><td></td><td><input class="input_button" type="submit" name="upload_submit" value="Upload"></td></tr>
    </table>
    </form>
    </font>
    </body>
    </html>
    <?PHP
  }

  function pageSortData(){
    ?>
    <html>
    <head>
    <title>Select Your Fields</title>
    <?include("style.css")?>
    </head>
    <body>
    <table border=0 cellpadding=0 cellspacing=0>
    <tr>
    <?PHP
    echo $this->fieldCount;  //This and everything below does not work!!
    for($a=0;$a<=$this->fieldCount-1;$a++){
      ?>
      <td><select name="Field_<?=$a?>">
      <?PHP
      $this->sqlQuery("SHOW COLUMNS FROM phone_numbers");
      while($row = mysql_fetch_array($this->sqlResults)){
        ?>
        <option id="<?=$row['Field']?>"><?=$row['Field']?></option>
        <?PHP
      }
      ?>
      </select></td>
      <?PHP
    }
    ?>
    </table>
    </body>
    </html>
    <?PHP
  }
};

#index.php

<?PHP
include("include.inc");
$page = new IPTables();
if(!$_POST){
  $page->pageStartMenu();
}
if(isset($_POST['batch'])){
  $page->pageBatchEntry();
}
if(isset($_POST['batch_submit'])){
  if($_POST['input_type'] == "file"){
    $page->pageFileGet();
  }
  if($_POST['input_type'] == "paste"){
    echo "Paste";
  }
}
if(isset($_POST['upload_submit'])){
  if($page->fileGetData() == 1){
    $page->pageSortData();
  }
}
?>

    I would suspect it has SOMETHING to do with the fact that there are opening and closing PHP tags all over the place. My first action would be to replace all that with echo's and see if that works...

      I am working on getting rid of all of the php tags, but I saw something interesting before stripping the tags.

      I found out that if I set $this->fieldCount equal to something in the function pageSortData() then it will echo whatever I set $this->fieldCount equal to. It's like the function cannot even see the variable.

        Try adding

        global $this->fieldCount;

        as the first line of that function. You shouldn't need to, but try it and see if it works.

          I got rid of all the extra PHP tags, and no luck. I tried the global $this->fieldCount and it spit back this error: Fatal error: Cannot re-assign $this in usr/local/apache2/htdocs/TEST/include.inc on line 143

          BTW I am using PHP5, MySQL5 and Apache2

          Thanks for all the replies!!

            Hmm... well if you can't reassign it, I would have to assume that it's assigned in the first place. O_o Which means I'm just as confused as you are!

            Two things. One, are you calling pageFileGet () and phpSortData () in succession? And two, the echo in pageFileGet () works and in phpSortData () it doesn't?

              Yes, and it works in pageFileGet() but not in pageSortData(). Could it possibly have anyhing to do with using enctype="multipart/form-data" in pageFileGet()?

                Anything is possible. 😕 I'm out of ideas at the point, haha. Sorry. 🙁

                  I thank you for all of your help in this problem!! (Even if it is never resolved)

                  I tried using this to verify that the variable was set:

                  if(isset($this->fieldCount)){
                    echo "SET";
                  }else{
                    echo "NOT SET";
                  }
                  

                  it echoed back "NOT SET"

                  This is bananas

                    You are very welcome for the help! That's why I signed up here. 😃

                    Try making a new function altogether like this:

                    function ImNew () {
                       echo $this->fieldCount;
                    }

                    and call that function instead of pageSortData. Near as I can tell, that variable never has a value set to it, and that is why it is "not set."

                      When the page loads, there first call is to that function, is it not? If it is, then setting the value in the previous function does nothing...

                        i thought that if i set a variable in the class, then that variable would retain that value until destoryed. Is this not the case?

                          Not if you reload the page. :xbones:

                            I would suggest passing that value in your form in a "hidden" field via

                            <input type="hidden" name="numitems" value="' . $this->fieldCount . '" />

                            and then calling the function with

                            pageSortData ($_POST['numitems']);

                            and making this your new function:

                            function pageSortData ($num) {
                               $this->fieldCount = $num;
                               // etc...
                            }

                            See if that works! 😃

                              Thank you VERY much for all the help!!! I have to head out to work for now (night shift). I will try your example tomorrow and post the results. Once again, thanks you very much!!

                                I don't really like that idea: it assumes that no-one will attempt to mess with the value before it gets sent back.

                                What is the precise flow of logic in this? The only place $fieldCount gets set is in pageFileGet(), so that has to be called before pageSortData(); the only way this can happen is if

                                1. $POST['batch_submit'] is set,

                                2. $_POST['input_type']=='file', and

                                3. $_POST['upload_submit'] is set.

                                Have I got that right?


                                On an off-side note: is there any particular reason you're using <?include('style.css')?> instead of using a stylesheet <link> tag in the HTML? It seems the latter would be simpler and quicker for all concerned.

                                Oh, and EEWWW!!! <font> tags!!!!!

                                  batch_submit and input_type=="file" is what ends up setting $this->fieldCount. once upload_submit is set, then pageSortData attempts to call $this->fieldCount. Only problem is, it never finds the variable.

                                  I didn't think about the <link> tag, and it was easier to me to do it with and include.

                                  How else would you accomplish the font deal? (<body> tag? css?)

                                    That is indeed where CSS comes in! If you want the whole body to be that font, try something like <body style="font-family: 'Lucida Console';">.

                                    Weedpacket is definitely right when he mentions that the user might try to tamper with the info in the hidden field, so instead perhaps a cookie or a session? Because I really don't see any other way...

                                      Write a Reply...