Welcome Guest, Not a member yet? Register   Sign In
Creating a simple view help?
#1

[eluser]Haley[/eluser]
Well I'm new to CI. I'm trying to figure out how to create a simple view. It's sooner or later going to turn into a login/registration system so I might need help with that later on.

But I have the model, view, and controller all setup.

Here is my controller located at applications/controllers:
Code:
<?php

class User extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->model('User_model','User');
}
public function register() {
  $data['title'] = 'Signup';

  $this->load->view('templates/header');
  $this->load->view('forms/register',$data);
  $this->load->view('templates/footer');
}
}
?>

Then here is my model located at applications/models:
Code:
<?php

class User_model extends CI_Model {
public function __construct() {
}
public function add_user() {
  $data=array(
   'username' => $this->input->post('usr_username'))
  );
  $this->db->insert('user_usr',$data);
}
}
?>

Then last but not least, my view located at applications/views/forms/:
Code:
<div class="generalForm">
<div class="row">
  <h1>Registration</h1>
</div>
<div class="row">
  <div class="col">
   <b>Name:</b>
  </div>
  <div class="col">
   &lt;input type="text" name="usr_name" size="30" value="&lt;?php echo $_POST['usr_name']; ?&gt;" /&gt;
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Username:</b>
  </div>
  <div class="col">
   &lt;input type="text" name="usr_username" size="30" value="&lt;?php echo $_POST['usr_username']; ?&gt;" /&gt;
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Password:</b>
  </div>
  <div class="col">
   &lt;input type="text" name="usr_password" size="30" value="&lt;?php echo $_POST['usr_password']; ?&gt;" /&gt;
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Confirm Password:</b>
  </div>
  <div class="col">
   &lt;input type="text" name="usr_password2" size="30" value="&lt;?php echo $_POST['usr_password2']; ?&gt;" /&gt;
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Email Address:</b>
  </div>
  <div class="col">
   &lt;input type="text" name="usr_email" size="30" value="&lt;?php echo $_POST['usr_email']; ?&gt;" /&gt;
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Gender:</b>
  </div>
  <div class="col">
   &lt;input type="radio" name="gender" value="1" /&gt; Male
   &lt;input type="radio" name="gender" value="2" /&gt; Female
  </div>
</div>
<div class="row">
  <div class="col">
   <b>Date Of Birth:</b>
  </div>
  <div class="col">
   <select name="month">
    <option value="">Month</option>
    &lt;?php
    for($i = 1; $i <= 12; $i++)
    {
     $selected = ($i==$dobm) ? ' selected="selected"' : '';
     echo "<option value=\"".$i."\"".$selected."\">".date('M', mktime(0,0,0,$i+1,0,0))."</option>\n";
    }
    ?&gt;
   </select>
   <select name="day">
    <option value="">Day</option>
    &lt;?php
    for($i = 1; $i <= 31; $i++)
    {
     $selected = ($i==$dobd) ? ' selected="selected"' : '';
     echo "<option value=\"".$i."\"".$selected."\">".date('d', mktime(0,0,0,0,$i,0))."</option>\n";
    }
    ?&gt;
   </select>
   <select name="year">
    <option value="">Year</option>
    &lt;?php
    for($i = 1998; $i >= 1911; --$i)
    {
     $selected = ($i==$doby) ? ' selected="selected"' : '';
     echo "<option value=\"".$i."\"".$selected."\">".$i."</option>\n";
    }
    ?&gt;
   </select>
  </div>
</div>
</div>

I want the ending URL to be http://www.mysitename.com/register

I feel like I have everything setup correctly. I've been looking at other MVC projects and I really don't see a problem, but I am new so I might not be able to catch these things as easily.
#2

[eluser]Jason Stanley[/eluser]
This looks like what you are after.
http://ellislab.com/codeigniter/user-gui...uting.html

You want to make site.com/user/register appear at site.com/register right?
#3

[eluser]Haley[/eluser]
Ok so in my routes, I have:
Code:
$route['forms/(:any)'] = 'user/view/$1';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

No the URL is site.com/forms/register, but I want it to appear like site.com/register

I changed the controller to this:
Code:
&lt;?php

class User extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->model('User_model','User');
}
public function view($page = 'home')
{  
  if ( ! file_exists('application/views/forms/'.$page.'.php'))
  {
   // Whoops, we don't have a page for that!
   show_404();
  }
  
  $data['title'] = ucfirst($page); // Capitalize the first letter
  
  $this->load->view('templates/header', $data);
  $this->load->view('forms/'.$page, $data);
  $this->load->view('templates/footer', $data);

}
}
?&gt;

Still won't work.
#4

[eluser]Jason Stanley[/eluser]
What doesn't work exactly? You get a 404 message?

If it is 404 use the APPPATH constant your file_exists call.

Code:
if ( ! file_exists(APPPATH.'/views/forms/'.$page.'.php'))

I don't know offhand is APPPATH has a trailing slash. You would need to check.
#5

[eluser]Haley[/eluser]
Ok still getting the 404 page.
Here is my updated controller which is located at application/controllers/user.php:
Code:
&lt;?php

class User extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->model('User_model','User');
}
public function view($page = 'register')
{  
  if ( ! file_exists(APPPATH.'/views/user/'.$page.'.php'))
  {
   // Whoops, we don't have a page for that!
   show_404();
  }
  
  $data['title'] = ucfirst($page); // Capitalize the first letter
  
  $this->load->view('templates/header', $data);
  $this->load->view('user/'.$page, $data);
  $this->load->view('templates/footer', $data);

}
}
?&gt;

My updates routes.php:
Code:
$route['user/(:any)'] = 'user/view/$1';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

My view is located at application/views/user/register.php
#6

[eluser]Jason Stanley[/eluser]
I assume you have printed this path to confirm it is correct?

Code:
echo APPPATH.'/views/user/'.$page.'.php';

I don't see anything that is really wrong with the code you have provided.
#7

[eluser]Haley[/eluser]
It won't echo because all I'm getting is a 404 error.
I completely changed my controller and routes now so they match more efficiently.

Controller:
Code:
&lt;?php

class User extends CI_Controller {
public function __construct() {
  parent::__construct();
  $this->load->model('user_model');
}
public function register()
{  
  $data['title'] = 'Register'; // Capitalize the first letter

  $this->load->view('templates/header', $data);
  $this->load->view('user/register', $data);
  $this->load->view('templates/footer', $data);

}
}
?&gt;

Here is the routes:
Code:
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['user/register'] = 'user/register';
$route['user'] = 'user';
#8

[eluser]InsiteFX[/eluser]
You routes are wrong!
Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.
Code:
$route['user/(:any)'] = 'user/view/$1';
$route['user'] = 'user';
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

So you need to figure out the order of how your routes will be called...
#9

[eluser]Haley[/eluser]
What do you mean? I don't understand higher routes..
I put the default controller on top and nothing changed.. I don't fully understand routes. I took a look at the URL provided my someone earlier and it's hard for me to comprehend being so new to the MVC world.




Theme © iAndrew 2016 - Forum software by © MyBB