Welcome Guest, Not a member yet? Register   Sign In
Array problem in view
#1

[eluser]webscriptz.be[/eluser]
Hi,

I'm writing a system for a webhoster and it needs a language system from the database so i made my db(mysql) and have everything. Ci retrieves it and puts it into the view as an array.

model settings.php
Code:
<?php

class Settings extends Model{
  
  function Settings(){
    parent::Model();

  }
  
  public function getConfig(){
      
      $data = array();
      $Q;
      
      $this->db->select("name, value");
      
      $Q = $this->db->get("configurations");
      
      if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
             $data = array("name" => $row['name'], "value" => $row['value']);
           }
      }
      
      return $data;
  }
  
  public function getLang(){
      
      //assign $data var
      $data = array();
      
      //getting selected language
      $C = $this->db->query('SELECT id FROM langs WHERE status = 1');
      
      
      //only be one language selected at the time
      if ($C->num_rows() == 1){
          
          foreach ($C->result_array() as $row){
             $selected = $row['id'];    
           }
          
          //getting language values of the selected lang
          $this->db->select("name, value");
          $this->db->where("lang_id", $selected);
      
          $Q = $this->db->get("langv");
          
        foreach ($Q->result_array() as $row){
             $data = array("id"=>$row->id, "name" => $row['name'], "value" => $row['value']);
           }
     return $data;
      }
      
  }
  
  public function getLinks($section){
      $data = array();
      
      $this->db->select("name, href, status, section");
      $this->db->where("section", $section);
      
      $Q = $this->db->get("links");
      
      if ($Q->num_rows() > 0){
        foreach ($Q->result_array() as $row){
             //$lang var in view
             $data = array(    "name" => $row['name'],    "href" => $row['href'],    "status" => $row['status'],"section" => $row['section']);
           }
      }
      
      return $data;
  }

}
?>

controller frontpage.php
Code:
<?php

class Frontpage extends Controller {

    function Frontpage()
    {
        parent::Controller();
        $this->load->model('Settings');    
        $this->load->database();
        
    }
    
    function index()
    {    
        $data['links'] = $this->Settings->getLinks("frontpage");
        $data['config'] = $this->Settings->getConfig();
        $data['lang'] = $this->Settings->getLang();
        $this->load->view('default/frontpage/index', $data);
    }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/frontpage.php */

view
Code:
<p>$data variable</p>

&lt;?php foreach($lang as $item):?&gt;
  <li>&lt;?= $item;?&gt;</li>
  
&lt;?php endforeach;?&gt;

I was hoping i could do the following thing in CI:

$lang[id]["diffenrent things here"];
because i need them in a template. So anybody any idea? And don't tell me CI language helper is easier to use because i need it to be really agile and adding languages on the fly through an interface.
#2

[eluser]TheFuzzy0ne[/eluser]
Assuming id is the ID of the language, why are you loading all of your languages at once, instead of just loading the language you need?

EDIT: I take that back. I was a bit confused by your code.
#3

[eluser]TheFuzzy0ne[/eluser]
I think I understand the question, and I think this is your answer:
Code:
foreach ($Q->result_array() as $row){
    $data[$row['id']][$row['name']] = $row['value'];
}

I'd also suggest you use a helper for selecting the key you want, otherwise you will find you have to use isset() a lot or you may end up with warnings.

Also, I noticed that your original version of my code above attempts to access an object, when the result is an array.
#4

[eluser]webscriptz.be[/eluser]
I select all the values from "langv" where foreign key lang_id is selected from "langs" and only the one where status is 1.
as for the code:

I'don't think it's the solution,(sorry), basically I want to do something like smarty does with smarty->assign() so that I can easily take them in the tpl

my langv is id - name and value and I was thinking of parsing for each name, the same variable with the value in it.
#5

[eluser]TheFuzzy0ne[/eluser]
Why not use smarty? http://devcha.blogspot.com/2007/12/smart...-code.html
#6

[eluser]webscriptz.be[/eluser]
I like the CI system and smarty irritates me.
#7

[eluser]webscriptz.be[/eluser]
after trying I gave up on smarty but I tried your solution a last time and it works fine now so Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB