Jump to content

Zaxnyd

Members
  • Posts

    29
  • Joined

  • Last visited

    Never

Everything posted by Zaxnyd

  1. Answered my own Q much more quickly than I'd thought I would/could. So here's the answer for you curious readers. <?php class InputElement { var $customValidation; var $id; function InputElement($id){ $this->id = $id; } function validate(){ if(isset($this->customValidation)) return call_user_func($this->customValidation, $this); return "normal validation for ".$this->id; } } $input = new InputElement("input1"); echo $input->validate()."<br>\n"; echo "adding custom validation...<br>\n"; $input->customValidation = create_function('$obj', 'return "custom validation for ".$obj->id;'); echo $input->validate()."<br>\n"; ?> Outputs: normal validation for input1 adding custom validation... custom validation for input1
  2. I have a form class which offers basic default validation for each input method, but I'd like to be able to override the validate() function with any given logic (without re-writing the class each time). In javascript, it is possible to simply redeclare a function, such as inputElement.validate = function(){ return this.value != "invalid value"}; which would work like a freaking charm but alas PHP does not offer this syntax. Is there an equivalent or workaround that anyone knows of? I tried eval(), but it won't work, since it's impossible to access the object's variables from within the eval string. Thanks.
  3. Is it possible to output the line of some given code, particularly for debugging? Pseuso-code: showLine();//output 1 showLine();//output 2 showLine();//output 3 //etc...
  4. append_url = 'contact_info.php? Might be irrelevant, but I noticed this line needs a closing, like so: append_url = 'contact_info.php?';
  5. Solved it. It was the pesky plus sign that was doing it. So I just did an eregi_replace with a placeholder and it works wonderfully. Here's the class I made. Some of you might appreciate it: class url_serializable { function loadFromSerial($sSerial) { $urldecode = urldecode($sSerial); $urldecode = eregi_replace("--PLUS--", "\+", $urldecode); $base64_decode = base64_decode($urldecode); $unserialize = unserialize($base64_decode); return $this->loadFromObject($unserialize); } function sSerialized() { $sSerialized = $this; $sSerialized = serialize($sSerialized); $sSerialized = base64_encode($sSerialized); $sSerialized = eregi_replace("\+", "--PLUS--", $sSerialized); $sSerialized = urlencode($sSerialized); return $sSerialized; } }
  6. Solved it. It was the pesky plus sign that was doing it. So I just did an eregi_replace with a placeholder and it works wonderfully. Here's the class I made. Some of you might appreciate it: class url_serializable { function loadFromSerial($sSerial) { $urldecode = urldecode($sSerial); $urldecode = eregi_replace("--PLUS--", "\+", $urldecode); $base64_decode = base64_decode($urldecode); $unserialize = unserialize($base64_decode); return $this->loadFromObject($unserialize); } function sSerialized() { $sSerialized = $this; $sSerialized = serialize($sSerialized); $sSerialized = base64_encode($sSerialized); $sSerialized = eregi_replace("\+", "--PLUS--", $sSerialized); $sSerialized = urlencode($sSerialized); return $sSerialized; } }
  7. I'm looking for a failsafe way to send a serialized php object through AJAX. I posted <a href="http://www.phpfreaks.com/forums/index.php/topic,137636.0.html" target="_blank">my problem in the php section</a>, but haven't had a whole lot of luck there. Maybe you ajax pros have figured a way to do this. This is my failed attempt: (it works up until the data is passed via url, when it becomes corrupted) $serialize = serialize($myObject); $base64_encode = base64_encode($serialize); $urlencode = urlencode($base64_encode); //pass via Ajax / GET $urldecode = urldecode($urlencode ); $base64_decode = base64_decode($urldecode); $myObject= unserialize($base64_decode);
  8. I tried the ajax post method and the same conversion seems to occur.
  9. I had my encode/decode functions output the data at each stage of the transformation. I had it encode the data, then decoded it locally. Then it passed the data via URL to be decoded again. Most notable are the differences between the "As received" for the pre-pass and post-pass decodings. Encoding... this: Object id #6 serialize: O:10:"Classified":9:{s:2:"ID";s:1:"6";s:6:"userID";s:1:"1";s:4:"date";s:10:"0000-00-00";s:7:"listing";s:6:"Wanted";s:8:"category";s:8:"Antiques";s:7:"subject";s:7:"Testing";s:4:"body";s:191:" This is a p This is a list I am javascript ";s:8:"approved";N;s:5:"table";s:20:"wolfcreek_classified";} base64_encode: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== urlencode TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw%2BDQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ%3D%3D -------------------------------------------------------------------------------- Decoding... As received: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6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b3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== base64_decode: O:10:"Classified":9:{s:2:"ID";s:1:"6";s:6:"userID";s:1:"1";s:4:"date";s:10:"0000-00-00";s:7:"listing";s:6:"Wanted";s:8:"category";s:8:"Antiques";s:7:"subject";s:7:"Testing";s:4:"body";s:191:" This is a p This is a list I am javascript ";s:8:"approved";N;s:5:"table";s:20:"wolfcreek_classified";} unserialize: Object id #22 -------------------------------------------------------------------------------- Pass via URL -------------------------------------------------------------------------------- Decoding... As received: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw+DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== urldecode: TzoxMDoiQ2xhc3NpZmllZCI6OTp7czoyOiJJRCI7czoxOiI2IjtzOjY6InVzZXJJRCI7czoxOiIxIjtzOjQ6ImRhdGUiO3M6MTA6IjAwMDAtMDAtMDAiO3M6NzoibGlzdGluZyI7czo2OiJXYW50ZWQiO3M6ODoiY2F0ZWdvcnkiO3M6ODoiQW50aXF1ZXMiO3M6Nzoic3ViamVjdCI7czo3OiJUZXN0aW5nIjtzOjQ6ImJvZHkiO3M6MTkxOiI8cD4gVGhpcyBpcyBhIHAgPC9wPg0KPHVsPg0KPGxpPlRoaXMgaXMgYSBsaXN0PC9saT4NCjwvdWw DQo8c2NyaXB0Pg0KLy8gSGVyZSBpcyBzb21lICBqYXZhc2NyaXB0DQpkb2N1bWVudC53cml0ZSgnSSBhbSBqYXZhc2NyaXB0Jyk7DQo8L3NjcmlwdD4NCjxzdHlsZT4NCi5zaGVsbCB7DQpjb2xvcjojMEYwOw0KfQ0KPC9zdHlsZT4NCiI7czo4OiJhcHByb3ZlZCI7TjtzOjU6InRhYmxlIjtzOjIwOiJ3b2xmY3JlZWtfY2xhc3NpZmllZCI7fQ== base64_decode: unserialize:
  10. It's actually being passed via Ajax, so I don't think session would work. Is there no way to make a guaranteed URL-safe, string version of any given object without the corruption of loss of the serialized data?
  11. I'm passing an object of my own design.
  12. I need to serialize an object and pass it via URL. I've tried this, and many variations of it, but nothing seems to pass seemlessly in all cases: $sSerialized = serialize($myObj); $sSerialized = base64_encode($sSerialized); $sSerialized = urlencode($sSerialized); //pass via url, then on next page: $sSerialized = urldecode($sSerialized); $sSerialized = base64_decode($sSerialized); $myObj= unserialize($sSerialized); Seems like this should be a very easy thing to do, but I can't seem to get the hang of it. Thanks in advance.
  13. The answer was given in this thread: [url=http://www.phpfreaks.com/forums/index.php/topic,124051.0.html]http://www.phpfreaks.com/forums/index.php/topic,124051.0.html[/url]
  14. Is it possible to gather/output all of the method names within a class (particularly in php4)? seudocode: [code] class foo (   function1 {}   function2 {}   //soforth ) //desired result: // array("function1", "function2", soforth...); [/code]
  15. I need to push items and a reference key into an array, essentially an associative version of array_push(). I haven't been able to find a way to do this. If anyone could assist, I'd appreciate it. Thanks in advance.
  16. This returend properly: [code]$aList = array(     "ofoo" => $oFoo,     "obar" => $oBar,     "ozar" => $oZar ); [/code] Still looking for a solution, though...
  17. $item->color is returning correctly. Your most recent solution unfortunately won't suffice in my situation, as I won't actually know the number of items or colors being dealt with...
  18. How about running a query to pull all the ID's, then adding them into an array, sorting the array, and using their index numbers?
  19. I even tried this... [code] function sortByColor($array){     $list = array();     foreach($array as $item){       if($list[$item->color] == null)         $list["test"] = array();       $list["test"][] = $item;     }     return $list; } [/code] And got... [code] array(1) {   [1]=>      //Why doesn't this say ["test"]???   array(1) {     ["Zar"]=>     object(item)(1) {       ["name"]=>       string(3) "Zar"       ["color"]=>       string(4) "blue"     }   } } [/code]
  20. [!--quoteo(post=384808:date=Jun 16 2006, 06:00 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 06:00 PM) [snapback]384808[/snapback][/div][div class=\'quotemain\'][!--quotec--] how about taking away $list=array(); seeing as you'll be defining the keys anyway [/quote] A great idea. However, still not working... It's strange; it's being grouped properly, but it's just not making it associative. It seems "$list[$item->color][] = $item;" is not passing "$item->color" correctly. It seems like a syntactical error, but I've tried all these to no avail: $list['$item->color'][] = $item; $list["$item->color"][] = $item; $list["".$item->color.""][] = $item; [code] var_dump($aSortedList ); /* actual result (not associative) array(2) {   [0]=>      //I want this to say ["Red"]   array(2) {     ["Foo"]=>     object(item)(1) {       ["name"]=>       string(3) "Foo"       ["color"]=>       string(3) "red"     }     ["Bar"]=>     object(item)(1) {       ["name"]=>       string(3) "Bar"       ["color"]=>       string(3) "red"     }   }   [1]=>      //I want this to say ["Blue"]   array(1) {     ["Zar"]=>     object(item)(1) {       ["name"]=>       string(3) "Zar"       ["color"]=>       string(4) "blue"     }   } } */ [/code]
  21. [!--quoteo(post=384796:date=Jun 16 2006, 05:44 PM:name=joquius)--][div class=\'quotetop\']QUOTE(joquius @ Jun 16 2006, 05:44 PM) [snapback]384796[/snapback][/div][div class=\'quotemain\'][!--quotec--] $list[$item->color] = array(); you have only defined $list as an array you need one for each key value this should fix it [/quote] Thanks for the quick response! I just tried this, to no avail: [code] function sortByColor($array){     $list = array();     foreach($array as $item){       if($list[$item->color] == null)         $list[$item->color] = array();       $list[$item->color][] = $item;     }     return $list; } [/code] I still get a numerated, not associative, array result [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
  22. I have objects (with values "color" and "name") in an array. I want a function that will take this array and return a two level, associative array indexed by color. For example, my array initially contains "foo", "bar", and "zar", each having the colors "red", "red", and "blue" respectively. The result I'm looking for is an array containing the arrays "red" (containing "foo" and "bar") and "blue" (containing "zar"). With the function below, instead of getting an associative array as expected, I'm getting a numerated one. Can anyone tell me why? [code] //...(class definitions, instantiations, etc)...// function sortByColor($array){     $list = array();     foreach($array as $item){       $list[$item->color][] = $item;  //this isn't working right... syntax problem?     }     return $list; } $oFoo->name = "Foo"; $oFoo->color = "red"; $oBar->name = "Bar"; $oBar->color = "red"; $oZar->name = "Zar"; $oZar->color = "blue"; $aList = array($oFoo,$oBar,$oZar); $aSortedList = sortByColor($aList); var_dump($aList ); /*yelds: array(3) {   [0]=>   object(item)(1) {     ["name"]=>     string(3) "Foo"     ["color"]=>     string(3) "red"   }   [1]=>   object(item)(1) {     ["name"]=>     string(3) "Bar"     ["color"]=>     string(3) "red"   }   [2]=>   object(item)(1) {     ["name"]=>     string(3) "Zar"     ["color"]=>     string(4) "blue"   } } */ var_dump($aSortedList ); /*  Desired (not actual) result... array(2) {   ["red"]=>   array(2) {     ["Foo"]=>     object(item)(1) {       ["name"]=>       string(3) "Foo"       ["color"]=>       string(3) "red"     }     ["Bar"]=>     object(item)(1) {       ["name"]=>       string(3) "Bar"       ["color"]=>       string(3) "red"     }   }   ["blue"]=>   array(1) {     ["Zar"]=>     object(item)(1) {       ["name"]=>       string(3) "Zar"       ["color"]=>       string(4) "blue"     }   } } */ [/code]
  23. That's exactly the solution I was looking for. Brilliant! Thank you!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.