Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

pa help pls :( codes sa PHP

junelle13

Apprentice
Advanced Member
Messages
60
Reaction score
0
Points
26
may project kasi kami evaluation system na web. nag select ako ng lahat ng questions sa database at nilagay ko sa datatable tapos each question meron syang radio button na may value na 1, 2, 3, 4, 5.. ang problema is hindi ko alam kung paano sya iinsert sa database. ilang araw na ako na stuck sa problemang ito d ko pa din na sosolusyunan pa help naman po eto po codes ko..

<?php

$sql = "SELECT * from question where category='A' order by id ASC";

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) != 0)
{
while($row=mysqli_fetch_array($result))
{
$id = $row['id'];
$category = $row['category'];
$question = $row['question'];

echo "<tr>
<td>$question</td>
";

?>

<td width = "25%">
<center>
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios1" class="first" value="1 - No idea" required>1

<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios2" class="first" value="2 - Known little about it" required>2

<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios3" class="first" value="3 - Familiar with it" required>3

<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios4" class="first" value="4 - Confident to talk about it" required>4

<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios5" class="first" value="5 - Can apply it to work/ output deliverables" checked>5

</center>
</td>



<?php
} }

?>


</tr>

</table>
<center>
<input type="submit" name="evaluate" class="btn btn-primary btn-large" value="Submit" />
</center>

</form>

<?php

if (isset($_POST['evaluate'])) {

$subjectcode1 = $_GET['subjectcode'];
$professor = $row['professor'];
$session = $_SESSION['studentnumber'];
$q = $_POST['q'.$id];

$answers = array($q => $value);

foreach ($answers as $q => $value){
echo $value;

$sql = "INSERT INTO studentresult (professor, rater, q1)
VALUES('$subjectcode1','$session', '$value')";

if (mysqli_query($con, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}
mysqli_close($con);
}
}

?>

sana po may makatulong :( malapit na kasi yung defense namin ngayun monday na :help::pray:
 
Meron akong ginawang SURVEY PROGRAM gamit PHP

ganito ang sample idea


question_tbl
------------------------------------------------------------------------------------------------------------------
id | Question | Answer | input_Option | input_type | input_name
------------------------------------------------------------------------------------------------------------------
1 | what is your sex | 0 | ['male','female'] | radio | sex
------------------------------------------------------------------------------------------------------------------
2 | What in your name | bdalina | null | text | name
------------------------------------------------------------------------------------------------------------------

$query= "select * from question_tbl ";



mag loop ka dito kung ganu kadami questions mo.
{

$id = $row['id'];
$question = $row['question'];
$answer = $row['answer'];
$input_option = $row['inout_option'];
$input_type = $row['input_type'];
$input_name= $row['input_name'];


echo 'Question {$question} <br/>';


switch ($input_type) //start switch
{
case 'radio':
for($i =0; $i < count($inout_option); $++)
{
if($i == $answer){ $checked = 'checked';}else{ $checked = '';}
echo "<input type = '{$input_type}' name='{$input_name}' value='{$input_option[$i]}' {$checked} /> ";
}
break;


case 'text':
echo "<input type = '{$input_type}' name='{$input_name}' value='{$answer[$i]}' /> ";
break;

checkbox
textarea
select
etc....

}//end switch




}





What is your gender?

Male ⦿
Female ⦾



What is your name?

bdalina
 
Hi, I just modified your code. Please try it.

Code:
<form action="" method="POST">
	<table>
<?php 


//$_GET['subjectcode']="math1"
//$_SESSION['studentnumber']=001;

/[COLOR="#FF0000"]/ REPLACE IT WITH YOUR CONNECTION[/COLOR]
$con=mysqli_connect("localhost","king","master","control_system");

$sql = "SELECT * from question where category='b' order by id ASC";	

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) != 0) { 
	while($row=mysqli_fetch_array($result)) { 
		$id = $row['id'];
		$category = $row['category'];
		$question = $row['question'];

		?>

		<tr>
			<td><?php echo $question;?></td>
			<td width = "25%">
				<center>
					<input type="radio" name="q<?php echo $id;?>" id="inlineoptionsRadios1" class="first" value="1 - No idea" required>1

					<input type="radio" name="q<?php echo $id;?>" id="inlineoptionsRadios2" class="first" value="2 - Known little about it" required>2 

					<input type="radio" name="q<?php echo $id;?>" id="inlineoptionsRadios3" class="first" value="3 - Familiar with it" required>3 

					<input type="radio" name="q<?php echo $id;?>" id="inlineoptionsRadios4" class="first" value="4 - Confident to talk about it" required>4

					<input type="radio" name="q<?php echo $id;?>" id="inlineoptionsRadios5" class="first" value="5 - Can apply it to work/ output deliverables" checked>5

				</center>
			</td>
		</tr>
	<?php 
	} 
}

?> 

</table>
<center> 
<input type="submit" name="evaluate" class="btn btn-primary btn-large" value="Submit" /> 
</center>

</form>

<?php

