0

I have a rather long code, so i think the best is I post it here with some additional comments as questions inside of it:

<div class="contentRow">

    <h2>Title</h2>

    <div id="frame">

        <?php

        //Get files
        $sql->query("SELECT * FROM files WHERE job = 'true'");
        $count  = count($sql->get());

        //No files to display
        if ($count == "0"){

            echo "<div class='box red'><p>No jobs found.</p></div>";

        } else{

            $sql->query("SELECT * FROM files WHERE job = 'true'");

            $i=0;

            foreach($sql->get() as $result){

                $i++;

        ?>

        <!-- Here I start printing the results and here the problems begin -->



        <!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
        <?php if ($i % 4 == 0){ ?>
        </div><!-- CLOSE jobsContainer -->
        <?php } ?>

        <?php if ($i % 4 == 0 OR $i == 1){ ?>
        <div class="jobsContainer"><!-- OPEN jobsContainer -->
        <?php } ?>


            <!-- Print the .jobContainer -->
            <div class="jobContainer">
                content + table + form
            </div>

            <!-- My goal here: Print .jobSeperator after every 2nd  .jobContainer -->
            <?php if ($i % 2 == 0){ ?>
            <div class="jobSeperator">&nbsp;</div>
            <?php } ?>


        <!-- CLOSE loop -->
        <?php  } }?>


    </div><!-- jobsFrame -->

    <div class="clear"></div>

</div><!-- contentRow -->

The desired output should look like this:

enter image description here

My problem is following:

It seems that there are only 3 elements included in the first container, but after that there are 4. Therefore also the seperators are kinda messed up and it looks like this:

enter image description here

It seems that the problem is not applying to the .jobSeperator, as it does always get included after every 2th container, however it gets messed up too, because of the fact that in the first .jobsContainer there are just 3 sub containers.

I dont get why in the first .jobsContainer there are just 3 sub containers, but from than on there are, as desired, 4, but probably it is obvious and I just dont see it...

p.s. If I set the code to print 5 .jobContainer inside one .jobsContainer, than there are printed 4 in the first .jobsContainer and from than on 5 in every following .jobsContainer

14
  • 1
    You should consider using a template engine. Your code is hard to read. Commented May 29, 2012 at 9:17
  • True, however this is the only part of the current page that is a little complex, therefor using a template engine is a bit of a overkill. I have tried to make it easy to understand what I am trying to do by drawing this wonderful pictures :) Commented May 29, 2012 at 9:19
  • @mgraph: it shouldnt. it should be 4 .jobContainer inside one .jobsContainer (see the s) Commented May 29, 2012 at 9:21
  • where is the end of <div class="jobsContainer"> i mean </div> Commented May 29, 2012 at 9:22
  • Increment at the end, or $i will soon be 1 at first round Commented May 29, 2012 at 9:23

4 Answers 4

1

First of all, you should not keep your SQL logic mixed with HTML.

Anyway. The idea is that you need to output a separator after each second item. The testcase i would look at would be like this.

1 2
---
3 4
***
5 6
---
7

And the code for writing it would be:

$i = 0;
foreach( /* some array */ as $data ){

    $i++;
    if ( ($i - 1) % 2 === 0 ){

        if ( ($i - 1) % 4 === 0 ){

            echo '<br />***<br />';
        } else {

            echo '<br />---<br />';
        }            
    }

    echo $i, ' ';
}

This would reproduce the structure above (never tested).

As for the separation of layout and rest of application logic, you might look in to this article. It will show a quite simple way, without a need of additional 3rd party libraries.

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

5 Comments

A little off topic but you can probably write your foreach like: foreach ( $someArray as $i => $data ) (assuming it is numerically indexed and non-sparse). Then your $i value is incremented automatically.
@Okonomiyaki3000 , only that sort of information was not given in topic.
This seems to work how i intended it. I just have to adapt it to my example and will mark this answer asap.
Following your exmaple and adding one modification to display the closing div only after every 4th element, but display the opening div for the first element too, everything works now :)
@tereško Sorry, I meant that my comment was off topic. More of an aside or tip.
1

try to change these lines:

    <?php if ($i % 4 == 0 OR $i == 1){ ?>
         <div class="jobsContainer"><!-- OPEN jobsContainer -->
    <?php } ?>

to

    <?php if ( ($i-1) % 4 == 0 ){ ?>
         <div class="jobsContainer"><!-- OPEN jobsContainer -->
    <?php } ?>

Now you open container at the begginning of 1st, 4th, 8th, 12th,... iteration, after this change you should open container before 1st, 5th, 9th, 13th,... iteration which should fix the problem. And you will get rid of ugly or. It seems this is the only bug, just try it.

I also suggest separating application logic and presentation.

Comments

0

I believe You're making one small mistake.

<!-- My goal here: Print n .jobsContainer with exactly 4 .jobContainer inside -->
<?php if ( ($i+1) % 4 == 0){ ?>
</div><!-- CLOSE jobsContainer -->
<?php } ?>
<?php if ( ($i+1) % 4 == 0 OR $i == 1){ ?>
<div class="jobsContainer"><!-- OPEN jobsContainer -->
<?php } ?>

Use ($i+1) instead of $i in this bit.

2 Comments

It seems did does only put 2 items in the first jobsContainer and than 4 in every following one.
So you just need a logic for the first jobsContainer now?
0

If you inspect the generated HTML output, you'll see that when $i == 4, you first close the jobsContainer div before outputting the 4th jobContainer. You need to change the order of output to something like this:

EDIT: seems like you should split up the start of the first jobsContainer ($i == 1)

<div class="contentRow">
    <h2>Title</h2>
    <div id="frame">
        <?php
        $i = 0;
        for($j = 0; $j < 12; $j++)
        {
            $i++;
            ?>
            <?php if ($i == 1) { ?>
                <div class="jobsContainer">
            <?php } ?>

            <div class="jobContainer">job</div>

            <?php if ($i % 4 == 2) { ?>
                <div class="jobSeperator">&nbsp;</div>
            <?php } ?>

            <?php if ($i % 4 == 0) { ?>
                </div>
                <div class="jobsContainer">
            <?php } ?>

            <?php 
        }
        ?>
        <?php if ($i > 0) { ?>
            </div>
        <?php } ?>
    </div>

    <div class="clear"></div>
</div>

2 Comments

This reproduces the same problem I have: Look @ codepad and you see the first jobsContainer contains only 3 items.
I have splitted up the first and next jobsContainer start (answer contains updated code).

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.