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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    classes and variable life

    Code:
    class test
    {
    	var $historyIds = array();
    	
    	function initPageDefinition($id)
    	{
    		print "count pageHistoryIds: " . count($this->pageHistoryIds);
    		
    		if($this->setAndCheckHistory($pageID))
    		{
    		}
    	
    	}
    	function setAndCheckHistory($id)
    	{
    		$hasLoaded = false;
    		
    		foreach ($this->pageHistoryIds as $pageId)
    		{
    			if($pageId == $id)
    			{
    				print "<br>checking pageID(" . $id . ") against: [" . $pageId . "]";
    				$hasLoaded = true;
    			}
    		}
    		if(!$hasLoaded)
    		{
    			$arrSize = count($this->pageHasLoaded);
    			$this->pageHasLoaded[arrSize+1] = $id;
    			print "<br>adding to sizeof(" . count($this->pageHasLoaded) . ")";
    		}
    		
    		return $hasLoaded;
    	}
    }

    Above is supposed to be a test class which is is created on page a, then passed to page b using the $_SESSION object

    All seems fine and I can still access the data on page b without having to re-instantiate it.

    But when I try and update the historyIds array, the size is always zero, so whichever page I'm on, the array $historyIds only contain 1 value, am I setting it properly? or being stupid somewhere else?

    Thx

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In your class code, you don't store values in test::historyIds. You store them in pageHasLoaded though:

    Code:
    $this->pageHasLoaded[arrSize+1] = $id;
    However, you miss the '$' in front of arrSize. Could be that it evaluates to 0 and the whole expression to 1, and that's why pageHasLoaded will always set the $id value at position 1. Putting the error reporting level to E_ALL will print a warning message if you use an undefined constant like 'arrSize'.

    You don't need the $arrSize hack anyway, just writing

    Code:
    $this->pageHasLoaded[] = $id;
    appends a new value to pageHasLoaded as well.
    De gustibus non est disputandum.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    good point, but not the answer

    Been recoding bits of it, so var names have messed up, the code above is another mini-version of the problem

    Same difference though, the sizeof array goes up 1, but next time through, the size has been reset so gets re-created with only 1 value

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not quite sure now - the code above is a different version, ok, was my point about the faulty array appending obsolete as well or not?

    You need to show us the code you're using though, or we can't give qualified help.
    De gustibus non est disputandum.

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    class dbComm
    {
    	var $dbLink;
    	var $dbName;
    	var $host;
    	var $user;
    	var $pwd;
    	
    	var $resultSet;
    	
    	var $tableArr;
    	var $pageHistoryIds = array();
    	//var $hasData	= false;
    	
    	var $numColumns;
    	var $numRows;
    	
    	 # constructor
        function dbComm($h, $u, $p, $n)
        {
    		$this->host	= $h;
    		$this->user	= $u;
    		$this->pwd	= $p;
        	$this->dbName = $n;
        }
    	
    	function initPageDefinition($pageID)
    	{
    		print "<br>acting on pageID: [" . $pageID . "]";
    		print "count pageHistoryIds: " . count($this->pageHistoryIds);
    		
    		if($this->setAndCheckHistory($pageID) == false)
    		{
    			$this->dbLink = mysql_connect($this->host, $this->user, $this->pwd) or die("Could not connect: " . mysql_error());
    			print "<br>Successfully connected to mysql";
    			print "<br>Database: <b>" . $n . "</b> selected";
    			
    			mysql_select_db($this->dbName) or die("<br>Could not select database: " . $this->dbName);
    			
    			//$this->checkHistory("1");
    			
    			$sqlStr = "select pageDefinitionID, pageDefinitionName, pageDefinitionDesc, themeID from pageDefinitions where pageDefinitionID=" . $pageID;
    			print "<br>sqlStr: " . $sqlStr;
    			
    			$this->$resultSet 	= mysql_query($sqlStr) or die("<br>Query failed: " . mysql_error());
    			$this->numRows		= mysql_num_rows($this->$resultSet);
    			$this->numColumns	= mysql_num_fields($this->$resultSet);
    			
    			$this->tableArr = array($numRows => array($numColumns));
    			
    			$i = 0;
    			$rowID = 0;
    			
    			print "<br>numRows: " . $this->numRows . " - numColumns: " . $this->numColumns . " - dbLink: [" . $this->$dbLink . "]<br>";
    			
    			# fill tableArr
    			while ($row = mysql_fetch_array($this->$resultSet, MYSQL_NUM))
    			{
    				for($i=0; $i<$this->numColumns; $i++)
    				{
    					print "<br>Table/col val: [" . $row[$i] . "] - rowID: " . $rowID . " - i: " . $i . " - count(arr): " . count($tableArr);
    					$this->tableArr[$rowID][$i] = $row[$i];
    				}
    				$rowID++;
    			}
    			
    			mysql_free_result($this->$resultSet);
    			
    			mysql_close($this->dbLink);
    		}
    		else
    		{
    			print "<br><br><b>Page already loaded</b>, next step is to check the pageDefinitions to see if any of the objects are 'dirty'";
    		}
    		
    			print "<br>Table arr value 1: [" . $this->tableArr[0][1] . "]";
    		
    	}
    	
    	function setAndCheckHistory($id)
    	{
    		$hasLoaded = false;
    		
    		print "<br><br>type of $this->pageHistoryIds: " . count($this->pageHistoryIds);
    		print "<br>type: (" . gettype($id) . ") val: " . $id;
    
    		//if(array_key_exists($id, $this->pageHistoryIds)
    		if(count($this->pageHistoryIds) > 0)
    		{
    			foreach ($this->pageHistoryIds as $pageId)
    			{
    				print "<br>checking pageID(" . $id . ") against: [" . $pageId . "]";
    				
    				if($pageId == $id)
    				{
    					$hasLoaded = true;
    				}
    			}
    			
    		}
    		
    		print "<br><b>$hasLoaded: " . $hasLoaded . "</b>";
    		
    		if(!$hasLoaded)
    		{
    			$arrSize = count($this->pageHistoryIds);
    			print "<br>arrSize is: " . $arrSize;
    			// save the id
    			$this->pageHistoryIds[$arrSize+1] = $id;
    			print "<br>adding to sizeof(" . count($this->pageHistoryIds) . ")";
    		}
    		
    		return $hasLoaded;
    	}
    }
    
    //called by 
    <?php
    
    $obj = new dbComms("host", "username", "pwd", "dbname");
    $obj->initPageDefinition($_GET["id"]);
    Was trying to keep it simple, the above object gets called with varying $ids and for each session is instantiated only once, unique ids i want added to the history array and existing id's to skip the db load

    Think I might start again and build a tiny class with just array in, unless you can see anything wrong in here... which is plausible
    Last edited by Exodious; 11-19-2003 at 05:58 PM.

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hm, your class works for me... the ids get added to the array if they don't exist in it already... hm.

    How are you storing the object in the session? Do you assign a reference like

    PHP Code:
    $obj = new dbComm("host""username""pwd""dbname");
    $_SESSION['test'] =& $obj;
    $obj->initPageDefinition(5); 
    The code above will work as expected, compared when only a copy of the object gets stored:

    PHP Code:
    $obj = new dbComm("host""username""pwd""dbname");
    $_SESSION['test'] = $obj;
    $obj->initPageDefinition(5); 
    You see the difference?
    De gustibus non est disputandum.

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this may be unrelated, but how do you access objects in arrays?

    $_SESSION['test']->blah(); ?

  • #8
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, heres another question, I can keep the session object alive until the user enters a different url into the address bar

    say i have two buttons both pointing to index.php?id=1, and index.php=2 respectively

    Then manually enter index.php?id=3, or index.php=4, is this meant to kill the session object?
    Last edited by Exodious; 11-21-2003 at 03:17 PM.


  •  

    Posting Permissions

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