1

I have a codeigniter view where "echo" statement is not displaying the output - this happens only from a specific loop that I am giving below. I have tried to run it through a sample for loop with echo statement inside; it works without issues. In the code block below $results is an array returned from the controller. I have tried writing the values of the array to a file and they get written correctly.

foreach ($results as $row) {
  fwrite($myfile,$row[0] . PHP_EOL); //this writes perfectly fine
  echo "<tr><td>TEst . $row[0] </td>";  //this doesnt show up
 }

i have tried other threads in this forum with subject "echo not working" - they do not match my peculiar case. Any help is appreciated

6
  • try it: echo "<tr><td>TEst ". $row[0]." </td>"; Commented Feb 10, 2016 at 5:57
  • ist of all print echo "<tr><td>TEst . $row[0] </td>"; before fwrite($myfile,$row[0] . PHP_EOL); maybe it happened due to fwrite(); Commented Feb 10, 2016 at 6:03
  • print_r($row); inside the loop and check the indexes... Commented Feb 10, 2016 at 6:13
  • @Prasad, answers/suggestions provided should work unless your php script is invoked in a way we don't know yet. Can you provide us further details on how you called the script above (i.e, form submission, ajax)? Or can you simply do for loop with a sample echo string? Or how about echo count($results) to check if your results contain something. Commented Feb 10, 2016 at 6:59
  • on my page load, there is a ajax call to database to retrieve timesheet for yesterday (by default,yesterday's date). The requirement is to load the rows in a table format in the view. THis is what I am trying to do. While trying to iterate the loop, I find that echo is not displaying but writing to a file works. Commented Feb 10, 2016 at 7:16

3 Answers 3

1

Try this...

   foreach ($results as $row) {
     fwrite($myfile,$row[0] . PHP_EOL);
     echo '<tr><td>TEst'.$row[0].'</td></tr>';
 }
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry...didnt work. What I found is instead of foreach, if I try numbered loop, I get an error saying Undefined offset: 0. I am sure, index 0 is available, as proved by fwrite.
try foreach for key value pair, foreach($results as $key=>$value){} Then check values of $key and $value, and according to that make changes.
I tried: foreach($results as $key=>$value) { echo "<tr><td>TEst ".$value[0]."</td>"; //doesnt show up fwrite($myfile,$value[0] . PHP_EOL); //works fine }
foreach($results as $key=>$value){ echo $key."<br>"; echo $value; } Run this code and check what it print. It will give you some idea.
Strange ! it still doesnt echo anything. But, when I try to do a fwrite, it writes the key as 0 and 1 !!
|
0

WAS:

echo "<tr><td>TEst . $row[0] </td>";

IS:

echo "<tr><td>TEst ".$row[0]."</td>";

If that doesn't work, you need to find out what your array really has in it, because it means there is no 0 key. Try,

print_r($row);

4 Comments

I thought, that this dot could be an issue - unfortunately, even if I include it, it didnt work. additionally, I found that when I try to access the array by index 0, it throws an error !
print_r $row; or print_r($row);??
its little wiered ! print_r also doesnt show up anything. I am sure, the array has data as proved by fwrite statements !
well if print_r (...); doesn't reveal anything then the variable ain't no array
0

try

if($results){
foreach ($results as $row) {
  fwrite($myfile,$row[0] . PHP_EOL);

//try this
 echo "<tr><td>TEst ".$row[0]."</td></tr>";

//or this
echo "<tr><td>TEst ".$row->keyname."</td></tr>";

//or this
echo "<tr><td>TEst ".$row['keyname']."</td></tr>";   
 }
}
else
{
echo "NO RESULTS";
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.