Welcome Guest, Not a member yet? Register   Sign In
PHP strings with php arrays in CI
#1

[eluser]Jacek Dominiak[/eluser]
Hello all,

It's my first post here as I am stuck in a dead point (possible lack of knowledge from my side). I tried various ways and just can't get it run. As for a person which just started with CI, please be gentle ;P Wink

I have 2 tables:
1. pairs (list of all values and it's comments).
2. user_pairs defined by user to reuse later on within the application.

Here are concerned
- model functions:
Code:
function get_all_pairs()
    {  
        $q = $this->db->get('pairs');
        return $q->result();
    }  

    function get_user_pairs($uid)
    {  
        $this->db->select('pair');
        $q = $this->db->get_where('user_pairs', array('uid' => $uid));
        return $q->result();
    }

- controler:
Code:
function list_user_pairs()
    {  
    
        if($this->dx_auth->is_logged_in())
            {  
                $uid = $this->dx_auth->get_user_id();
                $uname = $this->dx_auth->get_username();
                $data['user_selected'] = $this->settings_model->get_user_pairs($uid);
                $data['pairs'] = $this->settings_model->get_all_pairs();
                $data['main_content'] = 'settings/list_pairs';
                $data['uname'] = $uname;                                                                                                                                                                                                  
                $this->load->view('theme/template', $data);
            }  
        else
            {  
               redirect('auth/login');
            }  
    
    }

- view:

Code:
echo form_open('settings/pair_save');
echo '<div id="content">';
$array = (array) $user_selected;
echo '<br />';
foreach($pairs as $pair){
        if (in_array($pair->pair, $array))                                                                                                                                                                                                
        {  
                $data = array(
                    'name' => $pair->pair,
                    'id' => $pair->pid,
                    'value' => 'accept',
                    'checked' => TRUE,
                    'style' => 'margin: 10px; ',);
        }  
        else
        {  
                $data = array(
                    'name' => $pair->pair,
                    'id' => $pair->pid,
                    'value' => 'accept',
                    'checked' =>  FALSE,
                    'style' => 'margin: 10px; ',);
        }  
echo '<p>' .form_checkbox($data), $pair->pair. '&nbsp;<h4>' .$pair->comment. '</h4></p><br />';
}
echo form_submit('save_pairs', 'Save');
echo form_close();


My problem here is that I try to check if there are any user pairs in order to display them as checked.

No believe it's a problem with string to array convention... but dunno where to go from here Sad

I would really appreciate any help.
#2

[eluser]überfuzz[/eluser]
Welcome to the forum!

First i thought I understood the issue, but after looking through the code I'm a little lost. What's the function going to do for the users..?
#3

[eluser]Jacek Dominiak[/eluser]
It is going to display all checkboxes possible (from 1st table) and check only those which user wanted to use later within the application (table 2)

As for the users ... it just checks if the user is logged in in order to get the UID for selecting right data from table 2
#4

[eluser]überfuzz[/eluser]
Sorry if I'm not getting it...

I don't understand why you need to do exceptions in the vie file..? Doesn't every user have a list with checkboxes? Is it possible to check whether you get any query result in the model or in the controller.
Code:
//model
if ($query->num_rows() > 0)
{
    return $query->result();
}
else
{
    return false; //or something nifty
}

Code:
//controller
if(!$this->your_model->fetch_something_please())
{
   //do stuff without list
}
else
{
   //do stuff with list
}

Code:
//view
&lt;?php foreach($params AS $field): ?&gt;
<p>layout</p>
&lt;?php echo form_checkbox( $field->name, $field->value, $field->checked); ?&gt;
<p>more layout </p>
&lt;?php endforeach; ?&gt;
#5

[eluser]überfuzz[/eluser]
edit, double post...
#6

[eluser]Jacek Dominiak[/eluser]
I agree, maybe the if statement should be in controller. But that is not the case. What I am missing is:

- you have table with all the pairs possible to choose(table 1)
- you have a table with user selected from pair table (in that table there are only pairs which user choose to use) (table 2)

I want to display it in a way that you will have all the pairs displayed with those selected by user already "ticked" in a check box.

I hope it's clearer now.
#7

[eluser]überfuzz[/eluser]
Maybe you could show some of the debugging you've done so far. I guess it's simply to hard to guess what you've done wrong.

What do you get in the query? Do get anything while running the model-methods in the controller. Do you get the info through to the view file..?
#8

[eluser]Jacek Dominiak[/eluser]
My guess is that I try to check the values in a array with

Code:
if (in_array($pair->pair, $array))

where if I print array it look like

Code:
Array ( [0] => stdClass Object ( [id] => 2 [uid] => 3 [pair] => GBP/USD ) [1] => stdClass Object ( [id] => 1 [uid] => 3 [pair] => USD/JPY ) )

Maybe I should change the in_array function... but for which one ?
#9

[eluser]überfuzz[/eluser]
Try
Code:
return $q->result_array();
#10

[eluser]Jacek Dominiak[/eluser]
I tried, and I am getting closer ... now I have array like

Code:
Array ( [0] => Array ( [id] => 2 [uid] => 3 [pair] => GBP/USD ) [1] => Array ( [id] => 1 [uid] => 3 [pair] => USD/JPY ) )

Still though the in_array clause does not work ... My guess is that is because it works on flat arrays not a multiple one which CI produses.

It worked when I hardcoded test array just to see if it works.

Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB