Welcome Guest, Not a member yet? Register   Sign In
From Database to Array
#1

[eluser]georgerobbo[/eluser]
I'm struggling to select two fields from my database and place them within this array. I am trying to get the first column to go within 'title' and the second column to go within 'text' and then to loop for every other value in the table.

Code:
$data['content_item']['0'] = array('title' => 'Title_Goes_Here', 'text' => 'Text_Goes_Here');
#2

[eluser]jcavard[/eluser]
Code:
$handle = $this->db->get();

$data = array();
foreach($handle->result_array() as $row)
{
  $data['content_item'][] = $row;
}

it's the third obvious thread you start this morning. You should have a look at the User Guide first. It's all in there!
#3

[eluser]georgerobbo[/eluser]
Thanks, sorry.
#4

[eluser]jcavard[/eluser]
[quote author="georgerobbo" date="1249945237"]Thanks, sorry.[/quote]
Hey, no problem!
Just sayin'... somethime it's faster than waiting on the forum Wink
#5

[eluser]georgerobbo[/eluser]
Code:
function index()
    {
        $data['title'] = 'Home';
        
        $query = $this->db->query('SELECT title, text FROM ros_sidebar');
        $data = array();
        foreach($query->result_array() as $row)
        {
        $data['side_item'][] = $row;
        }
        
        $this->load->view('meta', $data);
        $this->load->view('header');
        $this->load->view('panel');
        $this->load->view('content', $data);
        $this->load->view('footer');    
   }

For some reason my query is interfering with my $data['title']. Oddly If I put $data['title'] after the query it works perfectly, but I can't quite understand why?
#6

[eluser]Dam1an[/eluser]
Because you overwrite the first data array containing title with a new data array later on
#7

[eluser]jcavard[/eluser]
Try this instead. Notice the few changes
Code:
function index()
{        
  $data['title'] = 'Home';
  $data['side_item'] = array();

  $query = $this->db->query('SELECT title, text FROM ros_sidebar');
        
  foreach($query->result_array() as $row)
  {
    $data['side_item'][] = $row;
  }
        
  $this->load->view('meta', $data);
  $this->load->view('header');
  $this->load->view('panel');
  $this->load->view('content', $data);
  $this->load->view('footer');    
}
#8

[eluser]georgerobbo[/eluser]
Ah, when I try to load it as a model CodeIgniter returns an error. I know I'm doing something really obvious but I'm struggling to understand calling the model from the documentation.

Code:
function index()
    {
        $data['title'] = 'Home';
        
        $this->load->model('Ros_sidecont');
        $data['sidecont'] = $this->Ros_sidecont->get_sidecont();
        
        $this->load->view('meta', $data);
        $this->load->view('header');
        $this->load->view('panel');
        $this->load->view('content', $data);
        $this->load->view('footer');    
   }
}

Code:
class Ros_sidecont extends Model {

    function Ros_sidecont()
    {
        parent::Model();
    }
    
    function get_sidecont()
    {
    $query = $this->db->query('SELECT ros_sidecont_title, ros_sidecont_text FROM ros_sidecont');
    $data = array();
    foreach($query->result_array() as $row)
        {
        $data[] = $row;
        }
    }
#9

[eluser]jcavard[/eluser]
[quote author="georgerobbo" date="1249951708"]
Ah, when I try to load it as a model CodeIgniter returns an error.
[/quote]

What error?

You also want to change your model, the way you use it, it should be like this
Code:
class Ros_sidebar extends Model {

    function Ros_sidebar()
    {
        parent::Model();
    }
    
    function get_sideitem()
    {
        $query = $this->db->query('SELECT title, text FROM ros_sidebar');
        $data = array(); // CHANGED THIS LINE
        foreach($query->result_array() as $row)
        {
             $data[] = $row; // CHANGED THIS LINE
        }
}
#10

[eluser]georgerobbo[/eluser]
I get:

Parse error: syntax error, unexpected ';', expecting T_FUNCTION in /var/www/system/application/models/ros_sidecont.php on line 19




Theme © iAndrew 2016 - Forum software by © MyBB