Results 1 to 5 of 5

Thread: convert Results into Associative array

  1. #1
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default convert Results into Associative array

    Hi All,

    I want to convert mysql results into associative array like below

    Code:
    function jobs()  // previously i was using this function and i was declaring as mentioned in $job_arr
    {
    	
    	$job_arr = array(
    	' '  => "Please select job type",
           '1'=>"Full Time",
           '2'=>"Part Time",
    '       3' =>"Freelancing"	
    	);
    
    	return $job_arr;
    }
    But i want to get these results from my mysql result

    Code:
    function jobs()
    {
    
    
            $sql = "Select * from jh_job_type where Status = 1";
    	$query = $ci->db->query($sql);
    	$result =  $query->result_array();
    
       // Id,Name are the field names 
    
      $job_arr = '';     // i started doing with foreach but did not get required result.
    
    
    
    return $job_arr;
    }

    Anyone know how to do this one?
    Last edited by james438; 02-15-2013 at 01:12 PM. Reason: please use code tags

  2. #2
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    solved

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    1,689
    Thanks
    82
    Thanked 89 Times in 87 Posts

    Default

    Good to hear! How did you solve it? If it is solved could you mark this thread as resolved?
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Nov 2010
    Posts
    114
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default

    I have changed my function as below


    Code:
    function jobs()
    {
    
           
    $sql = mysql_query("Select Id,Name from jh_job_type where Status = 1");
    	while($result = mysql_fetch_array($sql))
    	{
    		$jobs_arr[$result['Id']] = $result['Name'];
    	}
    	return $jobs_arr;
    
    }

    This solved my issue. Hope it helps someone
    Last edited by james438; 02-19-2013 at 06:22 AM.

  5. #5
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 20 Times in 20 Posts

    Default

    Why not just use mysql_fetch_assoc instead?

Similar Threads

  1. Replies: 2
    Last Post: 05-05-2011, 05:23 PM
  2. Woes Getting a Value from an Associative Array
    By Warvinlin in forum JavaScript
    Replies: 3
    Last Post: 10-18-2009, 07:26 PM
  3. Replies: 2
    Last Post: 10-25-2008, 09:35 AM
  4. php sort using associative array
    By Strangeplant in forum PHP
    Replies: 2
    Last Post: 01-25-2007, 06:56 PM
  5. Db results in an array....possible?
    By Mojeaux in forum JavaScript
    Replies: 2
    Last Post: 05-16-2006, 04:55 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •