Welcome Guest, Not a member yet? Register   Sign In
Stuck on database query
#1

[eluser]mathew edison[/eluser]
Yep, guess what, I managed to get stuck in the progress of making a script for my home page Tongue Below is the code I'm using and I want to select news from a separate table to display on my homepage however I limited my query to everything with an ID of 1. How would I fix this. Is there a way to end the effect of the $this->db->where or any other suggestions. I know this might be a stupid question but I'm not fully used to CodeIgniter yet. Thanks in advance.

Sincerely
- Mathew

Code:
function index()
    {
        $this->load->helper('url');
        $setting = '1';
        $this->db->where('id', $setting);
        $data['query'] = $this->db->get('settings');
        $this->load->view('header', $data);
        
        
        $this->load->view('menu');
        $this->load->view('welcome', $data);
        $this->load->view('welcome_message');
        $this->load->view('sidebar');
        $this->load->view('footer');
    }
#2

[eluser]mathew edison[/eluser]
Nvm fixed it by building a custom query inside the view file. Thanks anyways.
#3

[eluser]Michael Wales[/eluser]
eww... in the view - this sort of code should belong in the model.

I'm not exactly sure what you are trying to accomplish here though, everything is limited to an ID of 1 because of this line:
Code:
$setting = '1';
$this->db->where('id', $setting);

If you are wanting to display news, on a page, something like this would work:

models/news.php
Code:
function get($limit = 10, $offset = 0) {
  $query = $this->db->get('news', $limit, $offset);
  if ($query->num_rows() > 0) {
    return $query->result();
  }
  return FALSE;
}

controllers/home.php
Code:
function index($offset = 0) {
  $this->load->model('news');
  $this->data->news = $this->news->get();
  $this->load->view('home', $this->data);
}

views/home.php
Code:
// I've used the modified tags in this example, getting fond of them for client work
// Clients seem to understand more when braces are not found
<?php if ($news === FALSE): ?>
  <p>There are no news articles.</p>
&lt;?php else: ?&gt;
  &lt;?php foreach ($news as $article): ?&gt;
    <h2>&lt;?php echo $article->title; ?&gt;</h2>
    <p>&lt;?php echo $article->body; ?&gt;</p>
  &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
#4

[eluser]mathew edison[/eluser]
Hey, thx. That makes my code alot more cleaner. I was using the blog tutorial just cause I couldn't be bothered reading the user guide which was pretty much stupid. Anyways I got a questions as to the code you gave me just now. If I wanted to build a pagination for the news engine so it displays all articles on the front page but using pagination where would I apply the pagination class and would that change my code drastically or not at all? Owh and it's like this, I've got two things I'm showing on that page, the title which is retrieved from the database 'settings' however I want it to be retrieved from the first row, which is why I limited it to the ID of 1 though the above can obviously fix that.
#5

[eluser]Michael Wales[/eluser]
It wouldn't change your code much at all - thus the reason I place the $limit and $offset paramaters throughout the controller and model.

Read over the User Guide on Pagination (there are a few extended libraries in the Wiki as well) and give it a shot - if you hit any snags the people here on the forums can help out.




Theme © iAndrew 2016 - Forum software by © MyBB