0

I am trying to display the value of an array in which is passed from one php page to another php page, but I am getting only last element value. Where as when I checked the array content using print_r() it is showing all the values. This is where I need to the value to be displayed -

<tr class="bgcolor_02">
  <?php
    if(is_array($subject_array))
    {
    foreach($subject_array as $each_subject)
    {
  ?>
  <td width="9%"> <?php echo ucfirst($each_subject['es_subjectname']);?> </td>
  <?php } } ?>
</tr>
<tr>
  <?php
  $inc=0;
  foreach($subject_array as $each_subject)
  {
  ?>
  <td> <?php echo $array_roll[$inc]; ?> </td>
  <?php $inc++;
  }?>
</tr>

And here is the code from where values are fetched and passed -

if(count($errormessage)==0)
{
  $cout=0;
  if(isset($classid))
  {
    $subject_array=$db->getRows("SELECT * FROM es_subject WHERE es_subjectshortname='".$classid."'");
    if(is_array($subject_array))
    {
       foreach($subject_array as $each_subject)
       {
      $array_subject[$each_subject['es_subjectid']]=$each_subject['es_subjectname'];

      $sub_id=$each_subject['es_subjectid'];
      $roll_array=$db->getRow("SELECT es_exam_detailsid FROM es_exam_details WHERE academicexam_id='".$exam_id."' AND subject_id='".$sub_id."'");               
          $array_roll[$cout]=$roll_array['es_exam_detailsid'];
      print_r($array_roll[$cout]."  ");
    }
      }
   }
}

By this code I am getting Output like this
ENGLISH GEOGRAPHY ACCOUNTANCY COMPUTER ECONOMICS
33
Where as output must be
ENGLISH GEOGRAPHY ACCOUNTANCY COMPUTER ECONOMICS
26            27                   31                       32                 33
I am able to see the values on top of the page due to print_r(); which is like this
26 27 31 32 33

Need some help on this problem why I am not getting all values

1
  • Protip: you can use the {} button in the Post Editor to post the output text as well. Saves you some time messing with &nbsp;s :) Commented Dec 13, 2013 at 19:16

2 Answers 2

1

Had to tell exactly what is going on because of poor code formatting and non-meaningful variable names, but I am guessing your problem might lie here:

$array_roll[$cout]=$roll_array['es_exam_detailsid'];

You never change the value for $cout, so you are just continually overwriting the value and array index position 0.

Sign up to request clarification or add additional context in comments.

5 Comments

BrantThank you very much showing me my silly mistake. It works I can't vote you up because of less reputation. But surely I will accept your reply. Thank you again
ON localhost while testing its work but now I am getting warning `Warning: Invalid argument supplied for foreach() in /home/cbsecop2/public_html/manazeschool.com/software/office_admin/templates/import_marks.tpl.php on line 121 ------ SAME CODE NO CHANGE onlu cout++ added
@user3100533 it probably means you don't have an iterable object/array that you are trying to work with in foreach.
Give me some clue how to come out of this ?
@user3100533 I have no idea what line of code that is or what value you get for the item trying to be iterated (you need to var_dump or similar to determine that). This is just basic debugging that you should gain familiarity with how to do.
0
foreach($subject_array as $each_subject)
       {
      $array_subject[$each_subject['es_subjectid']]=$each_subject['es_subjectname'];

      $sub_id=$each_subject['es_subjectid'];
      $roll_array=$db->getRow("SELECT es_exam_detailsid FROM es_exam_details WHERE     academicexam_id='".$exam_id."' AND subject_id='".$sub_id."'");               
          $array_roll[$cout]=$roll_array['es_exam_detailsid'];
      $cout++; //HERE YOU MUST HAVE++
    }

AND will be good.

6 Comments

I have done this, it works on local testing, but after uploading giving warning and no numbers Warning: Invalid argument supplied for foreach() in /home/cbsecop2/public_html/manazeschool.com/software/office_admin/templates/impo‌​rt_marks.tpl.php on line 121 this error is in 2nd foreach($subject_array as $each_subject)
It worked perfectly on localhost I tested and then I accepted, as you also suggested same thing and really it worked. But after uploading it on server showing error
Where in 1st part of the code or in 2nd part of the code shown above
try $subject_array = array(); it will help
@serigo No effect I tried this <?php for($i=0;i<$inc;$i++) { ?> <td> <?php echo $array_roll[$i]; ?> </td> <?php `}?> Output came without warning BUT Browser hangs and throw me out on login page
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.