...

View Full Version : classes and variable life



Exodious
11-19-2003, 03:31 PM
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

mordred
11-19-2003, 03:41 PM
In your class code, you don't store values in test::historyIds. You store them in pageHasLoaded though:



$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



$this->pageHasLoaded[] = $id;


appends a new value to pageHasLoaded as well.

Exodious
11-19-2003, 03:58 PM
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

mordred
11-19-2003, 05:25 PM
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.

Exodious
11-19-2003, 05:51 PM
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 :)

mordred
11-19-2003, 08:10 PM
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



$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:



$obj = new dbComm("host", "username", "pwd", "dbname");
$_SESSION['test'] = $obj;
$obj->initPageDefinition(5);


You see the difference?

missing-score
11-20-2003, 12:37 PM
this may be unrelated, but how do you access objects in arrays?

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

Exodious
11-20-2003, 09:33 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum