Jump to content


Photo

Displaying Mysql data as checked checkboxes


  • Please log in to reply
4 replies to this topic

#1 DjNaF

DjNaF
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 18 March 2006 - 11:33 PM

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--]

#2 DjNaF

DjNaF
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 19 March 2006 - 11:33 PM

No one to help please? :(

#3 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 March 2006 - 12:32 AM

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

<?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>";
}


#4 DjNaF

DjNaF
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 20 March 2006 - 01:02 AM

I am really thankful hitman6003

how can i put value for the check box?
value="CardID"

can u give me the update for the upper code please

please do u have msn messenger to contact you?


#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 20 March 2006 - 01:54 AM

I think this will give you what you want:

<?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>";
}

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:
Array
(
    [cards] => Array
        (
            [Applebees] => Array
                (
                    [1] => on
                )

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

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

        )

    [submit] => submit
)

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users