joecooper Posted February 17, 2012 Share Posted February 17, 2012 Hi, Here is a part of my code to generate a URL to output to some table headers. $URL = "order_sheet.php?sortby=orders_id&fromdate=" . $fromdate . "&todate=" . $todate . "&sortorder=" . $newsortorder; On each heading, I need to do something like this: $URL['orders_id'], $URL['customer_name'], etc to decide on which "sortby" to use. I know I could make multiple varibles for each headding, but thats the lazy way and I need to learn! Thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 17, 2012 Share Posted February 17, 2012 There will be no $URL['orders_id'], there will be a variable as $_GET['sortby'] with the value associated. So, you simply need to use the value of $_GET['sortby'] to determine what field to sort on. Of course you want to sanitize and validate the value before using it in a query Sample code //Create list of approved field the query can be ordered by $approvedSortField = array('orders_id', 'from_date', 'todate'); //Create ORDER BY caluse for query if(in_array($_GET['orders_id'], $approvedSortField)) { $orderByClause = " ORDER BY {$_GET['orders_id']} "; } else { $orderByClause = ''; } //Add order by clause to query $query = "SELECT * FROM table_name {$orderByClause}"; Quote Link to comment Share on other sites More sharing options...
joecooper Posted February 17, 2012 Author Share Posted February 17, 2012 Ah of course! dont know why i missed that! Not enough coffee I suppose! Thanks! Quote Link to comment Share on other sites More sharing options...
joecooper Posted February 17, 2012 Author Share Posted February 17, 2012 Ah actually, The $_GET['order_by'] is set from the links in the headers! This is where i need to set the row names in the links. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 17, 2012 Share Posted February 17, 2012 Ah actually, The $_GET['order_by'] is set from the links in the headers! This is where i need to set the row names in the links. I'm not sure I understand what you are talking about. You really need to define your requests with a little more clarity. If you are talking about the process of creating the links that is pretty simple. Just set the value in the URL for each link you create. Since you provided no code I can't give you anything specific. But, I would create a function. Also, I see you have two similar values in the URL "sortorder" and 'sortby'. But, the 'sortby' value is apparently an order ID, so I would call that something such as 'filterby' Here is an example function searchURL($pageName, $sortBy, $fromDate, $toDate, $sortOrder) { $url = "{$pageName}?sortby={$sortBy}&fromdate={$fromDate}&todate={$toDate}&sortorder={$sortOrder}"; return $url; } Quote Link to comment Share on other sites More sharing options...
dazman Posted February 18, 2012 Share Posted February 18, 2012 in addition, if you are outputting the URL in a hyperlink on a web page, you should really use & instead of &. Quote Link to comment 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.