DjNaF Posted March 18, 2006 Share Posted March 18, 2006 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 , 2that mean the restaurant with the ID 1 , accepts Visa and MasterCardi manage to save the data from checkboxes into Mysqlbut i added an option to edit Restaurant Accepted Cardsso 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 CHECKEDand the not accepted card arent checked.this is the code i used to display checkboxes, and waiting for you to help meadd 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--] Quote Link to comment https://forums.phpfreaks.com/topic/5260-displaying-mysql-data-as-checked-checkboxes/ Share on other sites More sharing options...
DjNaF Posted March 19, 2006 Author Share Posted March 19, 2006 No one to help please? :( Quote Link to comment https://forums.phpfreaks.com/topic/5260-displaying-mysql-data-as-checked-checkboxes/#findComment-18909 Share on other sites More sharing options...
hitman6003 Posted March 20, 2006 Share Posted March 20, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/5260-displaying-mysql-data-as-checked-checkboxes/#findComment-18923 Share on other sites More sharing options...
DjNaF Posted March 20, 2006 Author Share Posted March 20, 2006 I am really thankful hitman6003how can i put value for the check box?value="CardID"can u give me the update for the upper code pleaseplease do u have msn messenger to contact you? Quote Link to comment https://forums.phpfreaks.com/topic/5260-displaying-mysql-data-as-checked-checkboxes/#findComment-18932 Share on other sites More sharing options...
hitman6003 Posted March 20, 2006 Share Posted March 20, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/5260-displaying-mysql-data-as-checked-checkboxes/#findComment-18941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.