Welcome Guest, Not a member yet? Register   Sign In
Reading from database table
#1

[eluser]Altazar[/eluser]
Hi, I have a multilingual site with HMVC and it works fine, but I want to put translations in the database, instead text files. I'll leave text files translation for menu, but main content should be read from the database (for later easy updating). Database structure looks like on attached picture (should I make it different?). I'm trying all day to get it to work, but no success.

Controller:
Code:
class Italy extends MX_Controller {

public function __construct()
{
  parent::__construct();
}

public function index(){
   $this->load->view('site_header');
   $this->lang->load('menu');
   $this->load->view('menu');
   $lang_short = $this->lang->lang(); // en, de, fr...
   $this->load->model('mdl_italy');
   $data['results'] = $this->mdl_italy->get_harbour($lang_short); // ?
   $this->load->view('content_italy', $data);
   $this->load->view('site_footer');
}
Model:
Code:
class Mdl_italy extends MX_model {

function get_harbour($lang_short)  
  {  
  $query = $this->db->get_where('harbours', array('lang_short' => $lang_short));
  return $query->result();
  }
}
View:
Code:
<?php
print_r($results);
?>
I get no errors, only a completely blank page.
#2

[eluser][email protected][/eluser]
are you able to see in English version ?

which browser are you using to see ?
#3

[eluser]TheFuzzy0ne[/eluser]
Are you sure that's what you want to do? For a page with, say, 50 translations. That's potentially 50 database queries on top of any other queries.

I would definitely suggest using files, or at the very least, caching languages from the database.
#4

[eluser]Altazar[/eluser]
Hi, thanks for answers.

I've made some changes and now I get the page in browser (Firefox), but with no data from the database.

I'm using text files for menus. It works fine, I have 30 languages.

For main content I have to use database because it will be often updated. I'd like to make it (later) easy editable for admin, he should click edit buton and get the content in a textbox. I think it's like in MojoMotor (I've only seen it in a YouTube video).

First I have to make the pages to only read data from the database.

I send another database screenshot. Maybe I should organize it other way?
I have this website's old version out of CodeIgniter and there I have a separate table for each language.
#5

[eluser]TheFuzzy0ne[/eluser]
I think you should have two tables. One for the languages, and one for the actual translations. This will save you from having to add an extra column every time you want to add a new translation, and ending up with a table that has hundreds of columns. You can use the language ID and word key as a composite primary key.

I would suggest extending the Language class to get the functionality you want. You could also implement any caching here.
#6

[eluser]Altazar[/eluser]
I actually already have a separate "languages" table, because I make this inside SharpEdge CMS. But I don't like the way that CMS makes translations - I have to create each page in each language separatelly. So for 30 pages in 30 languages, I'd have to manually add 900 pages. There will be no other languages and pages added.

I have to finish this project soon, I have other jobs to do.
If someone would help me make this multilingual CRUD, I will pay for it.
#7

[eluser]Altazar[/eluser]
I've made some changes in module, but instead the data from database, I get the word "Array" in page.
Code:
class Mdl_italy extends CI_model {

function Mdl_italy()
{
  parent::__construct();
}

function get_harbour($lang_short)
  {
  $query = $this->db->get_where("harbours", array("lang_short" => $lang_short));
  return $query->result();
  }
}
#8

[eluser]TheFuzzy0ne[/eluser]
If you're only expecting a single result, then you should return $query->row(). $query->result() returns an array of results, even if there's only a single result.

After that, you will need to actually return the value of the column:
Code:
if ( ! $query->num_rows())
{
    return '';
}

return $query->row()->some_column;
#9

[eluser]Altazar[/eluser]
I was trying to do that, but whatever I do, I get a blank page.

This is my model:
Code:
function get_harbour($lang_short)
  {
  $query = $this->db->get_where("harbours", array("lang_short" => $lang_short));
     if ( ! $query->num_rows())
   {
   return '';
   }
   return $query->row()->italy;  
  //$harbours = $this->db
  //->where('lang_short', $lang_short)
  //->get('harbours');
  //return $harbours;
  }

I've downloaded and installed Grocery CRUD, but its examples didn't help me. What I need is more simple.

First I want to read text from the table column ("Italy" in example) for the language which is in the URL ($lang_short).

Later I would like to have page for admin where I would display texts in textboxes for all languages and enable updating.
#10

[eluser]TheFuzzy0ne[/eluser]
A blank page normally means that an error is bring thrown, but you don't have error reporting enabled. Try putting this at the top of your index.php file (and don't forget to remove it when you're site goes live):
Code:
ini_set('display_errors', '1');
error_reporting(E_ALL);

I would suggest you have a language model. Something like this (untested):
Code:
class Language_m extends CI_Model {
    
    protected $short_lang = 'en';
    
    function set_lang($short_lang = 'en')
    {
        $this->short_lang = $short_lang;
    }
    
    function get($word_key = '')
    {
        if ( ! $word_key)
        {
            return FALSE;
        }
        
        $res = $this->db
            ->select($word_key);
            ->where('lang_short', $this->short_lang);
            ->get_where('language')->row_array();
    
        if ( ! $res)
        {
            return FALSE;
        }
        
        return $res;
    }
}

So from your controller:
Code:
// Load the language model (should be loaded via ./application/config/autoload.php).
$this->load->model('language_m', 'db_lang');

// Set the short language code (You could do this in your MY_Controller constructor).
$this->db_lang->set_lang($this->session->userdata('short_lang'));

// Print a word.
echo = $this->db_lang->get('word');

// Or get multiple words in one database query.
$words = $this->db_lang->get(array(
    'word_1',
    'word_2',
    'word_3',
));

// Print them.
echo $words['word_1'] . '<br />';
echo $words['word_2'] . '<br />';
echo $words['word_3'] . '<br />';

This could be improved upon in several ways. For example, you could have a db_lang() helper function, or maybe add a method that will simply preloads the translations into the model, so you don't hit the database once for each word.

I also recommend you restructure your table so you don't have to add a new column for each word.




Theme © iAndrew 2016 - Forum software by © MyBB