View Full Version : Adding together with PHP...
Chris M
06-07-2003, 01:43 PM
Sounds silly, but anyway...
I have 4 fields in the user table, score1,score2,score3 and score4...
These store the scores from each stage of a test I make the user's take...
Anyway, I want to know how to add the contents of the fields together (also what field types I would need), so that the following will work:
$total = $DB_site->query("SELECT score1,score2,score3,score4 FROM user WHERE userid='$bbuserinfo[userid]'");
$addingup = "ADDING UP BIT";
$totalscore = "THE RESULT";
if ($totalscore<=6) {
$group=='9';
}elseif ($totalscore>=7 AND $totalscore<=10) {
$group=='11';
}elseif ($totalscore>=11 AND $totalscore<=13) {
$group=='10';
}else{
$group=='8';
}
$usergroupname = $DB_site->query("SELECT * FROM usergroup WHERE usergroupid='$group'");
$DB_site->query("UPDATE user SET usergroupid='$group' WHERE userid='$bbuserinfo[userid]'");
Any ideas?:)
Edit: Extra Info -
There are 4 questions, each with 4 possible answers, which are assigned a point value from 1 to 4, 4 being the highest and "best" answer, 1 being the lowest and "worst" answer (as such:p)
Satan
colicab-d
06-07-2003, 01:51 PM
why not fetch the scores by user id as you doing, but then have a bit of code like where the id matches the userid add the 4 scores together...
simple maths.. or am i missing something as usual lol
or let me guess thast what u wana know
try this :
while($r1 = mysql_fetch_array($total)){
$score1=$r1['score1'];
$score2=$r1['score2'];
$score3=$r1['score3'];
$score4=$r1['score4'];
$subtotal1 = $score1 + $score2 ;
$subtotal2 = $subtotal1 + $score3 ;
$finaltotal = $subtotal2 + $score4 ;
}
Chris M
06-07-2003, 01:57 PM
I'm not sure what you mean dave:confused:
The script inserts the score from each stage into a seperate field...
The script then needs to calculate the total score from those 4 fields, so that the user's usergroupid can be updated based on their score...
Satan
Chris M
06-07-2003, 02:29 PM
Ok I have the add-up code (which I believe works), but for some reason the fields:
question1
question2
question3
question4
and
score1
score2
score3
score4
are not being updated, and are blank:(
Satan
I'm not sure I understand?
$getscores=$DB_site->query("SELECT score1,score2,score3,score4 FROM user WHERE userid=$bbuserinfo[userid]");
$1=$getscores['score1'];
$2=$getscores['score2'];
$3=$getscores['score3'];
$4=$getscores['score4'];
$total=$1+$2+$3+$4;
That would work for adding up but I'm not sure what you're looking for?
Chris M
06-07-2003, 05:14 PM
Thanks N9ne, but I have the adding up bit;):)
My problem now is that for some reason the database is not being given the values of the variables:(
I'll figure it out or die trying:p
Thanks for your help though;):)
Satan
So you want score1,score2,score3,score4 fields in the user table to be updated with the new scores?
$update=$DB_site->query("UPDATE user SET score1=$1, score2=$2, score3=$3, score4=$4 WHERE userid=$bbuserinfo[userid]");
I don't understand what you mean though lol...please explain a little more and I can try and help :)
filburt1
06-07-2003, 06:15 PM
Maybe I'm completely confused but:
SELECT score1 + score2 + score3 + score4 AS totalscore FROM user WHERE userid = userid;
Dean C
06-07-2003, 06:18 PM
I told you how to do this earlier ;). Have the main page
if($action=="page1") {
$score = calculatescorehereandassignittovariable();
}
if($action=="page2" and isset($score)) {
calculatenewscoreandassignittoavariable();
}
And so on... :)
- miSt
Chris M
06-07-2003, 07:48 PM
Hehe - The counting is no longer the problem...
I now have a problem with this code:
// ############################### start update4 ###############################
if ($action=="update4") {
$qid = intval($question4[qid]);
$question4 = intval($question4[question]);
$ganswer = intval($question4[ganswer]);
$sanswer = intval($question4[sanswer]);
$hanswer = intval($question4[hanswer]);
$ranswer = intval($question4[ranswer]);
$answer = intval($answer);
$answer1 = intval($answer1);
$answer2 = intval($answer2);
$answer3 = intval($answer3);
$answer4 = intval($answer4);
if ($answer==$ganswer) {
$whichscore = 'gscore';
}elseif ($answer==$sanswer) {
$whichscore = 'sscore';
}elseif ($answer==$hanswer) {
$whichscore = 'hscore';
}else{
$whichscore = 'rscore';
}
$score = $DB_site->query("SELECT $whichscore FROM *tablename*_questions WHERE qid='$qid'");
if ($bbuserinfo['usergroupid']==2)
{
$DB_site->query("UPDATE user SET question4='$qid',score4='$score[$whichscore]' WHERE userid='$bbuserinfo[userid]'");
}
$total = $DB_site->query("SELECT score1,score2,score3,score4 FROM user WHERE userid='$bbuserinfo[userid]'");
while($r1 = mysql_fetch_array($total)){
$score1=$r1['score1'];
$score2=$r1['score2'];
$score3=$r1['score3'];
$score4=$r1['score4'];
$subtotal1 = $score1 + $score2 ;
$subtotal2 = $subtotal1 + $score3 ;
$finaltotal = $subtotal2 + $score4 ;
}
if ($finaltotal<=6) {
$group=='9';
}elseif ($finaltotal>=7 AND $finaltotal<=10) {
$group=='11';
}elseif ($finaltotal>=11 AND $finaltotal<=13) {
$group=='10';
}else{
$group=='8';
}
$usergroupname = $DB_site->query("SELECT * FROM usergroup WHERE usergroupid='$group'");
$DB_site->query("UPDATE user SET usergroupid='$group' WHERE userid='$bbuserinfo[userid]'");
$goto="index.php?s=$session[sessionhash]";
eval("standardredirect(\"".gettemplate("congrats_sorted")."\",\"$goto\");");
}
$qid = the id of the question (doesn't appear to be intval'ing even though I have tried many different ways)
$question4 = the 4th question
$ganswer = one of the answer options
$sanswer = another answer option
$hanswer = another answer option
$ranswer = another answer option
$answer = the answer they selected
$answer1 = the 1st answer they could have chosen
$answer2 = the 2nd answer they could have chosen
$answer3 = the 3rd answer they could have chosen
$answer4 = the 4th answer they could have chosen
Thats pretty much all the explaination I can give - Here is the "question4" code:
// ############################### start *tablename* ###############################
if ($action=="sort4") {
$getqs4 = $DB_site->query("SELECT * FROM *tablename*_questions ORDER BY RAND() LIMIT 1");
while ($question4=$DB_site->fetch_array($getqs4)) {
$answers4 = array($question4[ganswer],$question4[sanswer]);
$answers24 = array($question4[hanswer],$question4[ranswer]);
$nbvalue4 = 1;
$answerd4 = array_rand($answers4,$nbvalue4);
if ($answerd4=="$question4[ganswer]") {
$answer1 = "$question4[ganswer]";
$answer2 = "$question4[sanswer]";
}else{
$answer1 = "$question4[sanswer]";
$answer2 = "$question4[ganswer]";
}
$answert4 = array_rand($answers24,$nbvalue4);
if ($answert4=="$question4[hanswer]") {
$answer3 = "$question4[hanswer]";
$answer4 = "$question4[ranswer]";
}else{
$answer3 = "$question4[ranswer]";
$answer4 = "$question4[hanswer]";
}
eval("\$thequestions .= \"".gettemplate('*tablename*_questions4')."\";");
}
eval("dooutput(\"".gettemplate('*tablename*_q4')."\");");
}
Here is the HTML code which is in "*tablename*_q4" and "*tablename*_questions4":)
*tablename*_q4
<html>
<head>
<title>$bbtitle - *file name*</title>
$headinclude
</head>
$header
<body>
<!-- breadcrumb -->
<table border="0" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td width="100%"><br>
<normalfont><b><a href="index.php?s=$session[sessionhash]">$bbtitle</a>
> *File name* - Question 4</b></normalfont></td>
</tr>
</table>
<!-- /breadcrumb -->
<form action="*filename*.php" method="post">
<input type="hidden" name="s" value="$session[sessionhash]">
$thequestions
<br>
<table cellpadding="2" cellspacing="0" border="0" width="95%" align="center">
<tr>
<td align="center"><normalfont>
<input type="hidden" name="action" value="update4">
<input type="submit" class="bginput" name="Submit" value="Submit">
<input type="reset" class="bginput" name="Reset" value="Reset">
</normalfont></td>
</tr>
</table>
</form>
$footer
</body>
</html>
*tablename*_questions4
<table cellpadding="0" cellspacing="0" border="0" bgcolor="#0A293E" width="95%" align="center"><tr><td>
<table cellpadding="4" cellspacing="1" border="0" width="100%">
<tr>
<td bgcolor="#1D6AA0" colspan="2"><normalfont color="#EEEEFF"><b>The Question</b></normalfont></td>
</tr>
<tr>
<td bgcolor="#1C5780"><normalfont><b>Quesion: $question4[question]?</b></normalfont></td>
</tr>
<tr>
<td bgcolor="#13486D"><normalfont>
<input type="radio" name="answer" value="answer1" checked>$answer1<br>
<input type="radio" name="answer" value="answer2">$answer2<br>
<input type="radio" name="answer" value="answer3">$answer3<br>
<input type="radio" name="answer" value="answer4">$answer4<br>
</normalfont></td>
</tr>
</table>
</td></tr></table>
Can you see anything wrong?:)
*tablename* = The name of the table
Problem I think it is: - "$qid" will not intval, so the"$whichscore" will not work:(
Satan
Vars are being passed on through a form; make sure that all the vars that you are using are in the previous page, in input tags, as the name="xxx" part, if they aren't, then if you use the variable on the next page, it won't work.
E.G.
<form type="post" blah blah blah>
<input NAME="BLAHBLAH" lkjsdlfkjsdlkjsdf>sldkfsdlk
<input type="submit">sdfsdf
</form>
next page:
if ($BLAHBLAH=="1") {
$elephants="fly";
}
That will work, because BLAHBLAH is the name of the input field on the previous page.
Chris M
06-07-2003, 08:01 PM
So if in the *tablename*_q4 template I were to add:
<input type="hidden" name="qid" value="$question4[qid]">you are telling me that would intval the $qid variable?
If so, it doesn't - I've already tried:(
Satan
No it won't intval it or anything...i'm just confused. What is working, and what isn't working?
Chris M
06-07-2003, 08:07 PM
AFAIK the count works...
The inserting the question's id (qid) into the question1, question2, question3 and question4 fields are not, and subsequently, because the $qid variable is not being intval'ed, the $whichscore will not work as that relies on the $qid variable also...
AKA - None of it works:(
Satna
Try not to intval $qid, then see what $whichscore outputs...
Chris M
06-07-2003, 08:13 PM
Absolutely nothing...
Satan
Chris M
06-08-2003, 07:33 AM
So can anyone help?:)
Satan
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.