Jump to content

Displaying Mysql data as checked checkboxes


DjNaF

Recommended Posts

Hello;

i am having a problem in displaying data from Mysql in checkboxes.
I got the following tables:

* Cards_table: ( CardID , CardName )
for example... 1 , Visa
2 , MasterCard
3 , Amex

* Restaurants_Cards: ( RestaurantID , CardID )\
For example... 1 , 1
1 , 2
2 , 1
3 , 3
4 , 2
that mean the restaurant with the ID 1 , accepts Visa and MasterCard
i manage to save the data from checkboxes into Mysql

but i added an option to edit Restaurant Accepted Cards
so i want a want to display the cards names as check boxes ( which i already did)
and put the already accepted cards in Restaurants_Cards table as CHECKED
and the not accepted card arent checked.

this is the code i used to display checkboxes, and waiting for you to help me
add on it, to show the already accepted cards as checked in the checkbox.

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--] $query="select CardID, CardName from cards_tbl";
$result=mysql_query($query);

[!--coloro:#999999--][span style=\"color:#999999\"][!--/coloro--]//$rescardq="select CardID from restcards_tbl where RestID=$restid";[!--colorc--][/span][!--/colorc--]
[!--coloro:#999999--][span style=\"color:#999999\"][!--/coloro--]//$rescardsr=mysql_query($rescardq);[!--colorc--][/span][!--/colorc--]

while($array= mysql_fetch_array($result))
{

echo "<input type='checkbox' name='card[]' value='$array[CardID]'>$array[CardName]<br>\n";
}[!--colorc--][/span][!--/colorc--]
Link to comment
Share on other sites

This should work. Make sure to read through it carefully and adjust names and such where needed.

[code]<?php

$query = "SELECT * FROM Cards_table";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
    $cards[] = $row['CardName'];
}

$query = "
    SELECT Restaurants.RestaurantName, Cards_table.CardName
    FROM Restaurants_Cards
        LEFT JOIN Restaurants ON Restaurants_Cards.RestaurantID = Restaurants.RestaurantID
        LEFT JOIN Cards_table ON Restaurants_Cards.CardID = Cards_table.CardID";
        
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $rname = $row['RestaurantName'];
    $cname = $row['CardName'];
    $accepted[$rname][] = $cname;
}

echo "
    <table>
        <tr>
            <th>Restaurant Name:</th>
            <th>Cards</th>
        </tr>";
foreach ($accepted as $key => $value) {
    echo "
        <tr>
            <td>$key</td>
            <td>";
    foreach ($cards as $card) {
        echo "<input type=\"checkbox\" name=\"$key-$card\" ";
        if (in_array($card, $value) {
            echo "checked";
        }
        echo "> $card ";
    }
    echo "</td>
        </tr>";
}[/code]
Link to comment
Share on other sites

I think this will give you what you want:

[code]<?php

$query = "SELECT * FROM Cards_table";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
    $id = $row['CardID'];
    $cards[$id] = $row['CardName'];
}

$query = "
    SELECT Restaurants.RestaurantName, Cards_table.CardID
    FROM Restaurants_Cards
        LEFT JOIN Restaurants ON Restaurants_Cards.RestaurantID = Restaurants.RestaurantID
        LEFT JOIN Cards_table ON Restaurants_Cards.CardID = Cards_table.CardID";
        
$result = mysql_query($query) or die(mysql_error());

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $rname = $row['RestaurantName'];
    $cid = $row['CardID'];
    $accepted[$rname][] = $cid;
}

echo "
    <table>
        <tr>
            <th>Restaurant Name:</th>
            <th>Cards</th>
        </tr>";
foreach ($accepted as $key => $value) {
    echo "
        <tr>
            <td>$key</td>
            <td>";
    foreach ($cards as $cid => $card) {
        echo "<input type=\"checkbox\" name=\"cards[$key][$cid]\" ";
        if (in_array($cid, $value) {
            echo "checked";
        }
        echo "> $card ";
    }
    echo "</td>
        </tr>";
}[/code]

It posts differently...do a print_r to see how it does it.

You should end up with a sub array of $_POST called cards. The key values of cards should be the restaurant names, and should consist of another sub array that has the card id's as the key values and will contain the string "on" if the box is checked, or be empty if the box is not checked...

Something like this:
[code]Array
(
    [cards] => Array
        (
            [Applebees] => Array
                (
                    [1] => on
                )

            [Chilis] => Array
                (
                    [1] => on
                    [2] => on
                )

            [Outback] => Array
                (
                    [2] => on
                )

        )

    [submit] => submit
)[/code]

Where 1 is the id for visa and 2 is the id for master card. From this you can see that Chilis accepts both cards, but Outback only accepts master card, and Applebees only Visa.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.