Solved

Query String and Array

Posted on 2002-07-23
15
442 Views
Last Modified: 2008-02-01
Is it possible to send an array as a query string (serialize, then urlencode), and also attach an element to it while actually sending it as an <a href> tag?
0
Comment
Question by:junkyboy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 6
15 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174045
<?php

foreach($HTTP_POST_VARS as $k => $v)
     {
     echo "$k = $v<br>";
     }
     
$s = serialize($HTTP_POST_VARS);

echo "Serialized = $s<br>";

$e = urlencode($s);

echo "URLEncoded = $e<br>";

echo "<br><br><a href=\"SomePlace.php?$e\">Someplace to decode this stuff</a><br><br>";

$d = urldecode($e);

echo "$d<br>";

$a = unserialize($d);

foreach($a as $k => $v)
     {
     echo "$k = $v<br>";
     }
     
?>

0
 

Author Comment

by:junkyboy
ID: 7174662
Maybe my wording wasn't quite good.  What I have is this:
<?
$param = array( USER=>"a1", PASS=>"a2", IP=>"a3", );
?>
<a href="test.php?<?echo "param=".urlencode(serialize($param))."&param[PAGE]=".urlencode(serialize("2"));?>">test</a>
?>

Whenever I do this, the $param array gets overwritten, and it only contains the PAGE key in it.  The USER, PASS, and IP keys are destroyed.  Is there anyway I can add it while I pass the <a> tag?  What I ultimately want to do is do:

next page:     ..."&param[PAGE]=".urlencode(serialize($param[PAGE]+1))...
previous page: ..."&param[PAGE]=".urlencode(serialize($param[PAGE]-1))...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174725
Assuming you have the current page number,

$iNextPage = $iPage++;

and

$iPrevPage = $iPage--;

So you can ...

<a href="test.php?<?echo "param=".urlencode(serialize($param))."&Page=".urlencode(serialize($iNextPage));?>">test</a>

sort of thing.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174727
Sorry.

<a href="test.php?<?echo "param=".urlencode(serialize($param))."&iPage=".urlencode(serialize($iNextPage));?>">test</a>

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174737
I would suggest using sessions for the username work and NOT putting username/passwords in the URL. These are EASY to spot as the urlencode will become plain text in IE6 on the status bar and in some cache readers.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174742
<?
$param = array( USER=>"a1", PASS=>"a2", IP=>"a3", );
$param["PAGE"] = 3;
$next = urlencode(serialize($param));
$param["PAGE"] = 1;
$prev = urlencode(serialize($param));

echo <<<END_HTML
<a href="test.php?param=$prev">prev</a>
<a href="test.php?param=$next">next</a>
END_HTML;

?>

May be better to your needs!

Richard.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7174847
Slight update ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );
$param["PAGE"] = 3;
$next = urlencode(serialize($param));
$param["PAGE"] = 1;
$prev = urlencode(serialize($param));

echo <<<END_HTML
<a href="test.php?param=$prev">prev - $prev</a>
<a href="test.php?param=$next">next - $next</a>
END_HTML;

echo "When I put the mouse over these links, the status bar shows ...<br>" . urldecode($next) . "<br>and<br>" . urldecode($prev) ."<br>";
?>

I was getting errors on USER rather than "USER".

Also added a note about the mouseover.

Richard.
0
 

Author Comment

by:junkyboy
ID: 7176081
So, there's no direct way of doing it within an <A> tag?
0
 

Author Comment

by:junkyboy
ID: 7176144
So, there's no direct way of doing it within an <A> tag?
0
 

Author Comment

by:junkyboy
ID: 7176592
So, there's no direct way of doing it within an <A> tag?
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7176618
You can simply break in and out of PHP ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );
?>
<a href="test.php?param=<?php $param["PAGE"] = 1; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"] = 3; echo urlencode(serialize($param)); ?>">Next</a>

So yes, but it is a little harder to read.

0
 

Author Comment

by:junkyboy
ID: 7184412
That's going to make PAGE = 3 because it goes through the whole script..
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 7184739
You would have a local page variable surely?

Let's call it $CurrentPage.

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

?>
<a href="test.php?param=<?php $param["PAGE"] = $CurrentPage - 1; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"] = $CurrentPage + 1; echo urlencode(serialize($param)); ?>">Next</a>

?>

But remember, HTML and PHP are read at different times.

The PHP happens on the server and can produce HTML.

The HTML (entered in the file or created by PHP) is read by the browser.

Doing things inline like this are OK, but they DO make it a little harder to read.

You could (and probably should) actually use the Page parameter (me being stupido!) ...

<?php
$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

?>
<a href="test.php?param=<?php $param["PAGE"]--; echo urlencode(serialize($param)); ?>">Previous</a>
<a href="test.php?param=<?php $param["PAGE"]++; echo urlencode(serialize($param)); ?>">Next</a>

?>

But you have to watch out for being on Page 1 and having a link to Page 0.

I would recommend ...

<?php

$param = array( "USER"=>"a1", "PASS"=>"a2", "IP"=>"a3", );

if ($param["Page"] > 1)
 {
 $param["PAGE"]--;
 echo '<a href="test.php?param=' . urlencode(serialize($param)) .'">Previous</a>';
 $param["PAGE"]++;
 }

if ($param["Page"] < $MaxPages)
 {
 $param["PAGE"]++;
 echo '<a href="test.php?param=' . urlencode(serialize($param)) .'">Next</a>';
 $param["PAGE"]--;
 }

?>

Richard.
0
 
LVL 40

Accepted Solution

by:
Richard Quadling earned 150 total points
ID: 7189071
If you already have a current page variable, then ...

$param["Page"] = $currentPage + 1;

and

$param["Page"] = $currentPage - 1;

would be better.

Richard.

P.S. I put "PAGE" instead of "Page" in my previous reply. Oops!
0
 

Author Comment

by:junkyboy
ID: 7283308
Sorry for the delay - was out of town a bit.  I don't know why I didn't think of something like that...  Thanks though.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question