Welcome Guest, Not a member yet? Register   Sign In
Help needed with Array.
#1

[eluser]Solarpitch[/eluser]
Hey Guys,

I'm having a problem trying to print the value of the arrays being called in the view. Here is my M, V and C.

MODEL~~~

Code:
//return the display specs
function in_c_display($id)
{
    
$query = $this->db->query('SELECT * FROM in_c_display WHERE id = "'.$id.'"');
return $query->result_array();
    
}
//return the memory specs
function in_c_memory($id)
{
    
$query = $this->db->query('SELECT * FROM in_c_memory WHERE id = "'.$id.'"');
return $query->result_array();
    
}



CONTROLLER~~~

Code:
function compare()
{

//~~~
$data['tab_nav'] = 'menus/tab_nav_1';
$this->load->view('template/header', $data);
$data['left_search'] = $this->load->view('menus/left_search');
//~~~
                
// HERE IM PASSING 3 PRODUCT ID'S TO AN ARRAY, THEN I WANT TO CALL THE 2 MODEL FUNCTIONS FOR EACH PRODUCT AND STORE THEM IN THE MULTIDIMENSIONAL ARRAY.    
    
$compare = array(1, 2, 3);    
foreach($compare as $key => $id)
        {
$data['in_c_display'][] = $this->product_model->in_c_display($id);
$data['in_c_memory'][] = $this->product_model->in_c_memory($id);
}
    
    
//~~~
$this->load->view('pages/compare', $data);
//~~~

$this->load->view('template/footer');
}



VIEW~~~

Code:
<?php foreach($in_c_display as $display)
{

//THIS IS HOW I'M TRYINT TO PRINT THE RESULT BUT IT DOESNT SEEM TO WORK. I GET AN ERROR SAYING UNDIFINED VARIABLE type, resolution etc. I tried it like $display['resolution'] BUT STILL NO JOY

echo "Type".$display->type;
echo "Resolution".$display->resolution;

}?>


<?php foreach($in_c_memory as $memory)
{


echo "Internal".$memory->internal;
echo "External".$memory->external;

}?>
#2

[eluser]JoostV[/eluser]
Have you checked if the queries return any records? In your controller, add this just before $this->load->view('pages/compare', $data);

Code:
echo '<h1>Query results</h1>';
var_dump($data);
#3

[eluser]Sarfaraz Momin[/eluser]
there seems to be one small issue with your controller method. Your resultset itself is an array and you are making another array to store before you pass it to the view. But in the view the display isn't picking it from the right array level. What I mean here is you are passing the value of this $data['in_c_display'][] to the view which is ok but in the view you are simply not using the correct method to display it. What you can do is try print_r($in_c_display) in your view to identify your array depth and use them to display accordingly.

Hope that makes sense.

-Sarfaraz.
#4

[eluser]Solarpitch[/eluser]
Hey,

I done print_r($in_c_display); and it printed out the below. So there are records being returned. Not sure what way I can pick the results out from the array to display on the page. $display->type; etc doesnt seem to work.

