CyberShot Posted January 11, 2018 Share Posted January 11, 2018 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? Quote Link to comment https://forums.phpfreaks.com/topic/306136-passing-an-array-though-anchor-link/ Share on other sites More sharing options...
Solution kicken Posted January 11, 2018 Solution Share Posted January 11, 2018 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/306136-passing-an-array-though-anchor-link/#findComment-1555212 Share on other sites More sharing options...
CyberShot Posted January 12, 2018 Author Share Posted January 12, 2018 (edited) 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 January 12, 2018 by CyberShot Quote Link to comment https://forums.phpfreaks.com/topic/306136-passing-an-array-though-anchor-link/#findComment-1555229 Share on other sites More sharing options...
CyberShot Posted January 12, 2018 Author Share Posted January 12, 2018 (edited) 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 January 12, 2018 by CyberShot Quote Link to comment https://forums.phpfreaks.com/topic/306136-passing-an-array-though-anchor-link/#findComment-1555230 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.