Home » Php » help with foreach loop that builds a recordset access database – PHP

help with foreach loop that builds a recordset access database – PHP

Posted by: admin February 23, 2020 Leave a comment

Q(Question):

So as everyone else I am new to PHP and I have been trying to create an amenity page for a B&B. I need folk to be able to select checkboxes and it fetches the info from the database.
I posted the checkbox list to the ampage and now I need to search the database for the matching records.
so I know I need to write

foreach ($_POST['amenty'] as $value) {
$breakfast = what
$bbq
$kitchenette
}

can someone check to see if I wrote my data base correctly

AMENITY CHECKBOXES

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>B and B Association of San Juan Amenities Page</title>
<link href="scripts/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="mainContent" >
<h1 class="headersTitles">Bed and Breakfast Amenities</h1>
<div id="mainArticle">
<p>&nbsp;</p>
<p class="mainTextLight">Check the Amenities you are interested in and we will show you the Inns with the features you desire.</p>
<p class="innTitleLightLeft">&nbsp;</p>
<p class="innTitleLightLeft">&nbsp;</p>
<form id="form1" name="form1" method="post" action="ampage.php">
<div id="amenities1">
<p>
<input name="amenity[]" type="checkbox" id="amenity_breakfast" value="breakfast" />
<label for="">Full Breakfast</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_bbq" value="bbq" />
<label for="amenity[]">BBQ Available</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_kitchenette" value="kitchenette" />
<label for="amenity[]">Kitchenette</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_fireplace" value="fireplace" />
<label for="amenity[]">Fireplace</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_internet" value="internet" />
<label for="amenity[]">Internet Access/ WiFi</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_tv" value="tv" />
<label for="amenity[]">Television</label>
</p>
</div>
<div id="amenities2">
<p>
<input name="amenity[]" type="checkbox" id="amenity_dvd" value="dvd" />
<label for="amenity[]">DVD / VCR</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_video" value="video" />
<label for="amenity[]">Videos</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_bath" value="bath" />
<label for="amenity[]">Private Bath</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_jacuzzi" value="jacuzzi" />
<label for="amenity[]">Jacuzzi / Jet Tub</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_hotub" value="hotub" />
<label for="amenity[]">Hot Tub</label>
</p>
</div>
<div id="amenities3">
<p>
<input name="amenity[]" type="checkbox" id="amenity_massage" value="massage" />
<label for="amenity[]">Massage Available</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_handicap" value="handicap" />
<label for="amenity[]">Handicapped Access</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_child" value="child" />
<label for="amenity[]">Child Friendly</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_pickup" value="pickup" />
<label for="amenity[]">Pickup Services</label>
</p>
<p>&nbsp;</p>
<p>
<input name="amenity[]" type="checkbox" id="amenity_pet" value="pet" />
<label for="amenity[]">Pet Friendly</label>
</p>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </p>
<p>
<input name="Search" type="submit" value="Search" tabindex="170"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input name="start" type="submit" value="Start Over" tabindex="180" />
</p>
</form>
</div>
</div>
</body>
</html>

AMPAGE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>checkboxes</title>
<link href="scripts/main.css" rel="stylesheet" type="text/css" />
<style type="text/css"></style>
</head>
<body>
<?php
//not sure what I am doing here I know I need to create a loop with a foreach
foreach ($_POST['amenity'] as $value) {
$breakfast;
}
mysql_connect($read, $write);
mysql_select_db($write) or die("Unable to select database");
$select_query="select am_id from amenities" . $POST;
?>
<div id="mainArticle">
<?php
// here are the image and text that needs to come back for each inn
$innID=mysql_result($result, "am_id");
$image1=mysql_result ($result, $row = "image1");
?>
<div id="mainArticleInn3">
<div id="inn3a"><a href = "<?php echo $row['url']; ?>" target="_blank"><img src="innpg/<?php echo $row['image1']; ?>" width="200" height="150" /></a></div>
<div class="lightText" id="innText3">
<p class="innTitleLight"><a href="<?php echo $row['url']; ?>" target = "_blank"> <?php echo $row['bbname']; ?></a></p>
<p class="innPhoneLight"><?php echo $row['phone']; ?></p>
<p class="innPhoneLight"><?php echo $row['tollFree']; ?></p>
<p class="innPhoneLight"><?php echo $row['fax']; ?></p>
<p class="innWebLight"><a href = "<?php echo $row['url']; ?>" target="_blank"><?php echo $row['web'] ?></a></p>
<p class="innWebLight"><a href="<?php echo $row['emailL']; ?>" target="_blank"><?php echo $row['email'] ?></a></p>
<div id="dateMap3"><a href="http://bytes.com/submit/calendar/calendar.html"><img src="logoMB /checkDateW.gif" alt="Check Date" width="60" height="40" align="left" /></a><a href="<?php echo $row['map']; ?>"><img src="logoMB /showOnMapW.gif" alt="Show on Map" width="60" height="40" align="right" /></a></div>
</div>
<div id="inn3b"><a href="<?php echo $row['url']; ?>" target="_blank"><img src="innpg/<?php echo $row['image2']; ?>" width="200" height="150" /></a></div>
<div id="innRating3"><img src="logoMB /<?php echo $row['logo1']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo2']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo3']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo4']; ?>" width="60" height="55" /></div>
<div class="InformationTextLight" id="innMainText3"><?php echo $row['description']; ?>
</div>
<p class="lightText">&nbsp;</p>
</div>
<form id="form1" name="form1" method="post" action="">
<input type="submit" name="start" id="start" value="Start Over" />
</form>
</div>
</body>
</html>

