0

I want to write out details from MySQL database to checkboxes. And the checkboxes structure should look like 6*6 square. ( 6 in one row, 6 in one column).

MySQL database looks like :

|id|package|
|1 |HEI    |
|2 |TAG    |
|...|

I wrote this function (in php)

function getPackages3()
{
    $con = openLockMySQLdb('labcom', 'packages', 'READ');

    $q = mysql_query("SELECT id, package FROM packages ORDER BY package ASC", $con);
    while($row = mysqli_fetch_array($q)) {
        $packages .= '<ul>' . '<li>' . '<label>' . '<input type="checkbox" class="packages_2" id="tab_packages" name="' . $row['package'] . '" value="' . $row['id'] . '-' . $row['package'] . '">' . '<p class="checkbox_label">' . $row['package'] . '</p>' . '</label>' . '</ul>' . '</li>';
    }

    closeUnlockMySQLdb($con);

    return $packages;
}

to get in checkboxes, it's working but i can't but 6 in one row and 6 in one column. So i can't make a square from the checkboxes.

2
  • you're mixing ´mysql_` with mysqli_! And of course you should not use mysql_ at all. Commented Jun 23, 2016 at 15:07
  • </ul> and </li> wrong order at the end. Commented Jun 23, 2016 at 15:11

1 Answer 1

1

I suppose your database table contains 6 * 6 = 36 entries of packages, right? Then the only problem is to have a kind of 'line feed' after every 6th entry.

I'd prefer a CSS solution in this case and use the nth-of-type() selector. But you need to move the <ul> tag out of the loop:

function getPackages3()
{
    $con = openLockMySQLdb('labcom', 'packages', 'READ');

    $q = mysql_query("SELECT id, package FROM packages ORDER BY package ASC", $con);
    $packages = '<ul>';
    while($row = mysqli_fetch_array($q)) {
        $packages .= '<li>' . '<label>' . '<input type="checkbox" class="packages_2" id="tab_packages" name="' . $row['package'] . '" value="' . $row['id'] . '-' . $row['package'] . '">' . '<p class="checkbox_label">' . $row['package'] . '</p>' . '</label></li>';
    }
    $packages .= '</ul>';

    closeUnlockMySQLdb($con);

    return $packages;
}

The CSS would then be like that:

li {
    float: left;
}
li:nth-of-type(6n) {
    clear: left;
}

I kept your calls to the database functions even if they use mixed libraries (see comments from @jeff) because it is a different problem, but to make it work, you have to fix this as well.

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

3 Comments

I fixed the syntax problems (comments from @jeff) . And tried to use your solution. Now putting in one <ul> all <li> . But i want 6 <ul> which contains 6 <li> per each <ul> . BTW thanks for helping!
Ok, then it's even easier. Just keep the li { float: left; }' style and put the <ul>` tags back in the while loop again.
I solved it . Without li:nth-of-type(6n) { clear: left; } I made a 6*6 with css. Thanks for help.

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.