目錄
helloweba" >helloweba
如何使用jQuery+PHP+MySQL来实现一个在线测试项目" >如何使用jQuery+PHP+MySQL来实现一个在线测试项目
回复讨论(解决方案)
Failed to parse questions.
' + judgeSkills(json.score) + '
 您的分数: ' + json.score + '
首頁 後端開發 php教程 昨晚忙到凌晨3点都没解决的问题,大侠,今天晚能不熬夜不?HELP!

昨晚忙到凌晨3点都没解决的问题,大侠,今天晚能不熬夜不?HELP!

Jun 20, 2016 pm 12:43 PM

想做一个在线答题系统,看看网上有个现成的,拿来试试,下载源码上传到sina云,为什么和原创的不一样呢?(答完题后没分数)
我的sina云地址:http://1.xyqcsmwly.sinaapp.com/quizphp/quiz.php

原创的演示地址:http://www.helloweba.com/demo/quiz/quiz.php

我的代码:
quiz.php

<?phpinclude_once("connect.php");$sql = "select * from quiz order by id asc";$query = mysql_query($sql);while($row=mysql_fetch_array($query)){	$answers = explode('###',$row['answer']);	$arr[] = array(		'question' => $row['id'].'、'.$row['question'],		'answers' => $answers	);}$json = json_encode($arr);?><!DOCTYPE HTML><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><title>演示:如何使用jQuery+PHP+MySQL来实现一个在线测试项目</title><meta name="keywords" content="jquery,php,mysql" /><meta name="description" content="Helloweba文章结合实例演示HTML5、CSS3、jquery、PHP等WEB技术应用。" /><link rel="stylesheet" type="text/css" href="main.css" /><link rel="stylesheet" type="text/css" href="styles.css" /><style type="text/css">.demo{width:760px; margin:60px auto 10px auto}</style><script src="my.js"></script><script src="quizs.js"></script><script>$(function(){	$('#quiz-container').jquizzy({        questions: <?php echo $json;?>,		sendResultsURL: 'data.php'    });});</script></head><body>	<div id="header">   <div id="logo"><h1 id="a-nbsp-href-http-www-helloweba-com-nbsp-title-返回helloweba首页-helloweba-a"><a href="http://www.helloweba.com" title="返回helloweba首页">helloweba</a></h1></div>   <div class="demo_topad"><script src="/js/ad_js/demo_topad.js" type="text/javascript"></script></div></div><div id="main">   <h2 id="a-nbsp-href-http-www-helloweba-com-view-blog-html-如何使用jQuery-PHP-MySQL来实现一个在线测试项目-a"><a href="http://www.helloweba.com/view-blog-297.html">如何使用jQuery+PHP+MySQL来实现一个在线测试项目</a></h2>	<div class="demo">		<div id='quiz-container'></div>	</div>	<div class="ad_76090"><script src="/js/ad_js/bd_76090.js" type="text/javascript"></script></div><br/></div><div id="footer">    <p>Powered by helloweba.com  允许转载、修改和使用本站的DEMO,但请注明出处:<a href="http://www.helloweba.com">www.helloweba.com</a></p></div><p id="stat"><script type="text/javascript" src="/js/tongji.js"></script></p></body></html>
登入後複製


connect.php
<?php$host=SAE_MYSQL_HOST_M;$port=SAE_MYSQL_PORT;$db_user=SAE_MYSQL_USER;$db_pass=SAE_MYSQL_PASS;$db_name=SAE_MYSQL_DB;$link=mysql_connect($host.':'.$port,$db_user,$db_pass);mysql_select_db($db_name,$link);mysql_query("SET names UTF8");header("Content-Type: text/html; charset=utf-8");date_default_timezone_set($timezone); //北京时间?>
登入後複製


data.php
<?phpinclude_once("connect.php");$data = $_REQUEST['an'];$answers = explode('|',$data);$an_len = count($answers)-1; //题目数$sql = "select correct from quiz order by id asc";$query = mysql_query($sql);$i = 0;$score = 0; //初始得分$q_right = 0; //答对的题数while($row=mysql_fetch_array($query)){	if($answers[$i]==$row['correct']){		$arr['res'][] = 1;		$q_right += 1;	}else{		$arr['res'][] = 0;	}	$i++;}$arr['score'] = round(($q_right/$an_len)*100); //总得分echo json_encode($arr);?>
登入後複製
登入後複製


回复讨论(解决方案)

data.php

<?phpinclude_once("connect.php");$data = $_REQUEST['an'];$answers = explode('|',$data);$an_len = count($answers)-1; //题目数$sql = "select correct from quiz order by id asc";$query = mysql_query($sql);$i = 0;$score = 0; //初始得分$q_right = 0; //答对的题数while($row=mysql_fetch_array($query)){	if($answers[$i]==$row['correct']){		$arr['res'][] = 1;		$q_right += 1;	}else{		$arr['res'][] = 0;	}	$i++;}$arr['score'] = round(($q_right/$an_len)*100); //总得分echo json_encode($arr);?>
登入後複製
登入後複製