DATABASE

-- phpMyAdmin SQL Dump
-- version 3.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 11, 2011 at 04:59 PM
-- Server version: 5.1.44
-- PHP Version: 5.3.2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sjibb_test`
--
-- --------------------------------------------------------
--
-- Table structure for table `amenities`
--
CREATE TABLE `amenities` (
`am_id` int(11) NOT NULL,
`image1` varchar(50) NOT NULL,
`bbName` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
`tollFree` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`web` varchar(50) NOT NULL,
`url` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`emailL` varchar(50) NOT NULL,
`map` varchar(50) NOT NULL,
`image2` varchar(50) NOT NULL,
`logo1` varchar(50) NOT NULL,
`logo2` varchar(50) NOT NULL,
`logo3` varchar(50) NOT NULL,
`logo4` varchar(50) NOT NULL,
`description` varchar(4096) NOT NULL,
`breakfast` varchar(4) NOT NULL,
`bbq` varchar(4) NOT NULL,
`kitchenette` varchar(4) NOT NULL,
`fireplace` varchar(4) NOT NULL,
`internet` varchar(4) NOT NULL,
`tv` varchar(4) NOT NULL,
`dvd` varchar(4) NOT NULL,
`video` varchar(4) NOT NULL,
`bath` varchar(4) NOT NULL,
`jacuzzi` varchar(4) NOT NULL,
`hotub` varchar(4) NOT NULL,
`massage` varchar(4) NOT NULL,
`handicap` varchar(4) NOT NULL,
`child` varchar(4) NOT NULL,
`pickup` varchar(4) NOT NULL,
`pet` varchar(4) NOT NULL,
PRIMARY KEY (`am_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `amenities`
--
INSERT INTO `amenities` VALUES(1, '../imagesMB/innpg/argyle.jpg', 'ARGYLE HOUSE', '360-378-4084', '800-624-3459', '', 'www.argylehouse.net', 'http://www.argylehouse.net', '[email protected]', '[email protected]', '../maps/Argyle.html', '../imagesMB/innpg/argyle1.jpg', '../imagesMB/logoMB/threedoor.gif', '', '', '', 'Enjoy the charm and character of this 1910-vintage Craftsman located only two blocks from downtown and a short walk from the ferry. Nestled on beautiful grounds to ensure the peace and quiet of a country setting. Comfortable well appointed rooms with in-room TV/DVD, plus hot tub, delicious hot breakfast every day, and wireless Internet access. Children and dogs are welcome here! ', 'yes', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'no', 'yes', 'yes', 'yes');
INSERT INTO `amenities` VALUES(2, '../imagesMB/innpg/dragonfly.jpg', 'DRAGONFLY INN', '360-378-4280', '877-378-4280', '360-378-1392', 'www.thedragonflyinn.com', 'http://www.thedragonflyinn.com', '[email protected]', '[email protected]', '../maps/dragonfly.html', '../imagesMB/innpg/dragonfly1.jpg', '../imagesMB/logoMB/aaa3D.gif', '../imagesMB/logoMB/threedoor.gif', '', '', 'A unique,Japanese-inspired bed and breakfast, promising a secluded, romantic get away on 15 acres of unspoiled Pacific Northwestern woods. Our tranquil rooms include a queen-size bed,Free Wi-Fi,satellite TV/DVD, pulsating 2-headed shower,oversized 2-person air massage tub, and private patio. Dragonfly''s sumptuous Asian fusion gourmet breakfasts and afternoon treats blend the comfortable and the exotic. No children or pets. ', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenities` VALUES(3, '../imagesMB/innpg/harrison.jpg', 'HARRISON HOUSE', '360-378-3587', '800-407-7933', '360-378-2270 FAX', 'www.harrisonhousesuites.com', 'http://www.harrisonhousesuites.com', '[email protected]', '[email protected]', '../maps/Harrison.html', '../imagesMB/innpg/harrison1.jpg', '../imagesMB/logoMB/threedoor.gif', '../imagesMB/logoMB/nwBest.gif', '../imagesMB/logoMB/kiss2.gif', '../imagesMB/logoMB/mobil.gif', 'A hilltop retreat one block from the ferry landing. Fully furnished private suites with kitchens, harbor views, fireplaces, and private hot tubs - sleep two to ten in comfort. Gourmet breakfast, afternoon time, and dinner delivered to your suite or in our private caf√©. Retreats, family reunions, and weddings. Pet friendly. NW Best Places. Peoples Choice Awards. One of 40 inns throughout the state, chosen to be featured in Special Places. ', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes');
INSERT INTO `amenities` VALUES(4, '../imagesMB/innpg/hillside.jpg', 'HILLSIDE HOUSE', '360-378-4730', '800-232-4730', '360-378-4715 FAX', 'www.hillsidehouse.com', 'http://www.hillsidehouse.com', '[email protected]', '[email protected]', '../maps/Hillside.html', '../imagesMB/innpg/hillside1.jpg', '../imagesMB/logoMB/aaa3D.gif', '../imagesMB/logoMB/threedoor.gif', '../imagesMB/logoMB/kiss2.gif', '../imagesMB/logoMB/mobil.gif', 'Enjoy views of snowcapped Mt. Baker over the blue waters of Friday Harbor from the spacious deck of this contemporary-style inn. Located just 3/4 of a mile from ferries and airport. Our famous homemade Banana Chocolate Chip cookies are always within easy reach. We are triple diamond rated by AAA and triple star rated by Mobil. WIFI Internet access. ', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no');
INSERT INTO `amenities` VALUES(5, '../imagesMB/innpg/intothewoods.jpg', 'INN TO THE WOODS', '360-378-9501', '888-292-9502', '', 'www.inntothewoods.com', 'http://www.inntothewoods.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/intothewoods1.jpg', '../imagesMB/logoMB/threedoor.gif', '', '', '', 'Luxurious amenities and furnishings in this new bed breakfast. Suite includes outdoor private deck with Jacuzzi spa, interior Jacuzzi tub & separate shower. All rooms have private phones, VCR and satellite TV with cordless stereo headphones, guest robes, hair dryers & chocates. Comfortable great room with outdoor deck overlooking lake, Web access, books, magazines, games, movies. Afternoon tea daily. Guest Quarters separated from Innkeeper quarters.', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'yes', 'no');
INSERT INTO `amenities` VALUES(6, '../imagesMB/innpg/kirk.jpg', 'KIRK HOUSE', '360-378-3757', '800-639-2762', '', 'www.kirkhouse.net', 'http://www.kirkhouse.net', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/kirk1.jpg', '', '', '', '', 'Historic Craftsman style home built in 1907 with twelve foot ceilings, wood-wrapped arched doorways, leaded glass and granite fireplaces and also modern conveniences like wireless internet access and TV/DVD in each room. Four rooms, each with private bath and private entry. Morning breakfast and afternoon snacks. Quiet in-town location with an easy walk to shops and restaurants. ', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenities` VALUES(7, '../imagesMB/innpg/olympic.jpg', 'OLYMPIC LIGHTS', '360-378-3186', '888-211-6195', '360-378-2097', 'www.olympiclights.com', 'http://www.olympic.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/olympic1.jpg', '', '', '', '', '1895 Victorian Farmhouse located on the sunny south end, near American Camp (National Park). Peaceful meadow setting on 5 acres with panoramic views of the sea and the Olympic Mountain Range. 4 rooms with private baths - two with water views. Full breakfast served with eggs from our resident hens. 5 1/2 miles from ferry.', 'yes', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenities` VALUES(8, '../imagesMB/innpg/slowSeasons.jpg', 'WHARFSIDE B & B', '', '360-378-5661', '', 'www.slowseason.com', 'http://www.slowseason.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/slowSeasons1.jpg', '', '', '', '', 'Experience Friday Harbor afloat, aboard an enchanting 60 ft. Ketch rigged motor sailor in the tradition of European floating Inns. This elegant vessel has two spacious private romantic staterooms, queen size beds, DVD, WiFi and full gourmet breakfast. Stay with us for your special occasions and experience the islands with a morning cruise to Seal Rock or enjoy relaxing on deck with a breathtaking view of Friday Harbor''s waterfront.', 'yes', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no');
INSERT INTO `amenities` VALUES(9, '../imagesMB/innpg/stateInn.jpg', 'STATES INN AND RANCH', '360-378-6240', '866-602-2737', '360-378-6241 FAX', 'www.stateinn.com', 'http://www.stateinn.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/statesInn1.jpg', '../imagesMB/logoMB/aaa3D.gif', '../imagesMB/logoMB/threedoor.gif', '../imagesMB/logoMB/kiss2.gif', '', 'Rooms and suites on a 60 acre working ranch. All private baths. Feed our sheep and alpacas; visit our chickens. Scratch breakfast with local foods from local farms, eggs from our own pasture raised eggs and our own award-winning jams and jellies. Free WiFi. Comfortable common areas. We welcome reunions and family gatherings. Handicapped units available. Country store features gifts, homemade lunch, fresh bread and local ice cream.', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no');
INSERT INTO `amenities` VALUES(10, '../imagesMB/innpg/trumpeter.jpg', 'TRUMPETER INN', '360-378-3884', '800-826-7926', '360-378-8235 FAX', 'www.trumpeterinn.com', 'http://www.trumpeterinn.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/trumpeter1.jpg', '../imagesMB/logoMB/aaa3D.gif', '../imagesMB/logoMB/threedoor.gif', '../imagesMB/logoMB/kiss2.gif', '', 'Contemporarily furnished Inn on 5 serene acres. Offering more than just beautiful bedrooms & gourmet breakfasts. Personalized vacation planning & WIFI. Pick‚Ä¢up service at ferry/airport. On site private massage studio. Gardens for reading, bird-watching or just relaxing. Soaking tub set under the stars. Two separate den to read or watch TV & movies. Private decks for sipping morning coffee or afternoon cocktails.', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes');
INSERT INTO `amenities` VALUES(11, '../imagesMB/innpg/tucker.jpg', 'TUCKER HOUSE', '360-378-2783', '800-965-0123', '360-378-2270', 'www.tuckerhouse.com', 'http://www.tuckerhouse.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/tucker1.jpg', '../imagesMB/logoMB/nwBest.gif', '../imagesMB/logoMB/kiss2.gif', '../imagesMB/logoMB/bellHop.gif', '', 'This newly-renovated 1898 Victorian inn with cottages is nestled in a quiet Friday Harbor neighborhood, just 2 blocks from the ferry landing and center of town. All rooms feature private baths, TV/VCRs and a full breakfast. Some rooms and suites have 2 person Jacuzzi tubs, balconies, fire places, kitchenettes and private entrances. Families and pets are welcome in our cottages. Excellent amenities and rates. One of 40 Inns in the state selected to be in the Special PlacesBook. ', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes');
INSERT INTO `amenities` VALUES(12, '../imagesMB/innpg/wildwood.jpg', 'WILDWOOD MANOR', '360-378-3447', '877-298-1144', '360-378-6095', 'www.wildwoodmanor.com', 'http://www.wildwoodmanor.com', '[email protected]', '[email protected]', '../imagesMB/logoMB/showOnMapW.gif', '../imagesMB/innpg/wildwood1.jpg', '../imagesMB/logoMB/threedoor.gif', '', '', '', 'Wildwood Manor is an elegant Queen Anne-style home with breathtaking views of the water and forest. Our eleven tranquil acres feature beautiful gardens and wooded walking paths. Four beautifully appointed rooms with private baths, TV, DVD, and fine amenities. Our three course breakfasts will entice you and get your day off to a delicious start. Endless snacks and refreshments.', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'yes', 'no', 'yes', 'no', 'no');

A(Answer):

your database code definitely won’t work.

– foreach(): I don’t know either what you want there
– line #20: inserts the string "Array". not sure what that’s supposed to do.
– mysql_connect(): a) wrong function (outdated), b) wrong parameter set, please refer to the manual.

A(Answer):

thank you for your reply
what I need is if you go on the amenities page and check breakfast, pickup, tv and child
The ampage will bring up the inns that have those amenities
so the harrison house, trumpeter inn and argyle house return as a result
does this make sense

A(Answer):

web site is at
http://mossgraphics.net/TestSite/ame…/amenities.php
ampage is not up yet
I am trying to create it with the mySQL
so all the information will show up on the page

A(Answer):

what you need for your query is a WHERE clause, e.g.

// find all amenities with internet
$select_query = "SELECT `am_id` FROM `amenities` WHERE `internet` = 1";

additionally, I’d use the ENUM type for those fields (either they have it or they don’t) and you can move all of the descriptive fields (fax, mail, phone, address) to a new table.

A(Answer):

Thank you soooooo much I will get on it right away. This weekend I was in a remote area but I downloaded the php manual and read some of it. Thanks again for all the help.

A(Answer):

oh boy I thought I was thinking like you but I got lost again.
For the good news I did separate the tables and was able to populate the fields of name, fax, mail, phone, address. I am lost with the amenities again, I created a new table, not sure which one to use now. I feel like an idiot. I am an artist and this does not compute. My right brain is getting a workout.

FOR THE GOOD NEWS

<?php
//Connect
$link = mysql_connect('localhost', '***', '***');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('***', $link);
if (!$db_selected) {
die('Could not select database: ' . mysql_error());
}
$query = 'SELECT * FROM inns';
$result = mysql_query($query);
if (!$result) {
die('Query failed: ' . mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>checkboxes</title>
<link href="scripts/main.css" rel="stylesheet" type="text/css" />
<style type="text/css"></style>
</head>
<body>
<div id="mainArticle">
<div id="mainArticleInn3">
<?php
//populates information fields
for ($i = 0; $i <= mysql_num_rows($result) -1; $i++) {
if (!mysql_data_seek($result, $i)) {
echo "Cannot seek to row $i: " . mysql_error() . "\n";
continue;
}
if (!($row = mysql_fetch_assoc($result))) {
continue;
}
}
mysql_free_result($result);
?>
<div id="inn3a"><a href = "<?php echo $row['url']; ?>" target="_blank"><img src="innpg/<?php echo $row['image1']; ?>" width="200" height="150" /></a></div>
<div class="lightText" id="innText3">
<p class="innTitleLight"><a href="<?php echo $row['url']; ?>" target = "_blank"> <?php echo $row['bbName']; ?></a></p>
<p class="innPhoneLight"><?php echo $row['phone']; ?></p>
<p class="innPhoneLight"><?php echo $row['tollFree']; ?></p>
<p class="innPhoneLight"><?php echo $row['fax']; ?></p>
<p class="innWebLight"><a href = "<?php echo $row['url']; ?>" target="_blank"><?php echo $row['web'] ?></a></p>
<p class="innWebLight"><a href="<?php echo $row['emailL']; ?>" target="_blank"><?php echo $row['email'] ?></a></p>
<div id="dateMap3"><a href="http://bytes.com/topic/php/calendar/calendar.html"><img src="logoMB /checkDateW.gif" alt="Check Date" width="60" height="40" align="left" /></a><a href="<?php echo $row['map']; ?>"><img src="logoMB /showOnMapW.gif" alt="Show on Map" width="60" height="40" align="right" /></a></div>
</div>
<div id="inn3b"><a href="<?php echo $row['url']; ?>" target="_blank"><img src="innpg/<?php echo $row['image2']; ?>" width="200" height="150" /></a></div>
<div id="innRating3"><img src="logoMB /<?php echo $row['logo1']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo2']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo3']; ?>" width="60" height="55" /><img src="logoMB /<?php echo $row['logo4']; ?>" width="60" height="55" /></div>
<div class="InformationTextLight" id="innMainText3"><?php echo $row['description']; ?>
</div>
<p class="lightText">&nbsp;</p>
</div>
<form id="form1" name="form1" method="post" action="amenities.php">
<input type="submit" name="start" id="start" value="Start Over" />
</form>
</div>
</body>
</html>

HERE IS TABLE 1 where I played with ENUM and it didn’t quite make sense, so I created table 2 down below. I am still working with it, but not sure which one to use

-- phpMyAdmin SQL Dump
-- version 3.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 15, 2011 at 04:12 PM
-- Server version: 5.1.44
-- PHP Version: 5.3.2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sjibb_test`
--
-- --------------------------------------------------------
--
-- Table structure for table `amenities`
--
CREATE TABLE `amenitites` (
`am_id` int(11) NOT NULL,
`bbName` enum('1','2','3','4','5','6','7','8','9','10','11','12') NOT NULL,
`breakfast` varchar(4) NOT NULL,
`bbq` varchar(4) NOT NULL,
`kitchenette` varchar(4) NOT NULL,
`fireplace` varchar(4) NOT NULL,
`internet` varchar(4) NOT NULL,
`tv` varchar(4) NOT NULL,
`dvd` varchar(4) NOT NULL,
`video` varchar(4) NOT NULL,
`bath` varchar(4) NOT NULL,
`jacuzzi` varchar(4) NOT NULL,
`hotub` varchar(4) NOT NULL,
`massage` varchar(4) NOT NULL,
`handicap` varchar(4) NOT NULL,
`child` varchar(4) NOT NULL,
`pickup` varchar(4) NOT NULL,
`pet` varchar(4) NOT NULL,
PRIMARY KEY (`am_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `amenity_test`
--
INSERT INTO `amenity_test` VALUES(1, '1', 'yes', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'no', 'yes', 'yes', 'yes');
INSERT INTO `amenity_test` VALUES(2, '2', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenity_test` VALUES(3, '3', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes');
INSERT INTO `amenity_test` VALUES(4, '4', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no');
INSERT INTO `amenity_test` VALUES(5, '5', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'yes', 'no');
INSERT INTO `amenity_test` VALUES(6, '6', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenity_test` VALUES(7, '7', 'yes', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'no', 'no', 'no');
INSERT INTO `amenity_test` VALUES(8, '8', 'yes', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'yes', 'no', 'no', 'no', 'no', 'yes', 'no', 'no');
INSERT INTO `amenity_test` VALUES(9, '9', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no');
INSERT INTO `amenity_test` VALUES(10, '10', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes');
INSERT INTO `amenity_test` VALUES(11, '11', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes');
INSERT INTO `amenity_test` VALUES(12, '12', 'yes', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'yes', 'no', 'no', 'yes', 'no', 'yes', 'no', 'no');

THEN I THOUGHT OF CHANGING IT TO THIS is this wrong?
I made a row of inns and the columns are the amenities. Is this what you meant?

-- phpMyAdmin SQL Dump
-- version 3.2.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 15, 2011 at 04:00 PM
-- Server version: 5.1.44
-- PHP Version: 5.3.2
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `sjibb_test`
--
-- --------------------------------------------------------
--
-- Table structure for table `amenities_test`
--
CREATE TABLE `amenities_test` (
`am_id` int(11) NOT NULL AUTO_INCREMENT,
`argyle` varchar(20) NOT NULL,
`dragonfly` varchar(20) NOT NULL,
`harrison` varchar(20) NOT NULL,
`hillside` varchar(20) NOT NULL,
`intothewoods` varchar(20) NOT NULL,
`kirk` varchar(20) NOT NULL,
`olympic` varchar(20) NOT NULL,
`warfside` varchar(20) NOT NULL,
`states` varchar(20) NOT NULL,
`trumpeter` varchar(20) NOT NULL,
`tucker` varchar(20) NOT NULL,
`wildwood` varchar(20) NOT NULL,
PRIMARY KEY (`am_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=54 ;
--
-- Dumping data for table `amenities`
--
INSERT INTO `amenities` VALUES(1, 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast', 'breakfast');
INSERT INTO `amenities` VALUES(2, 'bbq', '', 'bbq', 'bbq', '', 'bbq', '', '', 'bbq', '', 'bbq', '');
INSERT INTO `amenities` VALUES(3, '', '', 'kitchenette', '', '', '', '', '', 'kitchenette', '', 'kitchenette', '');
INSERT INTO `amenities` VALUES(4, '', '', 'fireplace', 'fireplace', '', 'fireplace', 'fireplace', '', 'fireplace', 'fireplace', 'fireplace', 'fireplace');
INSERT INTO `amenities` VALUES(5, 'internet', '', '', '', '', '', '', '', '', '', '', '');
INSERT INTO `amenities` VALUES(6, 'tv', 'tv', 'tv', 'tv', '', 'tv', '', '', 'tv', 'tv', 'tv', 'tv');
INSERT INTO `amenities` VALUES(7, 'dvd', 'dvd', 'dvd', 'dvd', '', 'dvd', '', '', 'dvd', 'dvd', 'dvd', 'dvd');
INSERT INTO `amenities` VALUES(8, 'video', '', 'video', 'video', '', 'video', '', '', '', 'video', 'video', 'video');
INSERT INTO `amenities` VALUES(9, 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath', 'bath');
INSERT INTO `amenities` VALUES(10, '', 'jacuzzi', 'jacuzzi', 'jacuzzi', '', 'jacuzzi', '', '', '', '', 'jacuzzi', '');
INSERT INTO `amenities` VALUES(11, 'hotub', '', 'hotub', '', '', '', '', '', '', 'hotub', 'hotub', '');
INSERT INTO `amenities` VALUES(12, 'massage', '', 'massage', '', '', '', '', '', '', 'massage', 'massage', 'massage');
INSERT INTO `amenities` VALUES(13, '', '', '', '', '', '', '', '', 'handicap', 'handicap', 'handicap', '');
INSERT INTO `amenities` VALUES(14, 'child', '', 'child', 'child', '', '', '', 'child', 'child', 'child', 'child', 'child');
INSERT INTO `amenities` VALUES(15, 'pickup', '', 'pickup', '', '', 'pickup', '', '', 'pickup', 'pickup', 'pickup', '');
INSERT INTO `amenities` VALUES(16, 'pet', '', 'pet', '', '', '', '', '', '', 'pet', 'pet', '');

A(Answer):

you should use neither of these dumps (as they are now). it may be a typo, but you create `amenities` and insert into `amenity_test` (and vice versa) … and in your PHP file, you query `inns`

A(Answer):

NOTE: Fixed… I think

This might be a start assuming you setup the amenities table as follows:

bnb_id int – the B&B id (foreign key to bnb table’s id field)
amenity varchar(length?) – the amenity name

Here’s the amenities table populated with some records:


bnb_id, amenity
1, "bbq"
1, "kitchenette"
1, "child"
1, "pickup"
1, "pet"
1, "tv"
2, "child"
2, "pet"
2, "tv"

<?php
$am_count = count($_POST['amenity']);
if (!$am_count) {
$sql = "Select name FROM bnb";
} else {
$in_clause = "";
for ($i = 0; $i < $am_count; $i++ ) {
$in_clause .= '"' . $_POST['amenity'][$i] . ($i < $am_count -1 ? '",' : '"');
}
$sql =
sprintf(
"SELECT name "
. "FROM bnb "
. "WHERE id IN "
. "(SELECT bnb_id FROM amenities "
. "WHERE amenity IN (%s) "
. "GROUP BY bnb_id "
. "HAVING COUNT(amenity) = %d)",
$in_clause, $am_count
);
}
print $sql
/*
SELECT name FROM bnb WHERE id IN
(SELECT bnb_id FROM amenities
WHERE amenity
IN (
"bbq","kitchenette","child","pickup","pet"
)
GROUP BY bnb_id
HAVING COUNT(amenity) = 5
)
*** Only B&B with an id of 1 will get selected based on the amenities selected ***
*/
?>

A(Answer):

Hi dgreenhouse I hope you are still out there. I have been playing with the code and it is not working I keep getting an error undefined index: amenity in/…..
I have simplified everything as much as I could
INN TABLE

-- Database: `bnbtest`
--
-- -----------------------------------------------------
--
-- Table structure for table `bnb`
--
CREATE TABLE `bnb` (
`bnb_id` int(11) unsigned NOT NULL,
`image1` varchar(50) NOT NULL,
`bbName` varchar(50) NOT NULL,
`phone` varchar(20) NOT NULL,
`tollFree` varchar(20) NOT NULL,
`fax` varchar(20) NOT NULL,
`web` varchar(50) NOT NULL,
`url` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`emailL` varchar(50) NOT NULL,
`map` varchar(50) NOT NULL,
`image2` varchar(50) NOT NULL,
`logo1` varchar(50) NOT NULL,
`logo2` varchar(50) NOT NULL,
`logo3` varchar(50) NOT NULL,
`logo4` varchar(50) NOT NULL,
`description` varchar(4096) NOT NULL,
PRIMARY KEY (`bnb_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `bnb`
--
INSERT INTO `bnb` VALUES(1, '../image/inn/argyle.jpg', 'ARGYLE HOUSE', '360-378-4084', '800-624-3459', '', 'www.argylehouse.net', 'http://www.argylehouse.net', '[email protected]', '[email protected]', '../maps/Argyle.html', '../image/inn/argyle1.jpg', '../image/logo/threedoor.gif', '', '', '', 'Enjoy the charm and character of this 1910-vintage Craftsman located only two blocks from downtown and a short walk from the ferry.');
INSERT INTO `bnb` VALUES(2, '../image/dragonfly.jpg', 'DRAGONFLY INN', '360-378-4280', '877-378-4280', '360-378-1392', 'www.thedragonflyinn.com', 'http://www.thedragonflyinn.com', '[email protected]', '[email protected]', '../maps/dragonfly.html', '../image/dragonfly1.jpg', '../image/logo/aaa3D.gif', '../image/logo/threedoor.gif', '', '', 'A unique,Japanese-inspired bed and breakfast.');
INSERT INTO `bnb` VALUES(3, '../image/inn/hillside.jpg', 'HILLSIDE HOUSE', '360-378-4730', '800-232-4730', '360-378-4715 FAX', 'www.hillsidehouse.com', 'www.hillsidehouse.com', '[email protected]', '[email protected]', '../map/hillside.html', '../image/inn/hillside.jpg', '../image/logo/tripleA', '../image/logo/mobile', '../image/logo/3doors', '../image/logo/bellhop', 'Enjoy views of snowcapped Mt. Baker');

TABLE AMENITYTEST

-- Database: `bnbtest`
--
-- ----------------------------------------------------
--
-- Table structure for table `amenitytest`
--
CREATE TABLE `amenitytest` (
`bnb_id` int(11) unsigned NOT NULL,
`amenity` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `amenitytest`
--
INSERT INTO `amenitytest` VALUES(3, 'internet');
INSERT INTO `amenitytest` VALUES(2, 'fireplace');
INSERT INTO `amenitytest` VALUES(1, 'kitchenette');
INSERT INTO `amenitytest` VALUES(2, 'bbq');
INSERT INTO `amenitytest` VALUES(3, 'breakfast');

and here is the process page

<html>
<head>
<title>checkboxes</title>
</head>
<body>
<?php
//connect
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("bnbtest") or die(mysql_error());
//selects all columns from bnb
$query = 'SELECT * FROM bnb';
//retrieves the records
$result = mysql_query($query);
if (!$result) {
die('Query failed: ' . mysql_error());
}
$am_count = count($_POST['amenity']);
if (!$am_count) {
$sql = "SELECT * FROM bnb";
} else {
$in_clause = "";
for ($i = 0; $i < $am_count; $i++) {
$in_clause .= ' " ' . $_POST['amenity'][$i] . ($i < $am_count -1 ? '",' : '"');
}
$sql=
sprintf(
"SELECT *"
. "FROM bnb"
. "WHERE id IN"
. "(SELECT bnb_id FROM amenitytest"
. "WHERE amenity IN (%s)"
. "GROUP by bnb_id"
. "HAVING COUNT (amenity) = %d)",
$in_clause, $am_count);
// Print out the contents of the entry
echo $row['bbName'] . "<br />";
echo $row['url'] . "<br />";
echo $row['phone'] . "<br />";
echo $row['tollFree'] . "<br />";
echo $row['fax'];
echo $row['web'] . "<br />";
echo $row['email'] . "<br />";
echo $row['description'] . "<br />". "<br />";
}
mysql_close()
?>
</body>
</html>

A(Answer):

you never check, whether $_POST['amenity'] actually exists (isset()).

A(Answer):

I placed

if(isset($_POST['amenity']))
//connect
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("bnbtest") or die(mysql_error());

and my error now is Warning: mysql_select_db() [function.mysql-select-db]: Access denied for user……..

I know that the connection is correct because it works for all my other simple test. for instance I just finished creating this example below and all the inns that have breakfast (WHERE amenity.breakfast =’y’) come up. the table is an older one I used before with y and n.

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("bnbtest") or die(mysql_error());
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT bnb.*
FROM amenity, bnb
WHERE amenity.breakfast = 'y'
AND amenity.bnb_id = bnb.bnb_id
LIMIT 0,30")
or die(mysql_error());
//store the record of the bnb and amenity table into $row
while($row = mysql_fetch_array($result)) {
// Print out the contents of the entry
echo $row['bbName'] . "<br />";
echo $row['url'] . "<br />";
echo $row['phone'] . "<br />";
echo $row['tollFree'] . "<br />";
echo $row['fax']. "<br />";
echo $row['web'] . "<br />";
echo $row['email'] . "<br />";
echo $row['description'] . "<br />". "<br />";
}
?>

A(Answer):

you are aware that an if() condition without {} only works on the next line (the mysql_connect())? thus when calling mysql_select_db() I think PHP tries to use the defaults from php.ini, which may cause that error.

one more reason to use MySQLi (or even better: PDO) instead of the outdated mysql functions.

A(Answer):

thanks I did not understand the brackets,

if(isset($_POST['amenity']))
{
//connect..............
//all the same code
echo $row['description'] . "<br />". "<br />";
}
}//thanks for the bracket info
mysql_close()//now it says this is a problem

I am getting

Warning: mysql_close(): no MySQL-link resource supplied in……..

A(Answer):

Also I started to read about MySQLi and PDO. I am not sure but MySQLi seems simpler because it works with MySQL.
With PDO there is a warning that some MySQL table do not support transactions.
I am new at programing and I would like to go in the right direction
thanks for all your help

A(Answer):

you cannot close a connection you don’t have (i.e. include the close in the brackets)

A(Answer):

Well it’s done I would like to thank Dormilich and dgreenhouse for all your help. I want to post this for anyone else who has the same problems I had with checkboxes. I left the checkboxes as an html file and posted to this file. You will have to create the table there is a sample above and like Dormilich says create two tables
thanks again

<?php
if(isset($_POST['amenity']))
{
//Build a where clause based on the form
$where = false;
foreach($_POST['amenity'] as $amenity)
{
if(!$where)
{
$where = 'WHERE a.'. $amenity." = 'y'";
}
else
{
$where .= 'AND a.'. $amenity." = 'y'";
}
}
$mysqli = new mysqli('localhost', 'root', 'root', 'bnbtest');
$sql ="
SELECT bnb.*
FROM bnb
JOIN amenity as a
ON a.bnb_id = bnb.bnb_id
$where
";
$result = $mysqli->query( $sql );
while ($row = $result->fetch_assoc()) {
echo $row['bbName'] . "<br />";
echo $row['url'] . "<br />";
echo $row['phone'] . "<br />";
echo $row['tollFree'] . "<br />";
echo $row['fax'] . "<br />";
echo $row['web'] . "<br />";
echo $row['email'] . "<br />";
echo $row['description'] . "<br />" ."<br />";
}
$mysqli->close();
}
?>