Welcome Guest, Not a member yet? Register   Sign In
[newbie] I need help (database listing)
#1

[eluser]Wievior[/eluser]
Hello,

I am completely new to CodeIgniter and I encountered a problem while learning from a tutorial I found on the Internet. Here's the code:

Model:
Code:
<?php

class News extends Model
{
function News()
{
parent::Model();
}
function get_news()
{
// Wszystkie newsy sortowane malejÄ…co po news_id
$this->db->orderby("news_id", "desc");
return $this->db->get('news');
}
function add_news($data)
{
// dodanie newsa
return $this->db->insert('news', $data);
}
function update_news($id, $data)
{
// zmiana newsa o podanym numerze news_id
$this->db->where('news_id', $id);
return $this->db->update('news', $data);
}
function delete_news($id)
{
// skasowanie newsa o podanym news_od
$this->db->where('news_id', $id);
return $this->db->delete('news');
}
}

Controller:
Code:
<?php

class Formularz extends Controller
{
function index()
{
$data["tytul"] = array('name' => 'tytul');
$data['tresc'] = array('name' => 'tresc', 'rows' => 3, 'cols' => 40);

$rules['tytul'] = "required";
$rules['tresc'] = "required";
$this->validation->set_rules($rules);

if ($this->validation->run() == FALSE)
{
$data['tytul']['value'] = $this->input->post('tytul');
$data['tresc']['value'] = $this->input->post('tresc');
$this->load->view('form', $data);
}
else
{
$this->load->model('News');
$this->News->add_news(array('news_title' => $this->input->post('tytul'), 'news_text' => $this->input->post('tresc')));
echo 'Dane zapisane';
}
}
function listuj()
{
$this->load->model('News');
foreach ($this->News->get_news()->result() as $val)
{
echo $val->news_title.'<BR />';
}
}

}

It's in polish but I guess that it shouldn't be a problem. Until I put in the listuj() function everything worked just fine, the form, validation, inserting into database (checked it with PMA).

However - when I inserted this function I get an error:
Quote:Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /mypage/formularz.php on line 30

Line 30 being the one with foreach. Something with result() seems to be wrong. But as I said, I just started my journey with CI so I don't really know how to fix it.

Any help/advise would be greatly appreciated.

Regards,
Paul
#2

[eluser]geocine[/eluser]
Where is the formularz.php please paste it.

Change this:

Code:
foreach ($this->News->get_news()->result() as $val)
{
echo $val->news_title.'<BR />';
}

To this:

Code:
$qs = $this->News->get_news();
foreach ($qs->result() as $val)
{
echo $val->news_title.'<BR />';
}

so that get_news() is not called every loop.
#3

[eluser]Wievior[/eluser]
The Controller is formularz.php ;]

How did I not see that? :| thanks a lot, works like a charm! :]




Theme © iAndrew 2016 - Forum software by © MyBB