Jump to content

passing an array though anchor link


Go to solution Solved by kicken,

Recommended Posts

I am passing an array of wordpress page IDs through an anchor link but it looks rather ugly. I am serializing my array like this

$var = htmlspecialchars(serialize($pages), ENT_QUOTES);

which gives me this

string(45) "a:4:{i:0;i:944;i:1;i:17;i:2;i:19;i:3;i:1310;}" 

passing it through an anchor tag like so

$link = tbpb_get_archives_link(); // returns the permalink of a WordPress page
<a href="<?php echo $link . '?var='. $var ?>" class="work-all"><span><i class="fa fa-times"></i> All works</span></a>

now my anchor tag looks like this in the navigation bar

http://mysite.com/all-projects/?var=a:4:{i:0;i:944;i:1;i:17;i:2;i:19;i:3;i:1310;}

is there a way to clean that up and make it look prettier? or another method of passing the variables from one page to another?

Link to comment
https://forums.phpfreaks.com/topic/306136-passing-an-array-though-anchor-link/
Share on other sites

  • Solution

I'd just implode the array with a separator, such as a comma or dash or whatever else. Then explode it on the next page to get your array back.

 

You could also form a URL with multiple parameters using PHP's parameter array syntax so your URL would look like:

http://mysite.com/all-projects/?var[]=944&var[]=17&var[]=19&var[]=1310
There is a function for this built into PHP.
  • Like 1

My only thing with this is that I don't see this kind of thing often. I would think there would be a better way. I think your idea has promise and I might actually look into that. I did try the http_build_query but I don't think it ended up working. This is the first time I have tried passing variables through a link

Edited by CyberShot

I'd just implode the array with a separator, such as a comma or dash or whatever else. Then explode it on the next page to get your array back.

 

You could also form a URL with multiple parameters using PHP's parameter array syntax so your URL would look like:

http://mysite.com/all-projects/?var[]=944&var[]=17&var[]=19&var[]=1310
There is a function for this built into PHP.

 

 

I re-wrote it with the implode, explode. It's shorter and cleaner. It also makes the address at least look like something WordPress would do. I had hoped to find a way to hide the variable all together but I can accept this method.

Edited by CyberShot
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.