Code:
Array ( [0] => Array ( [0] => Array ( [id] => 1 [phone_id] => 1 [type] => TFT [colours] => 256k [resolution] => 240 x 320 pixel [size] => 2.2" diagonal ) ) [1] => Array ( [0] => Array ( [id] => 2 [phone_id] => 2 [type] => TFT [colours] => 125k [resolution] => 240 x 320 pixel [size] => 2" diagonal ) ) [2] => Array ( [0] => Array ( [id] => 3 [phone_id] => 3 [type] => TPT [colours] => 256k [resolution] => 240 x 320 pixel [size] => 2.8" diagonal ) ) )
#5

[eluser]Sarfaraz Momin[/eluser]
If you check your result closely you would see that there are 2 arrays before it actually populates the result. I have just formatted it to understand better

Code:
Array (
    [0] => Array (
        [0] => Array (
            [id] => 1
            [phone_id] => 1
            [type] => TFT
            [colours] => 256k
            [resolution] => 240 x 320 pixel
            [size] => 2.2 diagonal
        )
    )
    [1] => Array (
        [0] => Array (
            [id] => 2 [phone_id] => 2
            [type] => TFT
            [colours] => 125k
            [resolution] => 240 x 320 pixel
            [size] => 2 diagonal
        )
    )
    [2] => Array (
        [0] => Array (
            [id] => 3
            [phone_id] => 3
            [type] => TPT
            [colours] => 256k
            [resolution] => 240 x 320 pixel
            [size] => 2.8 diagonal
        )
    )    
)

I can see just one row per id. In that case you can use row_array in your model to just get one single row. In case you still want to use result_array to pull multiple items you would have to either use proper index to create a foreach loop as you did for the base array

Code:
&lt;?php foreach($in_c_display as $display)
{
foreach($display as $displayeach){
//THIS IS HOW I'M TRYINT TO PRINT THE RESULT BUT IT DOESNT SEEM TO WORK. I GET AN ERROR SAYING UNDIFINED VARIABLE type, resolution etc. I tried it like $display['resolution'] BUT STILL NO JOY

echo "Type".$displayeach['type'];
echo "Resolution".$displayeach['resolution'];
}
}?&gt;

&lt;?php foreach($in_c_memory as $memory)
{
foreach($memory as $memoryeach){
echo "Internal".$memoryeach['internal'];
echo "External".$memoryeach['external'];
}
}?&gt;

Have a good day !!!
#6

[eluser]Solarpitch[/eluser]
Man thanks a mill for the help! I should have formatted that myself for you, sorry! It's working perfect now. Smile
#7

[eluser]John_Betong[/eluser]
&nbsp;
Here is a formatting function that I use extensively instead of using print_r( $myArray ):

Code:
//==========================================================================  
  function fred($msg=array('No', 'array or string', 'has', 'been', 'passed'), $msg_name='$msg_name not passed')
  {
    echo "<pre style='text-align:left; font-size:1.0em; background:#ffc none; color:#000'>";.

      echo '<b>' .$msg_name .'</b>';
    print_r($msg);
       echo '<br />';

    echo "</pre>";
  }//

&nbsp;
&nbsp;
&nbsp;
#8

[eluser]gscharlemann[/eluser]
The array formatting code is fantastic...a huge help. But after following the example here, I'm still having issues in my view. I am getting an undefined variable error.

I'm attempting to pass $query->result_array() to my view with the following data.
In the controller:
Code:
$people_array = $query->result_array();
$this->load->view('people',$people_array);

The data in the array is as follows:
Code:
$msg_name not passedArray
(
    [0] => Array
        (
            [contact_number] => 1
            [first_name] => John
            [last_name] => Doe
        )

    [1] => Array
        (
            [contact_number] => 2
            [first_name] => Jane
            [last_name] => Doe
        )
)

In the view, I've attempted the following:
Code:
&lt;?php
foreach($people_array as $people)
{
    foreach($people as $person)
    {
?&gt;
    <tr>
    <td>&lt;?php echo $person['contact_number'];?&gt;</td>
    <td>&lt;?php echo $person['first_name'];?&gt;</td>
    <td>&lt;?php echo $person['last_name'];?&gt;</td>
    </tr>
&lt;?php
    }
}
?&gt;

I get two errors total:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: contacts_array
...
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()

Any ideas where I went wrong?
#9

[eluser]TheFuzzy0ne[/eluser]
Try:
Code:
&lt;?php foreach($people_array as $person): ?&gt;
    <tr>
    <td>&lt;?php echo $person['contact_number'];?&gt;</td>
    <td>&lt;?php echo $person['first_name'];?&gt;</td>
    <td>&lt;?php echo $person['last_name'];?&gt;</td>
    </tr>
&lt;?php endforeach; ?&gt;

The problem is that you're trying to go too deep into the array. You're trying to go to level 3 when it's only 2 levels deep.
#10

[eluser]gscharlemann[/eluser]
Thanks for looking at my problem. I tried as you suggested, but it didn't work. I'm getting the same two errors.

Any other thoughts?




Theme © iAndrew 2016 - Forum software by © MyBB