How do get variables from current URL? [Help!]

I’m trying to make a link that exports an SQL query as an excel spreadsheet. It’s part of a project where first, the user can enter multiple criteria into a search and get a table of all the results. The search form uses the $_GET function, and puts all the different things that the user searched for into the URL. From the new page where they can see the table of results, I’d like to be able to download that table, having the the code pull the variables from the current URL which includes all of their search criteria. I know it sounds kinda confusing, so I’ll put some example code to better illustrate what I’m talking about.

the url after the search has queried the SQL looks like: http://example.com/search.php?box1=billy&box2=george

the code from the example download page is:
[php]

<?php if (isset($_GET['box1'])&&isset($_GET['box2'])) { $box1 = trim($_GET['box1']); $box2 = trim($_GET['box2']); $query = "SELECT * FROM example_table WHERE box1 LIKE '%".$box1."%' AND box2 LIKE '%".$box2."%'"; //the rest isn't really important to my question, but here for example purposes. //the rest is just getting the file ready to export etc. function cleanData(&$str) { $str = preg_replace("/\t/", "\\t", $str); $str = preg_replace("/\r?\n/", "\\n", $str); if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; } $filename = "product_list_" . date('Ymd') . ".xls"; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: application/vnd.ms-excel"); $flag = false; $result = odbc_exec($conn, $query); while(false !== ($row = oddc_fetch_array($result))) { if(!$flag) { // display field/column names as first row echo implode("\t", array_keys($row)) . "\r\n"; $flag = true; } array_walk($row, 'cleanData'); echo implode("\t", array_values($row)) . "\r\n"; } } ?>

[/php]

So yea, just to reiterate, my question is, Is there someway to pull variable information from the current URL? The only other option I see is having a second button on the Search form that would link to the download page and therefore receive the $_GET, but out of convenience, there must be a way to pull current URL data.

Please help, it would be greatly appreciated. Thank you.

Umm, hope this helps.

You could make the download link dynamic with $_GET
[php]echo (‘Download Table’);[/php]

Thanks a ton lothop! I never thought about that. However, instead of stating $_GET[‘box1’] for every variable, I just did $box1, seeing as that variable contains the get function. This was perfect though, its just what I needed, thank you again 8)

No problem. Yeah, its a lot tidier, and of course has been trimmed :slight_smile:
Just wanted you to get where the variable was coming from

Sponsor our Newsletter | Privacy Policy | Terms of Service