dmccabe Posted February 9, 2009 Share Posted February 9, 2009 I have built a database, which allows the user to run a query on 1 field or on a combination of fields. I want to make it so they can run the query as they need it, it displays the info on the page (up to here is already working), but then there will be a button to export the results to Excel. I already have a script to export to excel, but I need to pass the query string to the script, but how can I capture the last query string and then pass it to this sheet? Obviously I can use sessions, but I am not sure how best to capture the query string. Link to comment https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/ Share on other sites More sharing options...
phpdragon Posted February 9, 2009 Share Posted February 9, 2009 This will add the returned vars to the session <?php session_start(); $sql = "SELECT * FROM table ORDER BY id DESC LIMIT 1"; $result = mysql_query($sql); if ($result_row = mysql_fetch_array($result)) { $_SESSION['name'] = $result_row["name"]; $_SESSION['data'] = $result_row["data"]; $_SESSION['userID'] = $result_row["userID"]; } header('Location: next_file.php'); ?> Hope that helps Link to comment https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/#findComment-758189 Share on other sites More sharing options...
dmccabe Posted February 9, 2009 Author Share Posted February 9, 2009 Thanks for the help, but I dont fully understand what you mean? I have to specify the fields each time? I want it to remember the query as it was run last, so then it can pass that query to the excel export script. Excel Export Script: <?php header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=branchlist.xls"); header("Pragma: no-cache"); header("Expires: 0"); include($_SERVER['DOCUMENT_ROOT'] . 'connect2.php'); //mysql_connect(localhost,$username,$password); //@mysql_select_db($database) or die("Unable to select database"); $select = "SELECT `name`, `code`, `address`, `post_code`, `front_line`, `back_line`, `fax`, `mobile` FROM tbl_branches WHERE `live` = '1' ORDER BY `name` ASC"; $export = mysql_query($select); $count = mysql_num_fields($export); for ($i = 0; $i < $count; $i++) { $header .= mysql_field_name($export, $i)."\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r", "", $data); if ($data == "") { $data = "\n(0) Records Found!\n"; } print "$header\n$data"; ?> so what I would like to do is pass the query in where it currently has: $select = "SELECT `name`, `code`, `address`, `post_code`, `front_line`, `back_line`, `fax`, `mobile` FROM tbl_branches WHERE `live` = '1' ORDER BY `name` ASC"; so instead it says something like: $select = $_SESSION['LAST_QUERY']; Link to comment https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/#findComment-758245 Share on other sites More sharing options...
dmccabe Posted February 11, 2009 Author Share Posted February 11, 2009 Does anyone one know if this is possible? Link to comment https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/#findComment-759535 Share on other sites More sharing options...
phpdragon Posted February 22, 2009 Share Posted February 22, 2009 why dont you store that query in the database for each user, so then it will remember their last option by calling that from the user table, if they want to update their query it could just update the query stored in the database. Link to comment https://forums.phpfreaks.com/topic/144488-storing-last-query-in-a-session/#findComment-768245 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.