Orasion Posted February 6, 2011 Share Posted February 6, 2011 Hi again all, once again I need your help. I try to fetch data from my database which doesnt have any result yet. I want it to echo "No result" if there is no data from database. My code is <?php foreach($comment->result_array() as $comment) {?> <p><?php echo $comment['text']; ?></p> <p><i><?php echo $comment['author']; ?></i></p> <?php } ?> I tried to insert <?php if(!empty($comment)) { //above code } else { echo "No result"; } ?> but no output have been printed. Can you give me a hint to echo empty array like this one? Thanks Link to comment https://forums.phpfreaks.com/topic/226893-ask-codeigniter-empty-array/ Share on other sites More sharing options...
Cagecrawler Posted February 6, 2011 Share Posted February 6, 2011 Try (!empty($comment->result_array()) Link to comment https://forums.phpfreaks.com/topic/226893-ask-codeigniter-empty-array/#findComment-1170720 Share on other sites More sharing options...
Orasion Posted February 7, 2011 Author Share Posted February 7, 2011 I'm a bit confused here, does the full code look like this <?php if(!empty($comment->result_array())) { ?> <?php foreach($comment->result_array() as $comment) {?> <p><?php echo $comment['text']; ?></p> <p><i><?php echo $comment['author']; ?></i></p> <?php } ?> <?php } else {?> <?php echo "No result"; ?> <?php } ?> cos its error and output nothing. Or did i do wrong? Link to comment https://forums.phpfreaks.com/topic/226893-ask-codeigniter-empty-array/#findComment-1170921 Share on other sites More sharing options...
mikegeorgeff Posted February 8, 2011 Share Posted February 8, 2011 Try running your foreach loop using the result() function instead of result_array() if(!empty($comment->result())) { foreach ($comment->result() as $comment) { // rest of code here If you read codeigniters user guide you'll find result_array() expects ONLY results as an array so if you have 1 or less results it's going to throw an error. The result() function can handle arrays but doesn't require them Link to comment https://forums.phpfreaks.com/topic/226893-ask-codeigniter-empty-array/#findComment-1171245 Share on other sites More sharing options...
Orasion Posted February 8, 2011 Author Share Posted February 8, 2011 thanks mike, maybe I should read user guide before posting here btw, I follow your suggestion code but have no output with that, I think its still error but then I follow the 2nd example in this page http://codeigniter.com/user_guide/database/results.html and finally showing result that I want Link to comment https://forums.phpfreaks.com/topic/226893-ask-codeigniter-empty-array/#findComment-1171452 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.