Jump to content

Recommended Posts

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

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?

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

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

 

:)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.