Welcome Guest, Not a member yet? Register   Sign In
help with array
#1

[eluser]Jbeasley6651[/eluser]
i'm having some trouble trying to use a query result as an array

First i run a query to get all the zips that i need

Quote: $this->db->select('zip');
$this->db->from('custom_city');
$this->db->join('city_to_zip', 'custom_city.id = city_to_zip.city_id', 'left');
$this->db->where('city_to_zip.city_id', $_POST['city']);
$zip = $this->db->get();
$data['zips'] = $zip;

$zip_array = $zip->result_array();

then the query output is this, when i try to use it.

Now i know it's doing something, because it says "array" 8 times and thats the right count for the query. I just need to know how to get the zip code in there instead of the word array.

Quote:AND `zip_code` IN (Array, Array, Array, Array, Array, Array, Array, Array)

in my query i'm using...

Quote: $this->db->where_in('zip_code', $zip_array);

Thanks,

Jbeasley
#2

[eluser]Jbeasley6651[/eluser]
i also get this error

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: database/DB_active_rec.php

Line Number: 586
#3

[eluser]John_Betong[/eluser]
[quote author="Jbeasley6651" date="1248062151"]i also get this error

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: database/DB_active_rec.php

Line Number: 586[/quote]
 
I would be tempted to look at the help files and see what return values type you expect to be returned.
 
When debugging I found that I was continually repeating code to establish the return value type and also the contents. Eventually I wrote the following function and my debugging is now easier. It works on boolean, numerics, string, arrays and Objects.
 
The function is now in an include file and is the first line to be included in my index,php file.
 
Code:
// usage:
//           fred(&variable;_of_any_type, 'Optional name or details');

//==========================================================================  
  function fred($data=array('Nothing passed'), $data_name='_missing_$data_name', $data_type='')
    {
         // $data objects do not display as an array so...
        if (is_object($data))
        {
            $data = get_object_vars($data); // returns with $data = array();
        }


        // maybe find the $data type
        if (empty($data))
        {
            $data_type     = "empty()";    
        }else{    
            switch($data)
            {
                case ('' == $data)     :
                    $data_type     = "empty()";    
                    break;
                case is_array($data)     :
                    $data_count    = count($data);
                    $data_type     = "array($data_count)";    
                    break;
                case is_bool($data) :
                    $data_type    = 'boolean';
                    $data                = $data ? 'TRUE' : 'FALSE';
                    break;
                case is_numeric($data) :
                    $data_type    = 'numeric';
                    break;
                case is_object($data) :
                    $data_type    = 'object';
                    $data                =    get_object_vars($my_class);
                    break;
                case is_resource($data) :
                    $data_type    = 'resource';
                    $data_count    = mysql_num_rows($data);
                    $tmp                = array();
                    while ($row = mysql_fetch_assoc($data))
                    {
                        $tmp[] = $row;
                    }
                    $data = $tmp;
                    break;
                case is_string($data) :
                    $data_type    = 'string';
                    $data_count    = strlen($data);
                    break;
                default:      
                    $data_type     = 'oddball';
                    $data_value    = $data;
            }//end switch
        }//endif    
        
        // $data must now be an array or a string, numeric???
        $style = 'width:90%; margin:1em; overflow:auto;text-align:left; font-family:Courier; font-size:0.86em; background:#efe none; color:#000; text-align:left';
    echo "<div style='$style'>";
            echo '<fieldset>';        
                echo    '<legend>John&rsquo;s Data Determiner  asdfasdfsadfsadfasfd</legend>';        
                echo    '<br /><b>Name &nbsp; ==> </b>'        .$data_name;
                echo    '<br /><b>Type &nbsp; ==> </b>'        .$data_type;
                if (isset($data_count))
                {
                    echo    '<br /><b>Count&nbsp; ==> </b>'        .$data_count;
                }    
                echo    '<br /><b>Value &nbsp;==> </b>';
                echo    "<pre style='width:99.98%; margin:-1.2em 0 1em 9.0em'>";
                    print_r($data);
                echo '</pre>';
            echo '</fieldset>';        
    echo '</div>';        
        return ''; // prevent displaying a 1 return value
  }//endfunc
&nbsp;
&nbsp;
Here is a typical result:
Code:
John’s Data Determiner _missing_$data_name
Name   ==> $this->cache_album_list
Type   ==> array(16)
Count  ==> 16
Value  ==>

Array
(
    [0] => JohnJokes-style1.jpg
    [1] => JohnJokes-style2.jpg
    [2] => JohnJokes1.jpg
    [3] => JohnJokes2.jpg
    [4] => JohnJokes3.jpg
    [5] => JohnJokes4.jpg
    [6] => Resume 2007 english.htm
    [7] => Thumbs.db
    [8] => dir_001.bat
    [9] => image001.jpg
    [10] => image002.jpg
    [11] => index.html
    [12] => index.php
    [13] => johns-jokes.jpg
    [14] => kill.txt
    [15] => zips
)
&nbsp;
&nbsp;
18
#4

[eluser]Jbeasley6651[/eluser]
my solution is i'm dumb and have never had to add items to an array

apperantly you do it like this.

Code:
$zip_array = array();
        foreach($zip->result() as $row):  {
        $zip_array[] = $row->zip; }
        endforeach;




Theme © iAndrew 2016 - Forum software by © MyBB