I want to Match the student’s level and student’s class(SQFLite) with the student’s level and student’s class (MySQL)when fetching questions in MySQL.
I have an external MySQL database (localhost) that contains a "questions" table characterized by their level (Easy, Medium, Hard) and their class(1st class, 2nd class, 3rd class). I have an internal database (SQFLite) that contains a "students" table with level and class attributes that contain in the external MySQL database. I want to post only the questions that correspond to the level and class of the student that is already added in the internal database "SQFLite".
Problem: I posted all the questions in the JSON form but did not match the level and class of the student currently added.
Requirements: I want when I add a new student with their level and class in the internal database"SQFLite", the questions that are displayed must be matched to that of the student.
Try.php : get questions in json form
<?php
header("Content-type: application/json; charset=utf-8");
try{
$conn = new PDO('mysql:host=localhost;dbname=relations;charset=utf8','root','');
$conn ->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exc){
echo $exc->getMessage();
die("could not connect");
}
?>
<?php
$makeQuery ="SELECT * FROM questions ";
$stamement = $conn-> prepare($makeQuery);
$stamement -> execute();
$myarray = array();
while ($resultsFrom = $stamement->fetch()){
array_push(
$myarray,array(
"idques"=>intval($resultsFrom['idques']),
"description"=>$resultsFrom['description']
)
);
}
echo json_encode($myarray);
?>
model.dart: contains the student's model
class Student {
int _id;
String _name;
String _level;
String _classes ;
String _date;
Student(this._name, this._level, this._classes, this._date);
Student.withId(this._id, this._name, this._level, this._classes ,this._date);
String get date => _date;
String get level => _level;
String get classes => _classes;
String get name => _name;
int get id => _id;
set date(String value) {
_date = value;
}
set level(String value) {
_level = value;
}
set classes(String value) {
_classes = value;
}
set name(String value) {
if (value.length <= 255) {
_name = value;
}
}
Map<String, dynamic> toMap() {
var map = Map<String, dynamic>();
map["id"] = this._id;
map["name"] = this._name;
map["level"] = this._level;
map["classes"] = this._classes;
map["date"] = this._date;
return map;
}
Student.getMap(Map<String, dynamic> map){
this._id = map["id"];
this._name = map["name"];
this._level = map["level"];
this._classes= map["classes"];
this._date = map["date"];
}
}
sql_helper.dart: contains the internal database(SQFLite)
import 'package:sqflite/sqflite.dart';
import 'dart:async';
import 'dart:io';
import 'model.dart';
import 'package:path_provider/path_provider.dart';
class SQL_Helper {
static SQL_Helper dbHelper;
static Database _database;
SQL_Helper._createInstance();
factory SQL_Helper() {
if (dbHelper == null) {
dbHelper = SQL_Helper._createInstance();
}
return dbHelper;
}
String tableName = "students_table";
String _id = "id";
String __name = "name";
String __level= "level";
String __classes = "classes";
String __date = "date";
Future<Database> get database async {
if (_database == null){
_database = await initializedDatabase();
}
return _database;
}
Future<Database> initializedDatabase() async {
Directory directory = await getApplicationDocumentsDirectory();
String path = directory.path + "students.db";
var studentDB = await openDatabase(path, version: 1, onCreate: createDatabase);
return studentDB;
}
void createDatabase(Database db, int version) async {
await db.execute(
"CREATE TABLE $tableName($_id INTEGER PRIMARY KEY AUTOINCREMENT, $__name TEXT, $__level TEXT,$__classes TEXT, $__date TEXT )");
}
Future<List<Map<String, dynamic>>> getStudentMapList() async {
Database db = await this.database;
var result = await db.query(tableName, orderBy: "$_id ASC");
return result;
}
Future<int> insertStudent(Student student) async {
Database db = await this.database;
var result = await db.insert(tableName, student.toMap());
return result;
}
Future<int> updateStudent(Student student) async{
Database db = await this.database;
var result = await db.update(tableName, student.toMap(), where: "$_id = ?", whereArgs: [student.id]);
return result;
}
Future<int> deleteStudent(int id) async {
var db = await this.database;
int result = await db.rawDelete("DELETE FROM $tableName WHERE $_id = $id");
return result;
}
Future<int> getCount() async {
Database db = await this.database;
List<Map<String, dynamic>> all = await db.rawQuery("SELECT COUNT (*) FROM $tableName");
int result = Sqflite.firstIntValue(all);
return result;
}
Future<List<Student>> getStudentList() async{
var studentMapList = await getStudentMapList();
int count = studentMapList.length;
List<Student> students = new List<Student>();
for (int i = 0; i <= count -1; i++){
students.add(Student.getMap(studentMapList[i]));
}
return students;
}
}
question.dart: contains questions list
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class Questions extends StatefulWidget {
@override
_QuestionsState createState() => _QuestionsState();
}
class _QuestionsState extends State<Questions> {
getMethod()async{
String theUrl = 'http://10.0.2.2/Try/Try.php';
var res = await http.get(Uri.encodeFull(theUrl),headers:{"Accept":"application/json"});
var responsBody = json.decode(res.body);
print(responsBody);
return responsBody;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title:Text("Questions", style: TextStyle(fontWeight: FontWeight.bold),) ,
backgroundColor: Colors.blue[700],
),
backgroundColor: Colors.blue[99],
body:FutureBuilder(
future: getMethod(),
builder: (BuildContext context , AsyncSnapshot snapshot){
List snap = snapshot.data;
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: CircularProgressIndicator(),
);
}
if(snapshot.hasError){
return Center(
child: Text("Error .... "),
);
}
return ListView.separated(
separatorBuilder: (context, index) => Divider(
color: Colors.indigo,
),
itemCount: snap.length,
itemBuilder: (context,index){
return ListTile(
title: Text(
"${snap[index]['description']}", style: TextStyle(fontSize: 18.0)),
leading:Icon(Icons.question_answer, color:Colors.green , size: 20, ) ,
);
},
);
},
),
);
}
}