<?php
ini_set("display_errors", 1);
error_reporting(-1);
require_once 'config.php';
require_once 'functions.php';
/** cписок тестов **/
$tests = get_tests();
if( isset($_GET['test'])) {
$test_id= (int)$_GET['test'];
$test_data = get_test_data($test_id);
if( is_array($test_data) ) {
$count_questions =count($test_data);
}
}
<div class="wrap">
<?php if ( $tests ): ?>
<h3>Варианты тестов</h3>
<?php foreach($tests as $test): ?>
<p><a href="?test=<?=$test['id']?>"><?=$test['test_name']?></a></p>
<?php endforeach; ?>
<div class="voprosi">
<?php if( isset($test_data)): ?>
<?php if( is_array($test_data)): ?>
<p>Всего вопросов: <?=$count_questions?></p>
<div class="test-data">
<?php print_arr($test_data) ?>
</div>
<?php else:// is_array($test_data) ?>
Тест в разработке
<?php endif;// is_array($test_data) ?>
<?php else:// isser($test_data) ?>
Выбирите тест
<?php endif;// isser($test_data) ?>
</div>
<?php else: //$tests?>
<h3>Нет тестов</h3>
<?php endif; //$tests?>
</div>
<?php
/** Распечатка массива **/
function print_arr($arr) {
echo '<pre>' .print_r($arr, true) . '</pre>';
}
/** получение списка тестов **/
function get_tests() {
global $db;
$query = "SELECT * FROM test";
$res = mysqli_query($db, $query);
if(!$res) return false;
$data = array();
while($row = mysqli_fetch_assoc($res)) {
$data[] = $row;
}
return $data;
}
/** получение данных теста **/
function get_test_data($test_id) {
if( !$test_id ) return;
global $db;
$query = "SELECT q.question, q.parent_test, a.id, a.answers, a.parent_question
FROM questions q
LEFT JOIN answers a
ON q.id = a.parent_question
WHERE q.parent_test = $test_id";
$res = mysqli_query($db, $query);
$data = null;
while($row = mysqli_fetch_assoc($res)) {
if( !$row['parent_question']) return false;
$data[$row['parent_question']][0] = $row['question'];
$data[$row['parent_question']][$row['id']] = $row['answers'];
}
return $data;
}
<?php
define("HOST", "Localhost");
define("USER", "root");
define("PASS", "");
define("DB", "Testing");
$db = @mysqli_connect(HOST, USER, PASS, DB) or die('Нет соеденения с БД');
mysqli_set_charset($db, 'utf8') or die ('utf-8 error');
?>