3

I was hoping somebody could help me with this. I'm using codeigniter as my framework. In one of my views, I'd like to output as many tabs as the number in "bestoutof" column in my database. The code I tried is:

<?php for ($x = 1; $x = $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>

But it doesn't seem to work. I'd greatly appreciate any help on this.

Thank you!

3
  • What does it do? Any errors? What is the value of $post->bestoutof? Commented Jan 30, 2016 at 7:33
  • $x = $post->bestoutof; ??. Loop will not execute if the "bestoutof" is not 1. Use $x <= $post->bestoutof; Commented Jan 30, 2016 at 7:35
  • @Peter what is the value of $post->bestoutof? Commented Jan 30, 2016 at 7:37

3 Answers 3

2

For Id inside the for loop you need to use $x as like this example:

Modified code:

<?php
for ($x = 1; $x = $post['bestoutof']; $x++) { 
    ?>
    <div class="tab-pane fade active in" id="game<?php echo $x;?>"></div>
    <?php
}
?>

Note: If $post['bestoutof'] have multiple values, then $x = $post['bestoutof'] value should be added less-than mark $x <= $post['bestoutof']. This will depend with what the value you have on it

What's wrong in your code you are using $post->bestoutof for every iteration. This will return you same game Id not changed.

And do not use php tag inside the php tag.

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

Comments

1

You have PHP tags inside PHP tags. You're going to get your code output as a literal string. As @devpro pointed out, you probably want to use the counter for your id otherwise, you'll have duplicate ids.

<?php for ($x = 1; $x = $post->bestoutof; $x++) {
  echo '<div class="tab-pane fade active in" id="game'. $x .'"></div>'; 
} ?>

Comments

0

$x = $post->bestoutof; This decides the number of iteration.

Actually it should be $x <= $post->bestoutof;

<?php for ($x = 1; $x <= $post->bestoutof; $x++) {
echo '<div class="tab-pane fade active in" id="game<?php $post->bestoutof; ?>"></div>'; } ?>

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.