CodeIgniter Forums
get data from database as obj or array - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: get data from database as obj or array (/showthread.php?tid=22093)



get data from database as obj or array - El Forum - 08-29-2009

[eluser]ranjitbd[/eluser]
//this is the controller

<?php
class Holiday extends Controller {


function getDestination()
{
$data = array();
$this->load->model($this->config->item('generalModel'), 'getDes',TRUE);
$data['desPack']=$this->getDes->desPackName();
print_r($data);
// echo $data['destination_name']; //its not executing showing error
exit;
}

}
?>

//this is the model

<?php
class User_model extends Model
{
function User_model()
{
parent::Model();
}

function desPackName()
{
$query = $this->db->get('destination');
return $query->result();
}
}
?>

//output of this code

Array ( [desPack] => Array ( [0] => stdClass Object ( [destination_id] => 1 [destination_name] => Around Dhaka [destination_description] => Dhaka is a magnificent city, where people & history are thousand years old. [destination_image] => dhaka.jpg [remarks] => ) [1] => stdClass Object ( [destination_id] => 2 [destination_name] => Around Chittagong [destination_description] => Chittagong is a magnificent city, where people & history are thousand years old. [destination_image] => chittagong.jpg [remarks] => ) [2] => stdClass Object ( [destination_id] => 3 [destination_name] => Aroung Coxs Bazar [destination_description] => Coxs Bazar is a magnificent city, where people & history are thousand years old. [destination_image] => coxsbazar.jpg [remarks] => ) [3] => stdClass Object ( [destination_id] => 4 [destination_name] => Around Sylhet [destination_description] => Sylhet is a magnificent city, where people & history are thousand years old. [destination_image] => sylhet.jpg [remarks] => ) [4] => stdClass Object ( [destination_id] => 5 [destination_name] => Around Khulna [destination_description] => Khulna is a magnificent city, where people & history are thousand years old. [destination_image] => khulna.jpg [remarks] => ) [5] => stdClass Object ( [destination_id] => 6 [destination_name] => Hill District [destination_description] => Dhaka is a magnificent city, where people & history are thousand years old. [destination_image] => hills.jpg [remarks] => ) ) )


//i have to execute the following line of code
// echo $data['destination_name']; //its not executing showing error
//the output should be

Around Dhaka


get data from database as obj or array - El Forum - 08-29-2009

[eluser]jedd[/eluser]
Seriously .. after this many posts, you should have a handle on using [ code ] tags around your code.

Also, do an
Code:
echo "<pre>";
before print_r/var_dump


get data from database as obj or array - El Forum - 08-29-2009

[eluser]alboyd[/eluser]
You assigned the result array of the query to $data['desPack']. $data['destination_name'] doesn't exist...

You want to get destination_name out of the $data['desPack'] array...
Code:
foreach ($data['desPack'] as $row)
{
echo $row['destination_name'];
}

BTW I could very well be wrong but I don't think it's a good idea to use camel case on your variables... I certainly don't do it... but then what do I know..


get data from database as obj or array - El Forum - 08-30-2009

[eluser]ranjitbd[/eluser]
i cant understand what u want to say by "handle on using [ code ] tags"


get data from database as obj or array - El Forum - 08-30-2009

[eluser]zyzzzz[/eluser]
[quote author="ranjitbd" date="1251630007"]i cant understand what u want to say by "handle on using [ code ] tags"[/quote]

When you're posting code, use the [ code][ /code] tags around the code (without the spaces in the tags).


get data from database as obj or array - El Forum - 08-30-2009

[eluser]ranjitbd[/eluser]
//this is the controller
Code:
&lt;?php
class Holiday extends Controller {


  function getDestination()
  {
      $data = array();
      $this->load->model($this->config->item(‘generalModel’), ‘getDes’,TRUE);
      $data[‘desPack’]=$this->getDes->desPackName();
      print_r($data);
  // echo $data[‘destination_name’]; //its not executing showing error
      exit;
  }

}
?&gt;


would u like to say this about code block


get data from database as obj or array - El Forum - 08-30-2009

[eluser]jedd[/eluser]
What does this show:

Code:
echo $data['desPack'][‘destination_name’];



get data from database as obj or array - El Forum - 08-30-2009

[eluser]alboyd[/eluser]
[quote author="jedd" date="1251654953"]What does this show:

Code:
echo $data['desPack'][‘destination_name’];
[/quote]

That would show an error wouldn't it?

Code:
echo $data['desPack'][0][‘destination_name’];

Would show - Around Dhaka - no?


get data from database as obj or array - El Forum - 08-30-2009

[eluser]jedd[/eluser]
Oh, very possibly .. I've been spoiled by several years of formatted output.


get data from database as obj or array - El Forum - 08-30-2009

[eluser]alboyd[/eluser]
[quote author="jedd" date="1251659221"]Oh, very possibly .. I've been spoiled by several years of formatted output.[/quote]

Smile