Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 06-07-2003, 01:43 PM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Adding together with PHP...

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:

Code:
$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)

Satan
Reply With Quote
  #2  
Old 06-07-2003, 01:51 PM
colicab-d's Avatar
colicab-d colicab-d is offline
 
Join Date: Dec 2002
Location: Glasgow
Posts: 382
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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 :

PHP Code:
    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 ;


Reply With Quote
  #3  
Old 06-07-2003, 01:57 PM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not sure what you mean dave

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
Reply With Quote
  #4  
Old 06-07-2003, 02:29 PM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
Reply With Quote
  #5  
Old 06-07-2003, 05:09 PM
N9ne N9ne is offline
 
Join Date: Feb 2002
Posts: 1,495
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm not sure I understand?

PHP Code:
$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?
Reply With Quote
  #6  
Old 06-07-2003, 05:14 PM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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

Thanks for your help though

Satan
Reply With Quote
  #7  
Old 06-07-2003, 05:36 PM
N9ne N9ne is offline
 
Join Date: Feb 2002
Posts: 1,495
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So you want score1,score2,score3,score4 fields in the user table to be updated with the new scores?

PHP Code:
$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
Reply With Quote
  #8  
Old 06-07-2003, 06:15 PM
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Location: Maryland, US
Posts: 6,144
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Maybe I'm completely confused but:
[sql]
SELECT score1 + score2 + score3 + score4 AS totalscore FROM user WHERE userid = userid;
[/sql]
Reply With Quote
  #9  
Old 06-07-2003, 06:18 PM
Dean C's Avatar
Dean C Dean C is offline
 
Join Date: Jan 2002
Location: England
Posts: 9,071
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I told you how to do this earlier . Have the main page

PHP Code:
if($action=="page1") {
$score calculatescorehereandassignittovariable();

PHP Code:
if($action=="page2" and isset($score)) {
calculatenewscoreandassignittoavariable();

And so on...

- miSt
Reply With Quote
  #10  
Old 06-07-2003, 07:48 PM
Chris M's Avatar
Chris M Chris M is offline
 
Join Date: Dec 2001
Location: Northampton, England
Posts: 6,186
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hehe - The counting is no longer the problem...

I now have a problem with this code:

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:

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
Code:
<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> 
      &gt; *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
Code:
<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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:47 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04260 seconds
  • Memory Usage 2,289KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (5)bbcode_code
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete