Welcome Guest, Not a member yet? Register   Sign In
foreach in models and controller
#1

Helper
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

function 
infoServer($name) {
    
$CI get_instance();
    
$CI->load->model('informations');
    
$CI->informations->info_server($name);


Model
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class 
Informations extends MY_Model {
    public function 
__construct() {
        
parent::__construct();

    }
    public function 
info_server($name) {
        
$db $this->load->database('site'TRUE);
        
$db    ->select('value')
                        ->
from('arcor_server_setting')
            ->
where('name'$name);
        
$query_info_view $db->get('');
        foreach (
$query_info_view->result() as $row){
           echo 
$row->value;
        }
    }


MY_Controller
PHP Code:
class MY_Controller extends CI_Controller {
        public function 
__construct(){
                
parent::__construct();
                
$this->information_server();
        }
        public function 
information_server() {
                
$this->load->helper('infoserver');
        }
      

In the same way I have an My_Model with the same content.

When I use infoServer('with the name of value') The result is printed on the top page. But not where I would actually. Dentrol and variables or functions inside. Only at the top.

While if I use it in a template it works perfectly.

Any solution?

I want to use the function so that when you call me the exact result, which it does, but it should work this way:

This
$variable = 'hello';
and this
$variable = infoServer('with the name of value');
They should be the same.

While at the time if I use the function produces the result I described before.

Thanks for the support.
Reply
#2

Don't use `echo` in model.
Reply
#3

(07-08-2015, 01:41 AM)kenjis Wrote: Don't use `echo` in model.

What should I use?
Reply
#4

(This post was last modified: 07-08-2015, 03:21 AM by StratoKyke.)

I removed the echo. But it does not work anyway. Now I do not print the result, but at the same time is not read even the variable.


EDIT:This problem occurs in both models * have a MY_Models* that controllers while in templates, with echo. It worked perfectly.
Reply
#5

I can try to edit the model in this way:
PHP Code:
public function info_server($name) {
        
$db $this->load->database('site'TRUE);
        
$db    ->select('value')
                        ->
from('arcor_server_setting')
            ->
where('name'$name);
        
$query_info_view $db->get('');
        
$row $query_info_view->row();
        echo 
$row->value;
    } 

I also tried to remove the echo.But it does not solve the problem. The tag is not read dovre but should be printed first.
Reply
#6

(This post was last modified: 07-08-2015, 04:28 AM by StratoKyke.)

This is an example:

[Image: attachment.php?aid=274]


Obviously the error sql wanted.

But as you can see the values that should be included in the query are written on top of everything.


And this happens both in model * in this case * that in the controller.

I think that is an error in Model, but I can not figure out how to solve.



P.S. I can try to remove echo in the model but It does not solve the problem

Attached Files Thumbnail(s)
   
Reply
#7

(07-08-2015, 02:47 AM)StratoKyke Wrote:
(07-08-2015, 01:41 AM)kenjis Wrote: Don't use `echo` in model.

What should I use?

If you call `echo`, it outputs at the moment.
So you should call echo only in views (but it seems you don't use views).

You have to return data from model, instead of echo.
Reply
#8

(07-08-2015, 04:48 AM)kenjis Wrote:
(07-08-2015, 02:47 AM)StratoKyke Wrote:
(07-08-2015, 01:41 AM)kenjis Wrote: Don't use `echo` in model.

What should I use?

If you call `echo`, it outputs at the moment.
So you should call echo only in views (but it seems you don't use views).

You have to return data from model, instead of echo.


I can try edit in this way:


PHP Code:
public function info_server($name) {
        
$db $this->load->database('site'TRUE);
        
$db    ->select('value')
 
                       ->from('arcor_server_setting')
            ->
where('name'$name);
        
$query_info_view $db->get('');
        foreach (
$query_info_view->result() as $row){
            return 
$row->value;
        }
    } 


But in this way It does not work anyway. I do not print variables above code, but it makes me even use.
Reply
#9

(This post was last modified: 07-08-2015, 05:09 AM by kenjis.)

It seems you don't know what `return` is.
`return` returns only one variable or value, and go back to where you called it.
Reply
#10

But in this case how should I use it?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB