I am working on a open source script at the moment, but I am struggling on one final aspect of the script.
I am a bit of a newb to PHP at this level, I have a working knowledge of PHP and MySQL, but this seems to be a little beyond my knowledge.
I want the users to be able to answer a questionaire, this will be then stored in the MySQL database and displayed on the users profile. But when I press submit it is going into the database. The script is as follows:
Quesionaire PHP code:
<?php
$cVersion = '1.0';
$cType = 'widget';
if ( $_SESSION['UserId'] =='' ) {
header('location: signup.php');
exit;
}
$vCheck = $fDB->getOne ("SELECT count(*) from ! where name = ? and type = ?", array( FUSE_VERSIONS, $cName, $cType) );
if ($vCheck != '1') {
$fDB->query( 'insert into ! (name, version, type, date) values (?, ?, ?)', array( FUSE_VERSIONS, htmlentities($cName), htmlentities($cVersion), htmlentities($cType)) );
} else {
$fDB->query( "UPDATE ! SET version = ? WHERE name = ? and type = ?" ,array( FUSE_VERSIONS, htmlentities($cVersion), htmlentities($cName), htmlentities($cType) ) );
}
unset($cName, $cVersion, $vCheck, $cType);
// Query to reterive records from KangaDate_questions table, sorted descending on mandatory: that is, mandatory fields should be displayed first
$temp = $fDB->getAll( 'select id, question, mandatory, description, guideline, maxlength, control_type from ! where enabled = ? and question <> ? and gender in (?,?) order by mandatory desc, displayorder', array( QUESTIONS_TABLE, 'Y' , '',$_SESSION['gender'],'A') );
$data = array();
foreach( $temp as $index => $row ) {
if ($_SESSION['opt_lang'] != 'english') {
/* THis is made to adjust for multi-language */
$lang_question = $_SESSION['profile_questions'][$row['id']]['question'];
$lang_descr = $_SESSION['profile_questions'][$row['id']]['description'];
$lang_guide = $_SESSION['profile_questions'][$row['id']]['guideline'];
if ($lang_question != '') {
$row['question'] = $lang_question;
}
if ($lang_descr != '') {
$row['description'] = $lang_descr;
}
if ($lang_guide != '') {
$row['guideline'] = $lang_guide;
}
}
// reterive record from osdate_questionoptions table
$options = $fDB->getAll( 'select * from ! where enabled = ? and questionid = ? order by displayorder', array( OPTIONS_TABLE, 'Y', $row['id'] ) ) ;
$optsrs = array(); if ($_SESSION['opt_lang'] != 'english') { /* THis is made to adjust for multi-language */ foreach($options as $kx => $opt) { $lang_ansopt = $_SESSION['profile_questions'][$row['id']][$opt['id']]; if ($lang_ansopt != '') {$opt['answer'] = $lang_ansopt; } $optsrs[] = $opt; } } else {$optsrs = $options; }
unset($options);
$row['options'] = makeOptions ( $optsrs );
unset($optsrs);
$userprefrs = $fDB->getAll( 'select questionid, answer from ! where userid = ?', array( USER_PREFERENCE_TABLE, $userid, $row['id'], ) ) ;
$row['userpref'] = makeAnswers ( $userprefrs );
unset($userprefrs);
$data [] = $row;
}
if ( isset( $_GET['errid'] ) ) {
$t->assign( 'mandatory_question_error', get_lang('errormsgs',$_GET['errid']) );
}
$t->assign( 'sectionid', $sectionid );
$t->assign('frmname', 'frm' . $sectionid );
$t->assign( 'head', get_lang('myprofilepreferences')." - ".$sections[ $sectionid ] );
$t->assign( 'data', $data );
$t->assign('lang', $lang);
unset($data, $temp, $sections);
?>
TPL code:
<font color="{lang mkey='error_msg_color'}">{$mandatory_question_error}</font>
<form name="{$frmname}" method="post" action="savequestion.php">
<input type="hidden" name="sectionid" value="{$sectionid}"/>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td colspan="2" class="title">{$head}</td>
</tr>
<tr>
<td colspan="2" width="100%">
{*Outer Loop to traverse outer dimension data array*}
{foreach item=questionrow from=$data}
{if $questionrow.control_type == "select"}
<table width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td width="250">
<table width="250" border="0">
<tbody>
<tr>
<td width="250">
{$questionrow.question}
{if $questionrow.mandatory == 'Y'}
<font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
{/if}
<br/>
{if $questionrow.description != NULL}
{$questionrow.description}
{/if}
</td>
</tr>
</tbody>
</table>
</td>
<td width="329">
<table width="329" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td width="329">
<select name="{$questionrow.id}{$questionrow.mandatory}" class="select" style="width: 125px">
<option value="0">{lang mkey='tell_later'}</option>
{html_options options=$questionrow.options}
</select>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
{elseif $questionrow.control_type == "radio"}
<table width="100%" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td width="250">
<table width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td align="left">
{$questionrow.question}
{if $questionrow.mandatory == 'Y'}
<font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
{/if}
<br/>
{if $questionrow.description != NULL}
{$questionrow.description}
{/if}
</td>
</tr>
</tbody>
</table>
</td>
<td width="329">
<table width="329" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td>
{foreach name="iterator" key=key item=curropt from=$questionrow.options}
<input name="{$questionrow.id}{$questionrow.mandatory}" type="radio" value="{$key}" checked />{$curropt}
{if $smarty.foreach.iterator.iteration%2 == 0 }
</td></tr><tr><td>
{else}
</td><td>
{/if}
{/foreach}
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
{elseif $questionrow.control_type == "checkbox"}
<table width="700" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td>
<table width="250" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td align="left">
{$questionrow.question}
{if $questionrow.mandatory == 'Y'}
<font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
{/if}
<br/>
{if $questionrow.description != NULL}
{$questionrow.description}
{/if}
</td>
</tr>
</tbody>
</table>
</td>
<td>
<table width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td align="left">
{html_checkboxes name=$questionrow.id|cat:$questionrow.mandatory options=$questionrow.options separator=<br/>}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
{elseif $questionrow.control_type == "textarea"}
<table width="700">
<tbody>
<tr>
<td>
<table width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td align="left">
{$questionrow.question}
{if $questionrow.mandatory == 'Y'}
<font class="required_info">{$smarty.const.REQUIRED_INFO}</font>
{/if}
<br/>
{if $questionrow.description != NULL}
{$questionrow.description}
{/if}
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table width="450" border="0" cellspacing="{$config.cellspacing}" cellpadding="{$config.cellpadding}" bgcolor="{$config.bgcolor}">
<tbody>
<tr>
<td>
<textarea name="{$questionrow.id}{$questionrow.mandatory}" rows="7" cols="100">