Цитата (Loki @ 25.12.2015 - 09:22) | ||
Не обольщайтесь, ваш сопливый нос не интересен настолько, чтоб его утирать. |
$difficulty = 1;случайный вопрос я выбираю при помощи rand.
$rand = rand(1,5);
$query = "(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=1 AND q_id=$rand AND difficulty=$difficulty LIMIT 1)
UNION
(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=0 AND q_id=$rand AND difficulty=$difficulty ORDER BY RAND() LIMIT 3)
ORDER BY RAND()
";
Цитата (Valick @ 11.11.2015 - 00:12) |
Цитата (maruo @ 11.11.2015 - 00:07) $load_test =++ $_SESSION['count']; писать так это тяжкий грех для программиста возьмите за правило, что id можно только получить из БД его нельзя сформировать на уровне РНР, а потом сделать выборку из БД основанную на умозаключениях. |
Цитата |
$rand = rand(1,5); это из той же оперы |
$query2 = "(SELECT q_id FROM question ORDER BY RAND() LIMIT 1)";
$qrand = mysql_query($query2) or die(mysql_error());
$query = "(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=1 AND q_id=$qrand LIMIT 1)
UNION
(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=0 AND q_id=$qrand ORDER BY RAND() LIMIT 3)
ORDER BY RAND()
";
Цитата (maruo @ 31.12.2015 - 14:08) |
в чем ошибка |
$query = mysql_query("SELECT q_id FROM question ORDER BY RAND() LIMIT 1",$db);
$qrand = mysql_fetch_row($query);
$qrand = int($qrand[q_id]);
Цитата |
$qrand = int($qrand[q_id]); |
foreach ($qrand1 as $qrand) {
$qrand = (int)$qrand;
<?
error_reporting(E_ALL);
header('content-type: text/html; charset=utf-8');
include_once("bd.php");
include("answer.php");
$end_line = "\n";
$difficulty = 1;
if($_SESSION['count'] == 5){
$_SESSION['count'] = 0;
$difficulty++ and $_SESSION['count'] = 0;
}
$query = mysql_query("SELECT q_id FROM question WHERE difficulty=$difficulty ORDER BY RAND() LIMIT 1",$db);
$qrand1 = mysql_fetch_assoc($query);
foreach ($qrand1 as $qrand) {
$qrand = (int)$qrand;
}
$query = "(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=1 AND q_id=$qrand LIMIT 1)
UNION
(SELECT q.q_id, q.q_quest, a.a_id, a.a_answer, a.a_correct
FROM answer a
INNER JOIN question q USING(q_id) WHERE a.a_correct=0 AND q_id=$qrand ORDER BY RAND() LIMIT 3)
ORDER BY RAND()
";
$input = null;
$res = mysql_query($query) or die(mysql_error());
if($res){
if(mysql_num_rows($res) > 0){
while($row=mysql_fetch_assoc($res)){
$quest = $row['q_quest'];
$hidden = ' <input type="hidden" name="quest" value="'.$row['q_id'].'">'.$end_line;
$input .= '<button name="answer" value ="'.$row['a_id'].'"> '.$row['a_answer'].' </button>'.$end_line;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h4><?php echo 'score'.$end_line.$_SESSION['count'];?></h4>
<h2><?php echo $quest;?></h2>
<form action = "answer.php" method = "POST">
<?php echo $hidden.$input;?>
</form>
</body>
</html>
<?
include_once('bd.php');
error_reporting(E_ALL);
session_start();
$q_id = isset($_POST['quest'])?(int)$_POST['quest']:0;
$a_id = isset($_POST['answer'])?(int)$_POST['answer']:false;
if($q_id && $a_id){
$query = "SELECT a_correct, q_quest, (SELECT MAX(q_id) FROM question) m_id
FROM answer
LEFT JOIN question USING(q_id)
WHERE q_id=$q_id
AND a_id=$a_id";
$res = mysql_query($query) or die(mysql_error());
if($res){
$row = mysql_fetch_assoc($res);
if($row['a_correct']==1 and isset($_POST['answer'])){
$_SESSION['count'] ++;
header("Location: quest.php");
}else{
$_SESSION['count'] = 0;
$difficulty = 1;
header("Location: index.php");
}
$submit = '<input type="submit" value="Еще раз">';
if($q_id >= $row['m_id']){
$q_id = '0';
$submit = '<input type="submit" value="Пройти тест ещё раз">';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<h2><?php echo $message;?></h2>
<form action="quest.php" method="POST">
<input type="hidden" name="quest" value="<?php echo $q_id;?>">
<p><?php echo $submit;?></p>
</form>
</body>
</html>
<?php
}
}
?>