Welcome Guest, Not a member yet? Register   Sign In
Routing problem
#1

[eluser]calumogg[/eluser]
Hi I have been working on a routing script that will check if a username exists in a database if it does the script will redirect to the users profile or requested page the url will look like wwww.domain.com/username/page

I am having a few problems with passing on other data after page, I could set it up manually using lots of if statements but I would rather have a automatic way of passing on as many other elements as I like. Anyway here is the code (the part I am working on starts near the bottom line 102):

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* Router Class
*
* Extends CI Router
*
* @author     Original by EllisLab - extension by CleverIgniters
* @see        http://codeigniter.com
*/

class MY_Router extends CI_Router {
    
    /**
     * Constructor
     *
     * @access    public
     */
    function MY_Router()
    {
        parent::CI_Router();
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Validate Routing Request
     *
     * @access    public
     */
    function _validate_request($segments)
    {
    
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }
        
        // Connect to server and select database.
      mysql_connect("******", "*****", "*****")or die("cannot connect");
      mysql_select_db("*****")or die("cannot select DB");
        
      // Change select username from database
      $query = "SELECT `username` FROM `members` WHERE `username` = '$segments[0]'";
      $result = mysql_query($query);

      // Count the number of results
      $count = mysql_num_rows($result);
      
      //Close mysql connection
      mysql_close();

      // If number of results = 0 the username doesnt exist
      if($count==0){
      
        // Redirect to nouser
        if(isset($segments[0]) && sizeof($segments) == 1) {
          
          return array('members', 'nouser', $segments[0]);
        
        }
      
      //If the username is taken redirect to the requested page    
      } else {
      
        // Check the username segment is set
        if(isset($segments[0])) {
        
          //If there are 2 segments redirect to the chosen page
          if(sizeof($segments) == 2){
          
            return array('members', $segments[1], $segments[0]);
        
          //If there is only one segment redirect to the user index page
          } else {
        
            return array('members', 'index', $segments[0]);
          
        }
      }
    }
  }
}
?>

You can see I have set it up so if there is more than one segment it will put the username first then the page second, but I cant figure out how to make it automatically add any more segments after the first 2 have been put in place. Any help would be much appreciated. Also my thanks to the authors of this script, it saved me lots of time!!
#2

[eluser]jtkendall[/eluser]
Hi calumogg,

If you replace this if statement:
Code:
if(sizeof($segments) == 2){
    return array('members', $segments[1], $segments[0]);
    //If there is only one segment redirect to the user index page
} else {

with:

Code:
if(sizeof($segments) >= 2){
    $return = array('member', $segments[1]);
    foreach($segments as $key=>$value){
        if($key != 1){
            $return[] = $value;
        }
    }
    return $return;
    //If there is only one segment redirect to the user index page
} else {

I believe that will do what you're looking for
#3

[eluser]calumogg[/eluser]
Thanks for the reply, I have tried it and could not get it working. I have changed the code a bit so here is what I am using now:

Code:
if(sizeof($segments) > 1){
          
$username = $segments[0];
$page = $segments[1];
          
unset($segments[0]);
unset($segments[1]);

return array('members', $page, $username, $segments);

I have extracted the username and page and unset the values. The problem now is when I try and echo $segments in the view it just says 'Array' even if there is no more data being sent. Thanks again for the help
#4

[eluser]Tottys Design[/eluser]
instead of using this:
Code:
// Connect to server and select database.
      mysql_connect("******", "*****", "*****")or die("cannot connect");
      mysql_select_db("*****")or die("cannot select DB");
        
      // Change select username from database
      $query = "SELECT `username` FROM `members` WHERE `username` = '$segments[0]'";
      $result = mysql_query($query);

      // Count the number of results
      $count = mysql_num_rows($result);
      
      //Close mysql connection
      mysql_close();

how can you use models? Sad

(I need help)




Theme © iAndrew 2016 - Forum software by © MyBB