Welcome Guest, Not a member yet? Register   Sign In
I'm Exhausted - Give me a hand here!
#1

[eluser]developer10[/eluser]
Well, although i knew moving from native PHP to CodeIgniter wasn't going to be an easy mission, i didn't expect not to be able to manage somewhat basic things.

long story short, im rebuilding my site (native PHP) in CI. the site URLs are based on query strings. basically, these are examples of links and the same i want to achieve in CI:

the site is BUSINESS DIRECTORY - filtering is possible by area of business (category), company city (city), company first letter (letter) and also, there's pagintion parameter (page)

mysite.com/show.php?section=bizdirectory // all companies are listed

mysite.com/show.php?section=bizdirectory&category=Banks // only banks are listed

mysite.com/show.php?section=bizdirectory&category=Banks&letter=P // only banks beginning with "P" are listed

mysite.com/show.php?section=bizdirectory&&category;=Banks&letter=P&page=2 // if there are many results, "page" gets involved


The same goes for involving "city" parameter, i'll show some examples to get an idea


mysite.com/show.php?section=bizdirectory&category=Banks&city=Sarajevo // only banks from Sarajevo are listed

mysite.com/show.php?section=bizdirectory&category;=&city=Sarajevo // all companies from Sarajevo are listed (note empty value for the category parameter)

etc, etc (adding "letter" and page parameters in any possible way) The main thing is that parameters go in this order: category, city, letter, page, and if needed, parameters (not only values) from the end can be omitted



Basically, this is what i want to achieve in CI. So far, i managed to do this:

mysite.com/index.php/bizdirectory EQUALS TO mysite.com/show.php?section=bizdirectory // all companies listed
( USING ONE FUNCTION within bizdirectory_model )

AND

( USING another FUNCTION within bizdirectory_model )
mysite.com/index.php/category/Banks (using routing) EQUALS TO mysite.com/show.php?section=bizdirectory&category=Banks // only banks listed


Since i suppose there's an easy way to achieve everything i need/want in CI, please be so kind and give me some suggestions


Thanks!
#2

[eluser]taewoo[/eluser]
If I understand you correctly, i think you're tryin to do this in a controller:

Code:
function show($category, $city, $letter, $page)
{
  ....
}
#3

[eluser]developer10[/eluser]
[quote author="taewoo" date="1215658046"]If I understand you correctly, i think you're tryin to do this in a controller:

Code:
function show($category, $city, $letter, $page)
{
  ....
}
[/quote]

yes, i think that would be the way. i found some stuff about those parameters that get passed to a function but i dont fully understand it.

consider this function for retrieving ALL companies from database:

Code:
function all_companies() {
        $result = array();
        $this->db->select()->from('tbl_companies')->orderby('c_name', 'asc');
        $query = $this->db->get();
        
        if($query->num_rows() > 0) {
            $result = $query->result();
        }
          return $result;
    }

how do i incorporate those parameters into my query (with the option of using only some of them) and what URL/URI structure can you suggest?
#4

[eluser]taewoo[/eluser]
I suggest you watch the codeigniter tutorials... these are really really basic questions.
You need to pass the variables from your controller to your model. Just like you would in regular, non-MVC php app.

Code:
$this->load->model('your_model');
$result = $this->your_model->get($category, $city, $letter, $page);
#5

[eluser]developer10[/eluser]
[quote author="taewoo" date="1215658928"]I suggest you watch the codeigniter tutorials... these are really really basic questions.
You need to pass the variables from your controller to your model. Just like you would in regular, non-MVC php app.

Code:
$this->load->model('your_model');
$result = $this->your_model->get($category, $city, $letter, $page);
[/quote]

you're right, im on it

i thought models can only pass, not be passed anything. i imagined models being on one side, views on the other and controllers being as links and providers between them.. didn't know data can be passed in the opposite direction

well, back to basics
#6

[eluser]Colin Williams[/eluser]
Here's a very basic example of MVC interaction that I posted for doug ealier:

Here's the run down. The Blog Controller is called because the user enters our blog URL. We load the blog model to handle retrieving our data. We use the model to retrieve the data and pass it into the view. Our view loops through the result from the model and displays it in a manner we want.

Excuse the brevity and shit HTML Tongue Also, if this is all our Blog controller did, ever, a model might be slight overkill (a framework might even be overkill for this!). But it is highly likely your App and its interaction with your database will be much more complex.

Controller in controllers/blog.php

Code:
class Blog extends Controller {
  
   function Blog()
   {
      parent::Controller();

      // Load the model and assign to $this->blog
      $this->load->model('Blog_model', 'blog');
   }

   function index()
   {
      // Get 5 blog posts from our model
      $data['posts'] = $this->blog->get_latest(5);
      // Pass result to view for display
      $this->load->view('blog/front', $data);
   }
}

Model in models/blog_model.php

Code:
class Blog_model extends Model {
  
   function Blog_model()
   {
      parent::Model();
   }

   function get_latest($limit = 1)
   {
      $result = array(); // We should always return an array so our views don't choke in a foreach loop
      // Prepare the query
      $this->db->limit($limit);
      $this->db->orderby('created', 'desc'); // Get most recent first
      // Run the query and update $result if a result is found
      $query = $this->db->get('blog');
      if ($query->num_rows() > 0)
      {
         $result = $query->result();
      }
      return $result;
   }
}

View in views/blog/front.php

Code:
<html>
  <head>
    <title>My Blog Posts</title>
  </head>
  <body>
    <? if (count($posts)) : ?>
    <? foreach ($posts as $post) : ?>
      <h2>&lt;?= $post->title ?&gt;</h2>
      &lt;?= $post->body ?&gt;
    &lt;? endforeach; ?&gt;
    &lt;? else: ?&gt;
    <p>No blog posts yet!</p>
    &lt;? endif; ?&gt;
  &lt;/body&gt;
&lt;/html&gt;
#7

[eluser]taewoo[/eluser]
So that's what SHTML stands for.. Shit HTML




Theme © iAndrew 2016 - Forum software by © MyBB