How to open CMSB DB twice?

2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 27, 2010   (RSS)

By mingyq - July 27, 2010

OK, so we created a booking form, http://www.castlekid.ca/booking_form.php.

One of the fields (Type of Inflatable) pulls data straight from the CMSB Database, see code:

<td class="ra" height="38"><img src="image/book_typeinflatable.png" width="226" height="36" /></td>
<td class="la">
<select name="type">
<?php
function Connect(){$serveraddress = "localhost";$user = "castleki_cmsadm";$pass = "wi1hill";mysql_connect($serveraddress,$user, $pass) or die('Could not connect: ' . mysql_error());}
function Disconnect(){$serveraddress = "localhost";$user = "castleki_cmsadm";$pass = "wi1hill";mysql_close(mysql_connect($serveraddress,$user, $pass)) or die('Could not disconnect: ' . mysql_error());}
Connect();
$sql = mysql_query("SELECT * FROM castleki_cmsbuilder.cms_listings ORDER BY product ASC");
while($r=mysql_fetch_array($sql)){
$name = $r["product"];
$size = $r["category"];
print '<option>'.$name.' - ('.$size.')</option>';
}
Disconnect();
?>
</select>
</td>
</tr>
<tr>
<td colspan="2" height="20">(Please be sure to check
the prices on the pricing page!)</td>
</tr>
<tr>
<td class="ra" height="38"><img src="image/book_typeinflatable_2nd.png" width="226" height="36"></td>
<td class="la"> <input type="text" name="2ndtype" name="text" />
(see list above)</td>
</tr>

Just below we want to give the client an option to choose an alternative product (2nd Choice) based on the same list. However, copying the same code as for choice 1 into choice 2 gives a DB Error saying your not allowed to open a DB already opened or something to that effect (it's been a couple of weeks since I tried this).

Any ideas how to populate this twice based on the same data?

MingyQ

Re: [mingyq] How to open CMSB DB twice?

By Jason - July 27, 2010

Hi,

You actually don't have to worry about connecting to the database as CMS Builder can handle that for you. At the top of your page should be a line of code that looks something like this:

<?php require_once("cmsAdmin/lib/viewer_functions.php"):?>

This will handle all of your database connections. There are also functions that will handle your SQL statements. The function is called getRecords. The code would look something like this:

list($listingsRecords,$listingsMetaData)=getRecords(array(
'tableName' => 'listings',
'orderBy' => 'product asc',
));


After this code is executed, $listingRecords will be an array of all of the records from the listings table, sorted by product.

For more information on this function, see this page:
http://www.interactivetools.com/docs/cmsbuilder/viewer_options.html

Hope this helps get you started. A good thing to try is to use the code generator inside CMS Builder. This will give you some great examples of the functions available in CMS Builder and how to use them.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/