Jump to content

What function am I looking for?


joecooper

Recommended Posts

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

Link to comment
Share on other sites

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}";

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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.