Willkommen, Gast
Benutzername: Passwort: Angemeldet bleiben:
  • Seite:
  • 1
  • 2

THEMA: Reading from a Database

Reading from a Database 9 Monate 3 Wochen her #97475

  • FewPexia
  • FewPexias Avatar
  • Fresh Lemon
  • Beiträge: 6
  • Karma: 0
Built a survey that reads from a database and then displays what is read back to the user.
One question gets a dealer code.
A second question reads the dealer code and then looks it up in a table to get the associated dealer information.
The dealer code is displayed back to the user in a question and the dealer information is displayed as the answer to the question so
the user can make adjustments to it if needed.

This was implemented as follows.
Javascript for the second question reads the dealer code (a previous answer.)
The dealer code is passed to a php script.
The php script then builds a database query and runs it.
This results in dealer information corresponding to the passed in dealer code or an error (dealer code not found) indication.
The question is then adjusted to include the dealer code.
The question answer is then set with the dealer information.

This was implemented using the xampp installation on a PC using mysql.

This uses the following tables created and populated as follows.
CREATE TABLE IF NOT EXISTS `dealer_code_table` (
`dealer_code` varchar(6) NOT NULL,
`dealer_info` varchar(120) DEFAULT NULL,
PRIMARY KEY (`dealer_code`)
) ;

INSERT INTO `dealer_code_table` (`dealer_code`, `dealer_info`) VALUES
('1001', 'Book Dealer'),
('1002', 'Antique Dealer'),
('1003', 'Bond Dealer'),
('1004', 'Appliance Dealer'),
('1005', 'Toy Dealer');

The survey uses the default template.
This uses a first dealer code question set as short free text.
This uses a second question in a second question group (another page.)
The second question is set as long free text.
The javascript for the second question is:
Adjust the dealer information for the given dealer code if necessary.</p>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$dealerCode = '{449977X13X27}';
//alert("dealerCode = " + $dealerCode); // For debugging
$('#question{QID} td.questiontext').text('Adjust the dealer information for dealer code ' + $dealerCode + ' if necessary. ');
$.get('upload/templates/getDealerInfo.php?dealerCode='+$dealerCode, function(data) {
//alert("data loaded: " + data); // For debugging
$('#answer{SGQ}').val('Dealer Information: ' + data);

The php script getDealerInfo.php located in C:\xampp\htdocs\upload\templates is:
//echo $_REQUEST; // For debugging

$link = mysql_connect('localhost', 'root', '', 'limesurvey'); // connect to database - better if there were persistent connection.

if (!$link) // if connection fails
echo "Failed to connect to database!" ; // indicate error condition
die('Could not connect: ' . mysqli_error($link));
//echo "connected to database!" ; // For debugging
mysql_select_db('limesurvey', $link);

//$sql = "SELECT surveyls_title FROM lime_surveys_languagesettings";
$sql = "SELECT dealer_info FROM dealer_code_table where dealer_code = '" . $_REQUEST . "'";
// echo $sql; // For debugging

$result = mysql_query($sql);
if (!$result)
echo "Dealer Code not found in database!";
//die('nuts ' . mysqli_error($link));
} else {
$row = mysql_fetch_assoc($result); // Assume each dealer code can only have one enter in the database.
if (!$row)
echo "Dealer Code not found in database!";
} else {
echo $row["dealer_info"] ; // Return information

I hope this helps someone!
Please let me know it it does.

Some questions I still have:
Is there a way to make the database connection persistent?
I assume that LimeSurvey keeps a perisistent connection, can custom php scripts make use of it?

To reference a previous question's answer I seem to have had to use the Survey#QuestionGroup#Question#, is there a way to reference this without using the numbers?
Is there some way to get to just the previous question's answer?
It would be nice to have a tree navigation from {SGQ}.

I have just been playing with LimeSurvey for a couple of days and am not a frequent HTML,JavaScript developer, nor have any experience with php or even mysql, so please let me know any comments on the above.

Again I hope this helps someone.
Der Administrator hat öffentliche Schreibrechte deaktiviert.
  • Seite:
  • 1
  • 2
Moderatoren: ITEd
Ladezeit der Seite: 0.123 Sekunden
Donation Image