0

I have a for loop which runs upto 50 times. In my codeigniter view I have

Year          Id
2015          1000001
2015          1000002

I have get database value using the following command

$data['get_data']=$this->get_data_model->getSeating();

I want to set the database value into sno, year, id in the view. But my database has only 2 columns.

    <?php $i=1; if(@$get_data)
      {
          foreach($get_data as $row):       
    ?>
    <tr>
        <td align="center" height="35"><?=$i?></td>
        <td align="left"><?=$row->year?></td>
        <td align="left"><?=$row->id?></td>
    </tr>
    <?php
      $i++;
      endforeach;
     }

This will shows only 2 rows. Because my database has two values only. But I need to allocate 50 rows even if the data is not in database.
e.g In my example i want to show in my view

Normally it shows like this

Sno    Year    ID
1       2015   100001
2       2015   100002

But I want to show like this

Sno    Year    ID
1       2015   100001
2       2015   100002
3
4
5
......
50
7
  • 2
    So, what is the issue? Do you get any error messages? Commented Mar 2, 2015 at 9:43
  • 1
    I didn't get errors, but I want to show like the above view. Commented Mar 2, 2015 at 9:45
  • 1
    SO is not a coding service, try something and we will help you Commented Mar 2, 2015 at 9:52
  • 1
    Try with for loop instead of foreach. Commented Mar 2, 2015 at 9:52
  • 1
    typical use of for loop instead of foreach loop.. remember? ;) Commented Mar 2, 2015 at 9:55

3 Answers 3

1

Here's the basic idea (you could put them in one loop if you like):

<?php 
$i=1; 
if(@$get_data) {
  foreach($get_data as $row):       
?>
    <tr>
      <td align="center" height="35"><?=$i?></td>
      <td align="left"><?=$row->year?></td>
      <td align="left"><?=$row->id?></td>
    </tr>
<?php
    $i++;
  endforeach;
}
  for($j=$i; $j<51;$j++): 
?>
    <tr>
      <td align="center" height="35"><?=$j?></td>
      <td align="left"></td>
      <td align="left"></td>
    </tr>
<?php
  endfor;
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

How about like this :

<?php for($x=1; $x<51; $x++){?>
    <tr>
        <td align="center" height="35"><?=$x?></td>
        <td align="left"><?=$get_data[$x]->year?></td>
        <td align="left"><?=$get_data[$x]->id?></td>
    </tr><?php } ?>

2 Comments

will result in error if $get_data[$x] doesn't have a member set. Better check it before displaying.
Use isset function to preventing errors like that isset($get_data[$x])
0

Instead of using a foreach loop you should use a for instead. Something like this:

<?php
for(i=0;i <= 50;i++){
?>
  <tr>
    <td align="center" height="35"><?=$i?></td>
    <td align="left"><?=$row[$i]->year?></td>
    <td align="left"><?=$row[$i]->id?></td>
</tr>
<?php
}
?>

Also you should check if $i is a valid index for $row:

isset($row[$i]) ? <td align="left"><?=$row[$i]->year?></td> : '';

Comments

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.