CodeIgniter Forums
changing / rewriting the URI - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: changing / rewriting the URI (/showthread.php?tid=11574)



changing / rewriting the URI - El Forum - 09-14-2008

[eluser]Unknown[/eluser]
so I have my uri that brings up search results

http://www.mysite.com/info/listing

and it works fine now problems, the problem I am having is that I want the URI to be different ... I want to change / rewrite it to be more like

http://www.mystie.com/info/listing/searchTerm1/searchTerm2/searchTerm3

Problem is, doing a redirect I loose my $_POST data, trying to append those to the view file name obviously doesn't work either... any suggestions on how to achieve this?


changing / rewriting the URI - El Forum - 09-14-2008

[eluser]Rick Jolly[/eluser]
Something like this?
Code:
function listing($searchTerm1 = false, $searchTerm2 = false, $searchTerm3 = false)
{
   if (! empty($_POST))
   {
      $terms = '';

      if (! empty($_POST['searchTerm1']))
      {
         $terms .= '/' . urlencode($_POST['searchTerm1']);
      }
      if (! empty($_POST['searchTerm2']))
      {
         $terms .= '/' . urlencode($_POST['searchTerm2']);
      }
      if (! empty($_POST['searchTerm3']))
      {
         $terms .= '/' . urlencode($_POST['searchTerm3']);
      }
  
      $this->load->helper('url');
      redirect('info/listing' . $terms);
   }
   else
   {
      $searchTerm1 = (! empty($searchTerm1)) ? urldecode($searchTerm1) : false;
      $searchTerm2 = (! empty($searchTerm2)) ? urldecode($searchTerm2) : false;
      $searchTerm3 = (! empty($searchTerm3)) ? urldecode($searchTerm3) : false;

      // continue...
   }
}



changing / rewriting the URI - El Forum - 09-14-2008

[eluser]Mirage[/eluser]
What I do is to stick the cleaned Post data into a session variable, and read it out in the redirected method and use the url parameters to communicate paging

Code:
function listing($page=0) {
    if($_SERVER['REQUEST_METHOD']=='POST') {
        // sanitize post data
        $listingParams = $this->sanitize_post_data();
        
        if ($listingParams) {
            $this->session->set_userdata('listingparams', listingParams);
            header("Location: {$_SERVER['PHP_SELF']}";
            exit();
        }
    }

    // handle GET requests
    $listingParams= $this->session->userdata('listingparams');

    // redirect to the form if nothing was stored

    // else do the search

    // show the results view
    
}

HTH,
-m