quizs.js
(function($) {    $.fn.jquizzy = function(settings) {        var defaults = {            questions: null,            startImg: 'images/start.gif',            endText: '已结束!',            shortURL: null,            sendResultsURL: null,            resultComments: {                perfect: '你是爱因斯坦么?',                excellent: '非常优秀!',                good: '很好,发挥不错!',                average: '一般般了。',                bad: '太可怜了!',                poor: '好可怕啊!',                worst: '悲痛欲绝!'            }        };        var config = $.extend(defaults, settings);        if (config.questions === null) {            $(this).html('<div class="intro-container slide-container"><h2 id="Failed-nbsp-to-nbsp-parse-nbsp-questions">Failed to parse questions.</h2></div>');            return;        }        var superContainer = $(this),        answers = [],        introFob = '	<div class="intro-container slide-container"><a class="nav-start" href="#">请认真完成测试题。准备好了吗?<br/><br/><span><img  src="/static/imghw/default1.png"  data-src="'+config.startImg+'"  class="lazy"  +config.startImg+'" alt="昨晚忙到凌晨3点都没解决的问题,大侠,今天晚能不熬夜不?HELP!" ></span></a></div>	',        exitFob = '<div class="results-container slide-container"><div class="question-number">' + config.endText + '</div><div class="result-keeper"></div></div><div class="notice">请选择一个选项!</div><div class="progress-keeper" ><div class="progress"></div></div>',        contentFob = '',        questionsIteratorIndex,        answersIteratorIndex;        superContainer.addClass('main-quiz-holder');        for (questionsIteratorIndex = 0; questionsIteratorIndex < config.questions.length; questionsIteratorIndex++) {            contentFob += '<div class="slide-container"><div class="question-number">' + (questionsIteratorIndex + 1) + '/' + config.questions.length + '</div><div class="question">' + config.questions[questionsIteratorIndex].question + '</div><ul class="answers">';            for (answersIteratorIndex = 0; answersIteratorIndex < config.questions[questionsIteratorIndex].answers.length; answersIteratorIndex++) {                contentFob += '<li>' + config.questions[questionsIteratorIndex].answers[answersIteratorIndex] + '</li>';            }            contentFob += '</ul><div class="nav-container">';            if (questionsIteratorIndex !== 0) {                contentFob += '<div class="prev"><a class="nav-previous" href="#">< 上一题</a></div>';            }            if (questionsIteratorIndex < config.questions.length - 1) {                contentFob += '<div class="next"><a class="nav-next" href="#">下一题 ></a></div>';            } else {                contentFob += '<div class="next final"><a class="nav-show-result" href="#">完成</a></div>';            }            contentFob += '</div></div>';            answers.push(config.questions[questionsIteratorIndex].correctAnswer);        }        superContainer.html(introFob + contentFob + exitFob);        var progress = superContainer.find('.progress'),        progressKeeper = superContainer.find('.progress-keeper'),        notice = superContainer.find('.notice'),        progressWidth = progressKeeper.width(),        userAnswers = [],        questionLength = config.questions.length,        slidesList = superContainer.find('.slide-container');        function checkAnswers() {            var resultArr = [],            flag = false;            for (i = 0; i < answers.length; i++) {                if (answers[i] == userAnswers[i]) {                    flag = true;                } else {                    flag = false;                }                resultArr.push(flag);            }            return resultArr;        }        function roundReloaded(num, dec) {            var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);            return result;        }        function judgeSkills(score) {            var returnString;            if (score === 100) return config.resultComments.perfect;            else if (score > 90) return config.resultComments.excellent;            else if (score > 70) return config.resultComments.good;            else if (score > 50) return config.resultComments.average;            else if (score > 35) return config.resultComments.bad;            else if (score > 20) return config.resultComments.poor;            else return config.resultComments.worst;        }        progressKeeper.hide();        notice.hide();        slidesList.hide().first().fadeIn(500);        superContainer.find('li').click(function() {            var thisLi = $(this);            if (thisLi.hasClass('selected')) {                thisLi.removeClass('selected');            } else {                thisLi.parents('.answers').children('li').removeClass('selected');                thisLi.addClass('selected');            }        });        superContainer.find('.nav-start').click(function() {            $(this).parents('.slide-container').fadeOut(500,            function() {                $(this).next().fadeIn(500);                progressKeeper.fadeIn(500);            });            return false;        });        superContainer.find('.next').click(function() {            if ($(this).parents('.slide-container').find('li.selected').length === 0) {                notice.fadeIn(300);                return false;            }            notice.hide();            $(this).parents('.slide-container').fadeOut(500,            function() {                $(this).next().fadeIn(500);            });            progress.animate({                width: progress.width() + Math.round(progressWidth / questionLength)            },            500);            return false;        });        superContainer.find('.prev').click(function() {            notice.hide();            $(this).parents('.slide-container').fadeOut(500,            function() {                $(this).prev().fadeIn(500);            });            progress.animate({                width: progress.width() - Math.round(progressWidth / questionLength)            },            500);            return false;        });        superContainer.find('.final').click(function() {            if ($(this).parents('.slide-container').find('li.selected').length === 0) {                notice.fadeIn(300);                return false;            }            superContainer.find('li.selected').each(function(index) {                userAnswers.push($(this).parents('.answers').children('li').index($(this).parents('.answers').find('li.selected')) + 1);            });						progressKeeper.hide();			var resultSet = '';			            if (config.sendResultsURL !== null) {                var collate = [];				var myanswers = '';                for (r = 0; r < userAnswers.length; r++) {                    collate.push('{"questionNumber":"' + parseInt(r + 1, 10) + '", "userAnswer":"' + userAnswers[r] + '"}');					myanswers = myanswers + userAnswers[r]+'|';                }								$.getJSON(config.sendResultsURL,{an:myanswers},function(json){					if(json==null){						alert('通讯失败!');					}else{												var corects = json['res'];						$.each(corects,function(index,array){							resultSet += '<div class="result-row">' + (corects[index] === 1 ? "<div class='correct'>#"+(index + 1)+"<span></span></div>": "<div class='wrong'>#"+(index + 1)+"<span></span></div>")+'</div>';						});						resultSet = '<h2 id="nbsp-nbsp-judgeSkills-json-score-nbsp-nbsp-br-nbsp-您的分数-nbsp-nbsp-nbsp-json-score-nbsp-nbsp">' + judgeSkills(json.score) + '<br/> 您的分数: ' + json.score + '</h2><div class="jquizzy-clear"></div>' + resultSet + '<div class="jquizzy-clear"></div>';																		superContainer.find('.result-keeper').html(resultSet).show(500);					}					});            }			            //superContainer.find('.resultsview-qhover').hide();                        $(this).parents('.slide-container').fadeOut(500,            function() {                $(this).next().fadeIn(500);            });            return false;        });    };})(jQuery);
登入後複製

文件目录:

原创在线答题源码地址:http://www.helloweba.com/downsoft/297/

大侠们,帮忙看看,谢谢!

quiz.sql

-- phpMyAdmin SQL Dump-- version 3.2.0.1-- http://www.phpmyadmin.net---- 主机: localhost-- 生成日期: 2015 年 04 月 04 日 13:49-- 服务器版本: 5.5.8-- PHP 版本: 5.3.3SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;/*!40101 SET NAMES utf8 */;---- 数据库: `lrfbeyond_demo`---- ------------------------------------------------------------ 表的结构 `quiz`--CREATE TABLE IF NOT EXISTS `quiz` (  `id` int(11) NOT NULL AUTO_INCREMENT,  `question` varchar(100) NOT NULL,  `answer` varchar(500) NOT NULL,  `correct` tinyint(2) NOT NULL,  PRIMARY KEY (`id`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;---- 转存表中的数据 `quiz`--INSERT INTO `quiz` (`id`, `question`, `answer`, `correct`) VALUES(1, '罗马帝国曾一度辉煌,令人神往,故有“条条大陆通罗马”一说。那么,今天你是怎样理解这一谚语的准确含义的?', 'A.入乡随俗 ###B.四通八达 ###C.殊途同归 ###D.流连忘返', 3),(2, '找出不同类的一项:', 'A.斑马 ###B.军马 ###C.赛马 ###D.骏马 ###E.驸马', 5),(3, ' 蜡烛在空气中燃烧,蜡烛质量逐渐变小。这说明', 'A.物质可以自生自灭###B.发生的不是化学变化###C.不遵守质量守恒定律###D.生成物为气体,散发到空气中了', 4),(4, '以下哪位歌手没有获得过《我是歌手》总冠军?', 'A.羽泉###B.韩磊###C.邓紫棋###D.韩红', 3),(5, '下列哪个标签不是HTML5中的新标签?', 'A.<article>###B.<canvas>###C.<section>###D.<sub>', 4);
登入後複製

感谢版主大人xuzuning的回复,补齐4个文件,仍不能统计出结果。
补齐4个文件后的测试连接:http://2.xyqcsmwly.sinaapp.com/quiz.php

Warning: Cannot modify header information - headers already sent by (output started at /data1/www/htdocs/795/xyqcsmwly/2/quiz.php:1) in connect.php on line 12


你自己 F12

感谢,xuzuning,程序已正常运行。估计和SINA云身份认证有关,谢谢!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? 說明PHP中的安全密碼散列(例如,password_hash,password_verify)。為什麼不使用MD5或SHA1? Apr 17, 2025 am 12:06 AM

在PHP中,應使用password_hash和password_verify函數實現安全的密碼哈希處理,不應使用MD5或SHA1。1)password_hash生成包含鹽值的哈希,增強安全性。 2)password_verify驗證密碼,通過比較哈希值確保安全。 3)MD5和SHA1易受攻擊且缺乏鹽值,不適合現代密碼安全。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? PHP類型提示如何起作用,包括標量類型,返回類型,聯合類型和無效類型? Apr 17, 2025 am 12:25 AM

PHP類型提示提升代碼質量和可讀性。 1)標量類型提示:自PHP7.0起,允許在函數參數中指定基本數據類型,如int、float等。 2)返回類型提示:確保函數返回值類型的一致性。 3)聯合類型提示:自PHP8.0起,允許在函數參數或返回值中指定多個類型。 4)可空類型提示:允許包含null值,處理可能返回空值的函數。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

See all articles