if (isset($_POST['evaluate'])) {

	$subjectcode1 = $_GET['subjectcode'];
	$professor = $row['professor'];
	$session = $_SESSION['studentnumber'];
	$q = $_POST['q'.$id];

	$answers = array($q);

	foreach ($answers as $value){

		$sql = "INSERT INTO studentresult (professor, rater, q1)
		VALUES('$professor','$session', '$value')";

		if (mysqli_query($con, $sql)) { 
			echo "New record created successfully";
		} 
		else {
			echo "Error: " . $sql . "<br>" . mysqli_error($con);
		}
		mysqli_close($con);
	}	
}	

?>
 
Last edited:
Tatanggalin ko na muna itong part na magre-retrieve ka ng questionnaires from the database... natatamad ako gumawa ng dummy table :)

Note #1: Hindi mo na kailangan lagyan ng ID yung radio, buttons mo, hayaan mo na CLASS na lang ang gamitin mong CSS selector nya. para mabawasan na din yung kabuuan file size mo.

Note #2: Baligtad ang ginawa mong value at label ng radio button mo. Sa case mo, ang ginawa mong label ay 1, at ang value ay 1 - No idea. Pwede mo gawin na 1 ang label (yung makikita ng user) at 1 din ang value (yung ipapasa mo sa database). Since ang tingin ko ang 1-5 mo ay metrics lamang ng kaalaman o familiarity dun sa question. So hindi mo na kailangan paulit ulitin i-save yung buong phrases like "Confident to talk about it" o "Can apply it to work/ output deliverables" dahil it will be the same for all questions di ba. so kapag inayos na sya, ganito na: <input type="radio" name="" class="first" value="1"/>1

Note #3: sa name ng radio button, hindi mo na kailangan ng [], dahil iyan ay para sa selection ng multiple values. Remember, ang ginagamit mo dito ay radio button at sa radio buttons, kahit gaano kadaming options ang available, isa lang ang PWEDENG may check. To select multiple values, it is better to use a checkbox. And since 1 question, 1 answer ang kailangan mo, radio button dapat, so dapat walang []. LIke this: <input type="radio" name="sample" id="" />

Note #4 Para uli sa name ng radio button, it would be better if you would use the question number, or a word + the question number para mas madali mo malaman na yung answer na yun ay para sa question number na naka-assign sa radio button. So it could be <input type="radio" name="1" class="first" value="1"/>1 OR pwedeng ganito: <input type="radio" name="question_1" id="" />

Code:
<?php

echo '<form method="POST">';

for($q = 6; $q <= 10; $q++){ // $q for question
	echo '<table border="1" style="width: 600px; margin-bottom: 20px;">';
		echo '<tr><td colspan="5">Sample Questionnaire #' . $q . '</td></tr>';
		
		echo '<tr>';
		
			// $status, check lang kung option 5 na, which is yun ang magiging default selected value for each question
			for($a = 1; $a <= 5; $a++){ // $a for answer
				$status = ($a == 5) ? 'checked' : ''; 
				echo '<td width="20%"><input type="radio" name="q_' . $q . '" value="' . $a . '" required ' . $status . '>' . $a . '</td>';
			}
			
		echo '</tr>';
	
	echo '</table>';
}

echo '<input type="submit" name="evaluate" class="btn btn-primary btn-large" value="Submit" />';

echo '</form>';

if(isset($_POST['evaluate'])){
	// [B][COLOR="#FF0000"]Note #5[/COLOR][/B]: delete these next few lines, nilagay ko lang ito para makita mo kung ano ang ipinapasang value ng form na ginawa natin sa taas
	echo '<pre>';
		print_r($_POST);
	echo '</pre>';
	
	
	// Ganito ang kalalabasan ng code na nasa itaas. Kapag wala ka pinalitan value, puro 5 ang lalabas, kapag nagpalit ka, siyempre yung equivalent value ang lalabas.
	/*
		Array
		(
			[q_6] => 5
			[q_7] => 5
			[q_8] => 5
			[q_9] => 5
			[q_10] => 5
			[evaluate] => Submit
		)
	*/
	
	// [B][COLOR="#FF0000"]Note #6:[/COLOR][/B] Kailangan mo i-restructure ang studentresult table mo, dahil kailangan mo magkaroon ng 1 record PER question (sa sample ko 5, so dapat 5 records na), at imultiply ito sa dami ng students na sasagot sa evaluation (sample student count: 10) 5 questions per student x 10 students = 50 records
	
	// [B][COLOR="#FF0000"]Note #7:[/COLOR][/B] Sa bagong studentresult table, magdadagdag ka lang naman ng dalawang column: id (Primary Key, Integer, Auto-Increment) at question_number (integer)
	// Ganito ang magiging itsura ng restructured table mo
	/*	
		id (Primary Key, Integer, Auto-Increment) - importante to para maging unique yung lahat ng records mo
		professor - ok na ito, make sure lang na professor id ito para hindi lahat ng records ay lalagyan mo ng record name
		rater - ok na din ito, make sure lang na student id ito para hindi lahat ng records ay lalagyan mo ng record name
		
		question_number (integer) - para alam natin kung sa aling survey question number yung sagot nya
		q1 - ok na din ito, pero kung puro numbers ang ilalagay mo dito, gawin mo na lang type INT, pwedeng palitan mo na din yung column name para hindi ka malito, dito sa example ko, gawin ko na lang siyang question_answer
	*/
	
	
	
	// Paano na natin ito ipapasok sa database?
	
	// Gamitin natin itong foreach loop, balikan natin itong result sa taas, kapag sinabing foreach($_POST) maglo-loop sya sa lahat ng indexes / keys na present dun sa POST variable natin, so sa case nga natin kanina, ganito yun (check below). yung as $key => $value, para magamit natin yung $key (q_6 to q_10 as question_number) at $value (5 yung value nung sagot)
	
	/*
		Array
		(
			[q_6] => 5
			[q_7] => 5
			[q_8] => 5
			[q_9] => 5
			[q_10] => 5
			[evaluate] => Submit
		)
	*/
	
	$subjectcode1 = $_GET['subjectcode'];
	$professor = $row['professor'];
	$session = $_SESSION['studentnumber']
	foreach($_POST as $key => $value){
		
		if($key <> 'evaluate'){
			$questionnumber = str_replace('q_', '', $key); // para ma-extract natin yung question number dun sa key, palitan natin ng '' yung mga q_
			
			// do the insert query
			$sql = "insert into studentresult (professor, rater, question_number, question_answer) values ('$professor', '$session', $questionnumber, $value)";
			mysqli_query($con, $sql);
			
			// [B][COLOR="#FF0000"]Note #8:[/COLOR][/B] Multiple times sya mag insert ng query, 1 for each survey question na gagamitin mo for evaluation
		}
		
	}
}

