linux1880 Posted August 11, 2010 Share Posted August 11, 2010 Hi friends, a quick print_r() shows the following array while connecting to my database. How to get them as individual items and display on the website ? Array ( [0] => Array ( [id] => 52 [document_name] => xyz [document_ext] => gif [download_file_name] => ktm_impact.gif [upload_dt] => 2010-08-11 ) ) Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/ Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 a quick print_r() shows the following array while connecting to my database Connecting to a database does not retrieve data. You seem to be wrapping your results within another array. Can we see how you are actually creating this array? Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098112 Share on other sites More sharing options...
linux1880 Posted August 11, 2010 Author Share Posted August 11, 2010 well this is the views file of codeIgniter php framework. Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098201 Share on other sites More sharing options...
wildteen88 Posted August 11, 2010 Share Posted August 11, 2010 well this is the views file of codeIgniter php framework. Then you'll be defining this array within your controller. What is the name of the variable that contains the array you posted above? Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098204 Share on other sites More sharing options...
linux1880 Posted August 12, 2010 Author Share Posted August 12, 2010 view file var is print_r($document); coltroller file is $this->page_content['document']=$this->Model->showPics(); model is function showPics(){ $sql="select * from document"; $query=$this->db->query($sql); return $query->result_array(); } Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098412 Share on other sites More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 Your still being quite vague so I'll just give you a simple example. foreach ($document as $doc) { echo $doc['id'] . '<br />'; echo $doc['document_name'] . '<br />'; echo $doc['document_ext'] . '<br />'; echo $doc['download_file_name'] . '<br />'; echo $doc['upload_dt'] . '<br />'; } Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098413 Share on other sites More sharing options...
linux1880 Posted August 12, 2010 Author Share Posted August 12, 2010 Thank you thorpe, that's using foreach, i want to access some fields without using loops, for example if i access echo $document['document_name']; directly, it's giving me error. A PHP Error was encountered Severity: Notice Message: Undefined index: document_name Filename: libraries/Loader.php(673) : eval()'d code Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098421 Share on other sites More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 Arrays really are one of the fundamentals you need to understand thoroughly. They are simple, yet very powerful. echo $document[0]['document_name']; Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098425 Share on other sites More sharing options...
linux1880 Posted August 12, 2010 Author Share Posted August 12, 2010 Thank you so much, yeah i got what i want. When looking at arrays in php.net manual it sounds so simple but while building application it's a major issue, hope i will get learn more, the more i spend time in it. Quote Link to comment https://forums.phpfreaks.com/topic/210447-how-to-access-database-fields-as-an-array-variables/#findComment-1098430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.