Jump to content

Recommended Posts

Here is my code:

[code]<?php

class 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 value

var $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 url

var $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 string
function 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 array
function 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.
Link to comment
https://forums.phpfreaks.com/topic/33265-solved-error-with-this-code-can-you-help/
Share on other sites

You dont use "var" in php (at least im not aware of it).
code should be (i think):
[code]<?php

class 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 string
function 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 array
function getVar($num) {

$myString = $newUrlArray[$i];

return $myString;

}



} //end of class

?>[/code]
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]
[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. :)
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 ()
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.

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]<?php

class 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 value

global $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 string
function 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 array
function getVar($num) {

$myString = $newUrlArray[$i];

return $myString;

}



} //end of class

?>[/code]
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]
I changed what you said and I'm still returning nothing with my code :(

[code]<?php



class 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 string
function 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 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 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..
You're right, my Java teachers in College would have given me an F because of none indention.

here, is this better?

[code]<?php



class 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]
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.
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]
thought i did :(

/cry

lol
well i guess i'll try what u said real quick and see how that works

bah i feel so stupid for wayyyy over-complicating this

However, I am curious what was wrong with my last code and why it wouldn't work, *kicks it*
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.