?>
 
mga paps maraming salamat sa sagot nyo :) pero medyo hindi ko parin gets eh, sorry mejo mali din yung codes na nailagay ko kasi inaantok na ako that time kaka edit sa codes d ko na inedit basta2 ko nalang inupload..

eto talaga yung dapat na code...




<thead>

<th class="alert alert-info"><center><strong><font color="#2B547E">A. Commitment</th>
<th class="alert alert-info"><center><strong><font color="#2B547E">Scale</th>
</thead>

<?php

$sql = "SELECT * from question where category='A' order by id ASC";

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) != 0)
{
while($row=mysqli_fetch_array($result))
{
$id = $row['id'];
$category = $row['category'];
$question = $row['question'];

echo "<tr>
<td>$question</td>
// so first column ay lahat ng mga questions na nasa category A
";

?>

// eto second column mga radio button na may value 1, 2, 3, 4, 5

<td width = "25%">

<center>
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios1" class="first" value="1" required>1
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios2" class="first" value="2" required>2
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios3" class="first" value="3" required>3
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios4" class="first" value="4" required>4
<input type="radio" name="q[<?php echo $id;?>]" id="inlineoptionsRadios5" class="first" value="5" checked>5

</center>
</td>

<?php
}}

?>
</tr>
</table>


<center>
<input type="submit" name="evaluate" class="btn btn-primary btn-large" value="Submit" />
</center>

</form>





<?php
if (isset($_POST['evaluate'])) {


$subjectcode1 = $_GET['subjectcode'];
$professor = $row['professor'];
$session = $_SESSION['studentnumber'];
$q = $_POST['q']; //so eto $q dapat ma kuha ko lahat ng value ng radio button na selected on each question. hindi ko alam kung pano siya mag kaka value. wala naman syang error pero pagtingin ko sa database ang lalabas ay

------------------------------------------------------------------------------------------------------------------
subjectcode | rater | q1 | q2 | q3 | q4 | q5 |
------------------------------------------------------------------------------------------------------------------
math05 | juan | array | | | | |
------------------------------------------------------------------------------------------------------------------

ang gusto ko sana ay ganito ang maiinsert sa database

------------------------------------------------------------------------------------------------------------------
subjectcode | rater | q1 | q2 | q3 | q4 | q5 |
------------------------------------------------------------------------------------------------------------------
math05 | juan | 5 | 4 | 3 | 2 | 1 | //ang mga chineck ko na radio button each question . which means ang q1, q2, q3, q4, q5 ay nag correspond sa each
questions
------------------------------------------------------------------------------------------------------------------




$sql = "INSERT INTO studentresult (subjectcode, rater, q1, q2, q3, q4, q5)
VALUES('$subjectcode1','$session', '$q', '', '', '', '')";

if (mysqli_query($con, $sql)) {
echo "New record created successfully";

} else {
echo "Error: " . $sql . "<br>" . mysqli_error($con);
}

mysqli_close($con);

}
?>
 
Last edited:
lagay mo sya po sir sa form refer ka po d2 sir PHP Forms,

at sa table mo po sir is,

subjectcode | rater | Question_ID | rate |

math01 | Juan | Q-1011 | 5 |

Radio button po kasi sya kya isang value lng ang pweding ma select, baka po checkbox ung gusto nyu?
 
Back
Top Bottom