mikefrederick Posted March 9, 2008 Share Posted March 9, 2008 thanks to anyone that can help ahead of time..... What you'll see is this script adds 10 elements, and then deletes one from the middle of the list. When it deletes the one from the middle of the list, it also deletes one from the end, which shouldn't occur. You can see the script in action at: http://scripts.loado.com/vahldebug/test.php now for the code: test.php: <? require("jobs_general.php"); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Test</title> </head> <body> <pre> <? // $id = 0; // truncate test.dsv $fh = fopen("test.dsv", 'w'); fclose($fh); // instantiate class $jobs = new GeneralJobs(); for ($loopi=0; $loopi<10; $loopi++) { print "Add "."title".$loopi."\n"; $jobs->add("title".$loopi,"description".$loopi,1,"contact".$loopi,"dates".$loopi,"hours".$loopi); } print "Commit\n"; $jobs->commit(); print "\n"; // print "Debug Dump After Adding 10 Jobs:\n"; $jobsList = $jobs->getList(); for ($loopi=0; $loopi<sizeof($jobsList); $loopi++) { $job = $jobsList[$loopi]; print "id=".$job["id"]."; title=".$job["title"]."; description=".substr($job["description"],0,10)."... \n"; //$id = $job["id"]; } print "\n"; $id = $jobsList[4]["id"]; print "Delete ".$id."\n"; $jobs->delete($id); print "Commit\n"; $jobs->commit(); print "\n"; print "Debug Dump After Deleting 1 Job:\n"; $jobsList = $jobs->getList(); for ($loopi=0; $loopi<sizeof($jobsList); $loopi++) { $job = $jobsList[$loopi]; if (strcmp($job["id"],"") != 0) { print "id=".$job["id"]."; title=".$job["title"]."; description=".substr($job["description"],0,10)."... \n"; } } print "\n"; ?> </pre> </body> </html> jobsgeneral.php: <?php // Includes require_once($_SERVER["DOCUMENT_ROOT"]."/vahldebug/dsveditor.php"); /** */ class GeneralJobs { var $editor; var $index; /** */ function GeneralJobs() { $this->index = array(); $this->editor = new DSVEditor(); $this->editor->setDelimiter("|"); $this->editor->setPath("test.dsv"); $this->editor->load(); $this->refreshIndex(); } /** */ function add($title,$description,$visible,$contact,$dates,$hours) { $id = $this->getUniqueId(); $row["id"] = $id; $row["title"] = $title; $row["description"] = $description; $row["visible"] = $visible; $row["contact"] = $contact; $row["dates"] = $dates; $row["hours"] = $hours; $this->editor->addRow($this->rowUnhashed($row)); $this->refreshIndex(); return $id; } /** */ function commit() { $this->editor->save(); $this->refreshIndex(); } function delete($id) { $this->editor->remove($this->index[$id]); $this->commit(); $this->refreshIndex(); } /** */ function edit($id,$title,$description,$visible,$contact,$dates,$hours) { $row = $this->rowHashed($this->editor->getRow($this->index[$id])); $row["id"] = $id; $row["title"] = $title; $row["description"] = $description; $row["visible"] = $visible; $row["contact"] = $contact; $row["dates"] = $dates; $row["hours"] = $hours; $this->editor->setRow($this->index[$id],$this->rowUnhashed($row)); $this->refreshIndex(); } /** */ function get($id) { $row = $this->editor->getRow($this->index[$id]); return $this->rowHashed($row); } /** */ function getAdminUrl() { return "/admin/jobs"; } /** */ function getList() { $list = array(); if (is_object($this->editor)) { for ($loopi=0; $loopi<sizeof($this->editor->getRows()); $loopi++) { array_push($list,$this->rowHashed($this->editor->getRow($loopi))); } } return $list; } /** */ function getUniqueId() { $randomid = 0; do { srand(time()); $randomid = (rand()%999999)+100000; } while (array_key_exists($randomid,$this->index)); return $randomid; } /** */ function refreshIndex() { $this->index = array(); if (is_object($this->editor)) { $rows = $this->editor->getRows(); for ($loopi=0; $loopi<sizeof($rows); $loopi++) { $title = $rows[$loopi][0]; if (strcmp($title,"") != 0) { $this->index[$title] = $loopi; } } } } /** */ function rowHashed($arr) { $hash = array(); $hash["id"] = $arr[0]; $hash["title"] = $arr[1]; $hash["description"] = $arr[2]; $hash["visible"] = $arr[3]; if (is_string($hash["visible"])) { $hash["visible"] = ((strcmp($hash["visible"],"true")==0) ? true: false ); } $hash["contact"] = $arr[4]; $hash["dates"] = $arr[5]; $hash["hours"] = $arr[6]; return $hash; } /** */ function rowUnhashed($hash) { $arr = array(); $arr[0] = $hash["id"]; $arr[1] = $hash["title"]; $arr[2] = $hash["description"]; $arr[3] = $hash["visible"]; $arr[4] = $hash["contact"]; $arr[5] = $hash["dates"]; $arr[6] = $hash["hours"]; return $arr; } } ?> dvseditor.php <? /** Delimiter Seperated Value Editor */ class DSVEditor { var $baseDir = ""; var $path = ""; var $rows; var $delimiter; /** Constructor */ function DSVEditor() { $this->init(); } /** Initialize */ function init() { $this->baseDir = $_SERVER["DOCUMENT_ROOT"]."/vahldebug"; $this->rows = array(); $this->delimiter = "&"; } function addRow($items) { $this->append($items); } /** */ function append($items) { if (is_array($items)) { for ($loopi=0; $loopi<sizeof($items); $loopi++) { if (is_string($items[$loopi])) { $items[$loopi] = urlencode($items[$loopi]); } } array_push($this->rows,$items); } else { array_push($this->rows,$items); } } /** */ function getDelimiter() { return $this->delimiter; } /** */ function getId() { $id = ""; return $id; } /** */ function getRow($position) { return $this->read($position); } /** */ function getRowCount() { return sizeof($this->rows); } /** */ function getRows() { return $this->rows; } /** */ function getRowsAsString() { $content = ""; for ($loopi=0; $loopi<sizeof($this->rows); $loopi++) { $line = ""; for ($loopj=0; $loopj<sizeof($this->rows[$loopi]); $loopj++) { if ($loopj > 0) { $line .= $this->delimiter; } $line .= $this->rows[$loopi][$loopj]; } if (strcmp($line,"") != 0) { $content .= $line."\n"; } } //foreach ($this->items as $hkey => $hval) { // $content .= $hkey."=".urlencode($hval)."\n"; //} return $content; } /** */ function getPath() { return $this->path; } /** */ function getUniquePath($subDir) { $pathFull = ""; $pathSub = ""; do { srand(time()); $randomid = (rand()%999999999999)+10000000000; $pathSub = $subDir."/".$randomid.".kvp"; $pathFull = $this->baseDir."/".$pathSub; } while (file_exists($pathFull)); touch($pathFull); return $pathSub; } /** */ function load() { $path = $this->path; $handle = @fopen($path, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); $buffer = trim($buffer); if (strcmp($buffer,"") != 0) { $items = explode($this->delimiter,$buffer); array_push($this->rows,$items); } //echo $buffer; } fclose($handle); } return $this->rows; } /** */ function read($position) { $cols = array(); for ($loopi=0; $loopi<sizeof($this->rows[$position]); $loopi++) { $cols[$loopi] = urldecode($this->rows[$position][$loopi]); //print $loopi.": ".$this->rows[$position][$loopi]."=".$cols[$loopi]."<br>\n"; } return $cols; } /** */ function readAll() { $rows = array(); for ($loopi=0; $loopi<sizeof($this->rows); $loopi++) { $rows[$loopi] = $this->read($loopi); } return $rows; } /** */ function remove($pos) { unset($this->rows[$pos]); } /** */ function save() { $path = $this->path; // Let's make sure the file exists and is writable first. //if (!file_exists($path)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. //print $path; if (!$handle = fopen($path, 'w')) { echo "Cannot open file ($path)"; return; } $content = $this->getRowsAsString(); // Write $somecontent to our opened file. if (fwrite($handle, $content) === FALSE) { echo "Cannot write to file ($path)"; return; } //echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); //} else { // echo "The file $path already exists"; //} } /** */ function setBasePath($basePath) { $this->baseDir = $basePath; } /** */ function setDelimiter($d) { $this->delimiter = $d; } /** */ function setRow($pos,$row) { $this->write($pos,$row); } /** */ function setRows($rows) { $this->rows = $rows; } /** */ function setPath($subPath) { $this->path = $this->baseDir."/".$subPath; } /** */ function write($position,$items) { if (is_array($items)) { for ($loopi=0; $loopi<sizeof($items); $loopi++) { $items[$loopi] = urlencode($items[$loopi]); } $this->rows[$position] = $items; } else { $this->rows[$position] = $items; } } } ?> test.dsv: 1063198|title0|description0|1|contact0|dates0|hours0 474962|title1|description1|1|contact1|dates1|hours1 1050692|title2|description2|1|contact2|dates2|hours2 995908|title3|description3|1|contact3|dates3|hours3 792054|title5|description5|1|contact5|dates5|hours5 340774|title6|description6|1|contact6|dates6|hours6 432525|title7|description7|1|contact7|dates7|hours7 161815|title8|description8|1|contact8|dates8|hours8 Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 to save time, these are important lines: function delete($id) { $this->editor->remove($this->index[$id]); $this->commit(); $this->refreshIndex(); } $id = $jobsList[4]["id"]; print "Delete ".$id."\n"; $jobs->delete($id); print "Commit\n"; $jobs->commit(); print "\n"; print "Debug Dump After Deleting 1 Job:\n"; $jobsList = $jobs->getList(); for ($loopi=0; $loopi<sizeof($jobsList); $loopi++) { $job = $jobsList[$loopi]; if (strcmp($job["id"],"") !== 0) { print "id=".$job["id"]."; title=".$job["title"]."; description=".substr($job["description"],0,10)."... \n"; } } Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 9, 2008 Share Posted March 9, 2008 i think the problem is your use of sizeof: for ($loopi=0; $loopi<sizeof($jobsList); $loopi++) { i expect that when you unset the middle element, the sizeof() the array is less, however the highest array key has not changed. you might want to try a foreach loop instead: foreach ($jobsList AS $job) { if (strcmp($job["id"],"") !== 0) { print "id=".$job["id"]."; title=".$job["title"]."; description=".substr($job["description"],0,10)."... \n"; } } Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 this is also an important line: <?php function remove($pos) { unset($this->rows[$pos]); } Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 i tried switching that line to <=10 earlier just to see if that was an issue, but it still produced the same result. i assumed this meant that the error did not lie in that line... Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted March 9, 2008 Share Posted March 9, 2008 well, either the line is in the array or it's not. that's easy to find out. print_r($jobsList) Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 when i print_r after deletion, it shows all rows except the last one, and the one that was supposed to be deleted shows as: [4] => Array ( [id] => [title] => [description] => [visible] => [contact] => [dates] => [hours] => ) so it looks like it unsets one row and deletes the last. Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 sorry, my mistake, it turns out the last row is still there just not being printed Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 sorry again, my mistake, it is being deleted and is not printing with print_r Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 even if i change the function so that it does not clear any rows it will still delete the last one. i cant figure this out... Quote Link to comment Share on other sites More sharing options...
mikefrederick Posted March 9, 2008 Author Share Posted March 9, 2008 fixed it, nevermind Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.