1

Im trying to get results from a mysql database to properly format into columns, but cant figure out how to do it. The following is how i need it to look.

<div class="box">
<div class="row">
    <input name="1" type="radio" id="1" class="radio" />
    <label for="1">Result 1</label>
</div>
<div class="row">
    <input name="3" type="radio" id="3" class="radio" />
    <label for="3">Result 3</label>
</div>

<div class="box">
<div class="row">
    <input name="2" type="radio" id="2" class="radio" />
    <label for="2">Result 2</label>
</div>
<div class="row">
    <input name="4" type="radio" id="4" class="radio" />
    <label for="4">Result 4</label>
</div>

At the moment i can only get it to go into one column using the following:

<div class="box">
<?php
    foreach($roles as $row){
        echo '<div class="row">';
        echo '  <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />';
        echo '  <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
        echo '</div>';
    }
?>

Any help would be great.

Cheers

6
  • Can you be more specific? I want to help you but I need an example database scheme (not your real one) and exactly what output would you like to have for your example, this would help me understand your question and will gladly write you an answer. Commented Nov 9, 2010 at 1:19
  • Hey Lanjos, im using CodeIgniter as a base, to get all the results im loading a function which has the following code function get_roles() { $query = $this->db->get($this->table_name); return $query->result(); } Commented Nov 9, 2010 at 1:21
  • The exact output i need is the first 2 code blocks i posted ... thats how i need the source to look once outputted Commented Nov 9, 2010 at 1:22
  • Each <div class="box"></div> is a column ... I need 2 columns but the results to be split between the columns if that makes sense, so if there are 4 results, there are 2 in each div Commented Nov 9, 2010 at 1:24
  • Could you make the title more descriptive.. doesn't really relate to the specific issue of creating a two column output. Commented Nov 9, 2010 at 1:30

2 Answers 2

1

You should use an if statement to check either the key is odd or even number. It's not a very clean solution, but:

<?php
    foreach($roles as $row){
        if($row->key%2 == 1) {
            echo '<div class="row">';
            echo '  <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />';
            echo '  <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
            echo '</div>';
        } 
    }
    echo "</div><div class=\"box\">";
    foreach($roles as $row) {
        if($row->key%2 == 0) {
            echo '<div class="row">';
            echo '  <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />';
            echo '  <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
            echo '</div>';
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Here's an idea (not factored, and I'm trying to write the most illustrative code I can...) Basically, over each row, increment $i by one. If we have two rows, close the div, and open a new div on the next iteration.

<?php
    $i = 0;
    $output = "";
    foreach($roles as $row){
        if($i > 1) {
           $i = 0;
        }
        if($i == 0) {
           // Open the div
           $output .= "<div class='box'">;
        }
        $output .= '<div class="row">';
        $output .= '  <input name="_'.$row->key.'" type="radio" id="'.$row->key.'" class="radio" />';
        $output .= '  <label for="'.$row->key.'" style="text-transform: lowercase;">'.$row->name.'</label>';
        $output .= '</div>';

        if($i == 0) {
            // Close the div
            $output .= "</div>";
        }

        $i++;
    }
    if($i != 1) {
        // We need an extra one here, just in case the loop finished on 
        // an odd number.
        $output .= "</div>";
    }
    echo $output;
?>

6 Comments

Hey, Thanks for this response ..... It works ok, except when it outputs it misses the <div class='box'> on the second column
The output looks like this: <div class='box'> <div class="row"> <input name="_wfa" type="radio" id="wfa" class="radio" /> <label for="wfa" style="text-transform: lowercase;">Actionscripter</label> </div> </div> <div class="row"> <input name="_AP1" type="radio" id="AP1" class="radio" /> <label for="AP1" style="text-transform: lowercase;">art director</label> </div> </div>
Whoops! Left a bug in there, cut-and-pasted from the wrong window when I wrote the comment. However, looking at what you were after again - this isn't the solution. I misread your initial question. Sorry! :)
Is there no way we can put the tag in there?
Actually, wait on. Your question specifies that the first row should be in a box, and the second should not. The third should, the fourth should not, and so on. I've edited my submission and the output I get is:
|

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.