magic2goodil Posted January 7, 2007 Share Posted January 7, 2007 Here is my code:[code]<?phpclass urlUtility {var $query = $_SERVER['QUERY_STRING'];var $urlArray = explode('&', $query);//loops back through a new array of size $i//creates new array with pointer and valuevar $newUrlArray = array(count($urlArray));var $count = 0;while($count < count($urlArray)) {foreach($urlArray as $url) {//breaking down urlArray into array pointer and array value//allows for later implosion back to urlvar $pointer = substr($url, 0, strpos($url, "="));var $value = substr($url, strpos($url, "=") + 1, strlen($url));$newUrlArray["$pointer"] = $value;}$count++;}$count = 0;//used for breaking Array back to original url query stringfunction doImplode($myArray) {while($count < count($myArray)) {foreach($myArray as $key => $value) {if($count == 0) {var $newString = "";$count++;}else if($count < count($myArray) -1) {$newString .= $key . "=" . $value . "&";$count++;}else {$newString .= $key . "=" . $value;$count++;}}}return $newString;}//takes an int as an argument and returns that value in the arrayfunction getVar($num) {var $myString = $newUrlArray[$i];return $myString;}} //end of class?>[/code]The error to start is in line 5 or:[code]var $query = $_SERVER['QUERY_STRING'];[/code]I switched it to that instead of the original line 5 because i got the same error as i am now. ORiginal line was:[code]var $urlArray = explode('&', $_SERVER['QUERY_STRING']);[/code]Anyways, here is the error I am getting which is basically stating the error is in the line I showed in the class file itself:[code]Parse error: parse error, unexpected T_VARIABLE in /home/collegeb/public_html/urlUtility.class.php on line 5[/code]All that code worked fine until I made it into a class of its own and added the words var next to my variables when first initializing them.Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/ Share on other sites More sharing options...
papaface Posted January 7, 2007 Share Posted January 7, 2007 You dont use "var" in php (at least im not aware of it).code should be (i think):[code]<?phpclass urlUtility {$query = $_SERVER['QUERY_STRING'];$urlArray = explode('&', $query);//loops back through a new array of size $i//creates new array with pointer and value$newUrlArray = array(count($urlArray));$count = 0;while($count < count($urlArray)) {foreach($urlArray as $url) {//breaking down urlArray into array pointer and array value//allows for later implosion back to url$pointer = substr($url, 0, strpos($url, "="));$value = substr($url, strpos($url, "=") + 1, strlen($url));$newUrlArray["$pointer"] = $value;}$count++;}$count = 0;//used for breaking Array back to original url query stringfunction doImplode($myArray) {while($count < count($myArray)) {foreach($myArray as $key => $value) {if($count == 0) {$newString = "";$count++;}else if($count < count($myArray) -1) {$newString .= $key . "=" . $value . "&";$count++;}else {$newString .= $key . "=" . $value;$count++;}}}return $newString;}//takes an int as an argument and returns that value in the arrayfunction getVar($num) {$myString = $newUrlArray[$i];return $myString;}} //end of class?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155350 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 Yea I tried that too and got this error, sorry forgot to mention had already tried that:[code]Parse error: parse error, unexpected T_VARIABLE, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /home/collegeb/public_html/urlUtility.class.php on line 5[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155353 Share on other sites More sharing options...
Jessica Posted January 8, 2007 Share Posted January 8, 2007 [s]I am pretty new to classes, but I thought you could only define the variable, but not assign a value. IE:var $query;and then in the initialization function, you give it a default value.[/s]Edit: Tutorial says otherwise. Hm..Maybe it needs to be $urlArray = explode('&', $this->query); ??Sorry, hope you figure it out.@devnerds: you do in classes. :) Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155354 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Sorry but I dont see any fault with the var lines, however, you cannot have loops and the like outside of methods within a class. All logic must be contained within a function. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155364 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 i am reading about __construct() for php classes...does whatever is in there automatically get called when my class is initialized with $blah = new urlUtility(); ???also do i need $blah = new urlUtility(); or is it $blah = new urlUtility; without the () Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155397 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 yes the __construct is automatically called when you initiate your class. __construct is php5 only though, in php4 you get the same effect by naming your construct the same as your class. eg;[code]<?php class foo { function foo() { echo "this is a php4 __construct"; } }?>[/code]And no. $obj = new class; is fine unless you are passing args to the construct. I perfer to use $obj = new class(); just for asthestics. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155400 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 hmm, turns out my server runs on php4 so thanks for the tips on constructs..now i am having a problem pulling $newUrlArray into the function doImplode()the objective is to take the array that I have and be able to pull it into that function..but i think the scope of my variable or the way I am calling it is not working..now before you suggest i call it as an argument, i want to do that but how would i specify said array argument once i create a new urlUtility object in my outside code on another page...here's the new code:[code]<?phpclass urlUtility {function urlUtility() {$query = $_SERVER['QUERY_STRING'];$urlArray = explode('&', $query);//loops back through a new array of size $i//creates new array with pointer and valueglobal $newUrlArray;$newUrlArray = array(count($urlArray));$count = 0;while($count < count($urlArray)) {foreach($urlArray as $url) {//breaking down urlArray into array pointer and array value//allows for later implosion back to url$pointer = substr($url, 0, strpos($url, "="));$value = substr($url, strpos($url, "=") + 1, strlen($url));$newUrlArray["$pointer"] = $value;}$count++;}}//used for breaking Array back to original url query stringfunction doImplode() {$count1 = 0;$myArray = $newUrlArray;while($count1 < count($myArray)) {foreach($myArray as $key => $value) {if($count1 == 0) {$newString = "";$count1++;}else if($count1 < count($myArray) -1) {$newString .= $key . "=" . $value . "&";$count1++;}else {$newString .= $key . "=" . $value;$count1++;}}}echo $newString;}//takes an int as an argument and returns that value in the arrayfunction getVar($num) {$myString = $newUrlArray[$i];return $myString;}} //end of class?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155420 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Sorry... for starters, you should never need to make properties (variables) global within methods (functions) within a class. You can use the $this keyword to refer to properties which are contained within your class.Secondly, why would you ever need to pass this array as an argument? It is all based on the $_SERVER array which is a superglobal. Meening available within all scope.If you ever wanted to pass an argument to your construct you would do it as follows.[code]<?php class foo { function foo($string) { echo $string; } } $obj = new foo("hello");?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155423 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 I changed what you said and I'm still returning nothing with my code :([code]<?phpclass urlUtility {var $newUrlArray;function urlUtility() {$query = $_SERVER['QUERY_STRING'];$urlArray = explode('&', $query);//loops back through a new array of size $i//creates new array with pointer and value$newUrlArray = array(count($urlArray));$count = 0;while($count < count($urlArray)) {foreach($urlArray as $url) {//breaking down urlArray into array pointer and array value//allows for later implosion back to url$pointer = substr($url, 0, strpos($url, "="));$value = substr($url, strpos($url, "=") + 1, strlen($url));$newUrlArray["$pointer"] = $value;}//end foreach$count++;}//end while}//end construct//used for breaking Array back to original url query stringfunction doImplode() {$count1 = 0;$myArray = $this->newUrlArray;while($count1 < count($myArray)) {foreach($myArray as $key => $value) {if($count1 == 0) {$newString = "";$count1++;}else if($count1 < count($myArray) -1) {$newString .= $key . "=" . $value . "&";$count1++;}else {$newString .= $key . "=" . $value;$count1++;}}//end foreach}//end whilereturn $newString;}//end doImplode//takes an int as an argument and returns that value in the arrayfunction getVar($num) {$myString = $this->newUrlArray[$i];return $myString;}} //end of class?>[/code]And here is the way I am calling it as a test before I make it a session variable to be passed into a page loaded into an iframe..it should print out something before iframe is loaded in theory in this test:[code]<?php require_once('urlUtility.class.php');require_once('makeSession.php');$myUrl = new urlUtility();echo $myUrl->doImplode();$_SESSION['urlUtil'] = $myUrl;echo" <iframe src=\"" . $Home . ".php\" id=\"homeNav\" height=\"650\"></iframe>";?>[/code]before that iframe is even loaded..the String made from doImplode should be echoed..but I get nothing..when I changed the code for doImplode to just echo "crap"; it worked..but nothing happens now with the real code..i fear it isn't pulling in the var $newUrlArray correctly.. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155434 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Sorry, but here is where I suggest indenting your code into logical blocks. I honestly cant / wont read it. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155443 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 You're right, my Java teachers in College would have given me an F because of none indention.here, is this better?[code]<?phpclass urlUtility {var $newUrlArray; function urlUtility() { $query = $_SERVER['QUERY_STRING']; $urlArray = explode('&', $query); $newUrlArray = array(count($urlArray)); $count = 0; //loops back through a new array of size $i //creates new array with pointer and value while($count < count($urlArray)) { //breaking down urlArray into array pointer and array value //allows for later implosion back to url foreach($urlArray as $url) { $pointer = substr($url, 0, strpos($url, "=")); $value = substr($url, strpos($url, "=") + 1, strlen($url)); $newUrlArray["$pointer"] = $value; } //end foreach $count++; } //end while } //end construct //used for breaking Array back to original url query string function doImplode() { $count1 = 0; $myArray = $this->newUrlArray; while($count1 < count($myArray)) { foreach($myArray as $key => $value) { if($count1 == 0) { $newString = ""; $count1++; } //end if else if($count1 < count($myArray) -1) { $newString .= $key . "=" . $value . "&"; $count1++; } //end else if else { $newString .= $key . "=" . $value; $count1++; } //end else } //end foreach } //end while return $newString; } //end doImplode() //takes an int as an argument and returns that value in the array function getVar($num) { $myString = $this->newUrlArray[$i]; return $myString; } //end getVar()} //end of class?>[/code][code]<?php require_once('urlUtility.class.php');require_once('makeSession.php');$myUrl = new urlUtility();echo $myUrl->doImplode();$_SESSION['urlUtil'] = $myUrl;echo" <iframe src=\"" . $Home . ".php\" id=\"homeNav\" height=\"650\"></iframe>";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155452 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 What do you want this code to do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155455 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 The final objective is to pull the Query string from the address bar, aka my GET variables.Then, it will store them into a urlUtility object so that I can pass that object into my iframe as a Session variable. I have code on the page that I want to pull that session data once loaded into the iframe. The goal is to be able to use my GET variables in an iframe which cannot to my knowledge be directly called as GET variables in an iframe. So i came up with this idea last night to pull the query string into a class that has functions to break it back down, getVar, and another function i will add later getVarByKey so as to call the value by a specific key. In theory if I pass this class object over a session to my iframe, i can then easily call say my isbn being passed in the address bar for my book search by using getVarByKey("isbn");..I can then use this on the rest of the page and every time a new search is done before the page loads itself in the iframe it will create a new urlUtility object and then pass that to the search in the iframe page. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155458 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Ive never used iframes so not too sure on there detail. However, you could just store the entire $_GET array into a session var. eg;[code]<?php session_start(); $_SESSION['get'] = $_GET;?>[/code]Then to call any variables....[code]<?php session_start(); echo $_SESSION['get']['foo'];?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155463 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 oh, lolwell crap...so ur saying i should scrap this code, lol Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155469 Share on other sites More sharing options...
trq Posted January 8, 2007 Share Posted January 8, 2007 Well, you dont really need it is all. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155470 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 thought i did :(/crylolwell i guess i'll try what u said real quick and see how that worksbah i feel so stupid for wayyyy over-complicating thisHowever, I am curious what was wrong with my last code and why it wouldn't work, *kicks it* Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155471 Share on other sites More sharing options...
magic2goodil Posted January 8, 2007 Author Share Posted January 8, 2007 I suppose I will mark this as solved although this particular problem was not solved the overall problem was solved in simple manner. Thanks Thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/#findComment-155494 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.