Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Re: $_GET strings seperation
Old
  (#1)
Jared Farrish
Guest
 
Posts: n/a
Default Re: $_GET strings seperation - 06-02-2007, 07:55 PM

On 5/26/07, Navid Yar <EMAIL REMOVED> wrote:
> Hello Everyone,
>
> I have a problem with GET strings. I use
> $_SERVER["REDIRECT_QUERY_STRING"] to get the value-pairs in the URL.
> The problem is that there is a cID variable that keeps appending itself
> to the string continuously everytime someone clicks on a different
> category link on the website. For example, instead of this:
>
>

http://www.someexample.com/admin/ind...omevar2=value2
>
> it keeps appending another cID to it everytime it goes to a different
> link, like this:
>
>

http://www.someexample.com/admin/ind...omevar2=value2
>
> I know that this is happening because I'm appending it with the dot (.)
> but is there a way to just inject a single cID and still have the rest
> of the value-pairs available? Something built into PHP, maybe a different
> predefined variable I don't know about? Or, do I have to make a
> complex function to separate each out and then put it back together
> again like humpty dumpty? Is there an easier way to do this and still
> have a single cID variable in the GET string? Thanks in advance.


Is this what you're doing:

<code>
$cid = getCid(); // However you do set the new, included cid
$newlink = 'http://www.someexample.com/admin/index.html?'.$cid.
$_SERVER["REDIRECT_QUERY_STRING"];
</code>

???

If this is similar to what you're doing, this is a fairly problematic way
for you to insert a replacement property in a query string. An example of a
way to get a new query string:

<code>
function getNewQueryString($arr) {
$store = Array();
foreach ($_GET as $key=>$val) {
foreach ($arr as $k=>$v) {
if (isset($_GET[$k])) {
$store[$key] = $v;
}
}
if (!isset($store[$key])) {
$store[$key] = $val;
}
}
$i = 0;
$str = '?';
$count = count($store);
foreach ($store as $key => $val) {
$amp = $count-1 !== $i ? '&amp;' : '';
$str .= "{$key}={$val}{$amp}";
$i++;
}
return $str;
}
$query = Array('cID'=>42);
$newlink = "http://www.oompaloompa/land.php".getNewQueryString($query);
echo("<p>$newlink</p>");
</code>

What you need to do is transcribe your $_GET string to a new version,
replacing the current values that need replacing while retaining all other
values. To do this, loop through the $_GET global array, replace those that
match $_GET keynames with the new data, and then rebuild the query into a
string for inclusion in the link.

I'll leave it to you figure out how to add new values that are not replaced
($query=Array('cID'=>51,'doesnotexistyet'=>'comple telynewvalue'), for
instance). Also, the above is an example; there are certainly many other
ways to do what is done above (such as replacing the last foreach loop with
an implode() call). There are some strictly unnecessary things done above,
in other words, but I left them in to show what really is happening (and
needs to be done).

--
Jared Farrish
Intermediate Web Developer
Denton, Tx

Abraham Maslow: "If the only tool you have is a hammer, you tend to see
every problem as a nail." $$

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

calculate xml size
Old
  (#2)
Vo, Lance
Guest
 
Posts: n/a
Default calculate xml size - 06-02-2007, 07:55 PM

does php has any function that calculate a xml element in bytes or anything:

example:



<tests>

<test1>ABC</test1>

<test2>DEF</test2>

</tests>



If I want to calculate element <test1>, it should return 18 bytes.

thanks

Lance


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] calculate xml size
Old
  (#3)
Tijnema
Guest
 
Posts: n/a
Default Re: [PHP] calculate xml size - 06-02-2007, 07:55 PM

On 5/26/07, Vo, Lance <EMAIL REMOVED> wrote:
> does php has any function that calculate a xml element in bytes or anything:
>
> example:
>
>
>
> <tests>
>
> <test1>ABC</test1>
>
> <test2>DEF</test2>
>
> </tests>
>
>
>
> If I want to calculate element <test1>, it should return 18 bytes.
>
> thanks
>
> Lance
>


I have nearly no experience with XML parser in PHP, but it doesn't
look like this function exists. However, you could call
xml_get_current_byte_index [1] once, then read the element, and call
it again, and the difference should be what you're looking for.

Tijnema

[1] www.php.net/xml_get_current_byte_index
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Re: $_GET strings seperation
Old
  (#4)
Navid Yar
Guest
 
Posts: n/a
Default Re: [PHP] Re: $_GET strings seperation - 06-02-2007, 07:55 PM

Thanks so much Jarred. It helps me learn more when there's an
explaination on how the code works. I'll play around with it, change
it a bit and give that a try. Take care...

P.S. -- I'm in Arlington, TX



On 5/26/07, Jared Farrish <EMAIL REMOVED> wrote:
> On 5/26/07, Navid Yar <EMAIL REMOVED> wrote:
> > Hello Everyone,
> >
> > I have a problem with GET strings. I use
> > $_SERVER["REDIRECT_QUERY_STRING"] to get the value-pairs in the URL.
> > The problem is that there is a cID variable that keeps appending itself
> > to the string continuously everytime someone clicks on a different
> > category link on the website. For example, instead of this:
> >
> >

> http://www.someexample.com/admin/ind...omevar2=value2
> >
> > it keeps appending another cID to it everytime it goes to a different
> > link, like this:
> >
> >

> http://www.someexample.com/admin/ind...omevar2=value2
> >
> > I know that this is happening because I'm appending it with the dot (.)
> > but is there a way to just inject a single cID and still have the rest
> > of the value-pairs available? Something built into PHP, maybe a different
> > predefined variable I don't know about? Or, do I have to make a
> > complex function to separate each out and then put it back together
> > again like humpty dumpty? Is there an easier way to do this and still
> > have a single cID variable in the GET string? Thanks in advance.

>
> Is this what you're doing:
>
> <code>
> $cid = getCid(); // However you do set the new, included cid
> $newlink = 'http://www.someexample.com/admin/index.html?'.$cid.
> $_SERVER["REDIRECT_QUERY_STRING"];
> </code>
>
> ???
>
> If this is similar to what you're doing, this is a fairly problematic way
> for you to insert a replacement property in a query string. An example of a
> way to get a new query string:
>
> <code>
> function getNewQueryString($arr) {
> $store = Array();
> foreach ($_GET as $key=>$val) {
> foreach ($arr as $k=>$v) {
> if (isset($_GET[$k])) {
> $store[$key] = $v;
> }
> }
> if (!isset($store[$key])) {
> $store[$key] = $val;
> }
> }
> $i = 0;
> $str = '?';
> $count = count($store);
> foreach ($store as $key => $val) {
> $amp = $count-1 !== $i ? '&amp;' : '';
> $str .= "{$key}={$val}{$amp}";
> $i++;
> }
> return $str;
> }
> $query = Array('cID'=>42);
> $newlink = "http://www.oompaloompa/land.php".getNewQueryString($query);
> echo("<p>$newlink</p>");
> </code>
>
> What you need to do is transcribe your $_GET string to a new version,
> replacing the current values that need replacing while retaining all other
> values. To do this, loop through the $_GET global array, replace those that
> match $_GET keynames with the new data, and then rebuild the query into a
> string for inclusion in the link.
>
> I'll leave it to you figure out how to add new values that are not replaced
> ($query=Array('cID'=>51,'doesnotexistyet'=>'comple telynewvalue'), for
> instance). Also, the above is an example; there are certainly many other
> ways to do what is done above (such as replacing the last foreach loop with
> an implode() call). There are some strictly unnecessary things done above,
> in other words, but I left them in to show what really is happening (and
> needs to be done).
>
> --
> Jared Farrish
> Intermediate Web Developer
> Denton, Tx
>
> Abraham Maslow: "If the only tool you have is a hammer, you tend to see
> every problem as a nail." $$
>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: $_GET strings seperation
Old
  (#5)
Guest
Guest
 
Posts: n/a
Default Re: $_GET strings seperation - 06-02-2007, 07:55 PM

On May 26, 5:39 pm, n...@xenonsolutions.com ("Navid Yar") wrote:
> Thanks so much Jarred. It helps me learn more when there's an
> explaination on how the code works. I'll play around with it, change
> it a bit and give that a try. Take care...
>
> P.S. -- I'm in Arlington, TX


Incidently, ponder this:

<code>
function shortGetNewQueryString($arr,$merge) {
return array_merge($arr,$merge);
}
echo('<pre>');

// Let's do one new cID, new GET key/value
$query = Array('cID'=>42,'freudian'=>'slip');
$go = shortGetNewQueryString($_GET,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>9-002,'footloose'=>'fancy free');
$go = shortGetNewQueryString($go,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>493,'fugged'=>'dhaboutit');
$go = shortGetNewQueryString($go,$query);
print_r($go);

// Let's do one new cID, new GET key/value
$query = Array('cID'=>A4,'longlongtimeago'=>'in a galaxy far, far
away');
$go = shortGetNewQueryString($go,$query);
print_r($go);

echo('</pre>');
</code>

By the way, when you run that code, pay special attention to the
second test. Very very tricky entry anomaly... Wuffuh!

Pay attention to how short that new code is
(shortGetNewQueryString()). It's certainly arguable you don't even
need to wrap it in a function. Consider:

<code>
function mediumGetNewQueryString($arr,$add) {
foreach ($add as $key=>$val) {
$arr[$key] = $val;
}
return $arr;
}
echo('<pre>');
print_r(mediumGetNewQueryString($_GET,$query));
echo('<pre>');
</code>

And then, of course, a number of shortcuts may be used to obscurfy and
mystify your code for later puzzling, head-scratchedness.

This is, of course, exactly comparable to all the other example
methods:

<code>
function annoyingGetNewQueryString($arr,$add) {
foreach ($add as $key=>$val) $arr[$key] = $val;
return $arr;
}
echo('<pre>');
print_r(annoyingGetNewQueryString($_GET, $query));
echo('</pre>');
</code>

Caution: Using array_merge, though, will overwrite keynames, but NOT
numerical items. Consider:

<code>
$array = Array(
[0] => 'moe'
[1] => 'curly',
[2] => 'larry'
);
// Is equivalent to ~
$array = Array();
$array[] 'moe';
$array[] 'curly';
$array[] 'larry';
// Is equivalent to ~
$array = Array();
array_push($array, 'moe');
array_push($array, 'curly');
array_push($array, 'larry');
</code>

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top