Jump to content

Royal

Members
  • Posts

    10
  • Joined

  • Last visited

Recent Profile Visitors

The recent visitors block is disabled and is not being shown to other users.

Royal's Achievements

Member

Member (2/5)

0

Reputation

  1. <?php // Assuming you have a database connection established // Fetch student records from the database $query = "SELECT * FROM students"; $result = mysqli_query($connection, $query); // Create an empty array to store the student records $students = []; // Check if the query was successful and fetch the data if ($result) { while ($row = mysqli_fetch_assoc($result)) { $students[] = $row; } } else { // Handle the query error echo "Error: " . mysqli_error($connection); } // Sort the students array based on totalexamscore in descending order usort($students, function ($a, $b) { if ($a['totalexamscore'] === $b['totalexamscore']) { return 0; } return ($b['totalexamscore'] > $a['totalexamscore']) ? 1 : -1; }); // Calculate the position of each student $position = 1; $prevScore = null; foreach ($students as &$student) { if ($student['totalexamscore'] !== $prevScore) { $prevScore = $student['totalexamscore']; $student['position'] = $position; } $position++; } // HTML table generation echo '<table>'; echo '<tr> <th>Student ID</th> <th>Name</th> <th>Total Exam Score</th> <th>Subject 1 Test Score</th> <th>Subject 1 Exam Score</th> <th>Subject 2 Test Score</th> <th>Subject 2 Exam Score</th> <th>Subject 3 Test Score</th> <th>Subject 3 Exam Score</th> <th>Position</th> </tr>'; foreach ($students as $student) { echo '<tr>'; echo '<td>' . $student['student_id'] . '</td>'; echo '<td>' . $student['student_name'] . '</td>'; echo '<td>' . $student['totalexamscore'] . '</td>'; echo '<td>' . $student['subject1testscore'] . '</td>'; echo '<td>' . $student['subject1examscore'] . '</td>'; echo '<td>' . $student['subject2testscore'] . '</td>'; echo '<td>' . $student['subject2examscore'] . '</td>'; echo '<td>' . $student['subject3testscore'] . '</td>'; echo '<td>' . $student['subject3examscore'] . '</td>'; echo '<td>' . $student['position'] . '</td>'; echo '</tr>'; } echo '</table>'; // Close the database connection mysqli_close($connection); ?> Please i need to know if the code above will be able to sort the array results , calculate positions based on totalexamscore and also create a tie position for students with same score.
  2. Hello guys, i need help to generate students class position in a class broadsheet based on their individual exam totals, the results are being populated from the database and everything works fine but i do not know how to generate the position on the fly from the grand totals generated from the database and store in a $total variable for each student. below is my code <?php include_once('../controller/config.php'); if (isset($_POST['view'])) { $term1 = $_POST['term']; $year1 = $_POST['year']; $class1 = $_POST['class']; $sql1 = "SELECT * FROM student_exam_result where year = '$year1' and class = '$class1' and term = '$term1'"; $result1=mysqli_query($conn,$sql1); $row1=mysqli_fetch_assoc($result1); $subject1 = $row1['subject1']; $subject2 = $row1['subject2']; $subject3 = $row1['subject3']; $subject4 = $row1['subject4']; } ?> <h3 style="text-align:justify !important; font-weight:bolder" class="box-title"><?php echo "$term1 ";?>RESULT BROADSHEET FOR CLASS <?php echo "$class1 ";?><?php echo"$year1";?> </h3> </div><!-- /.box-header --> <div class="box-body table-responsive"> <table style="width:100%" id="example1" class="table table-bordered table-striped"> <thead> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-1">ID</th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Student Name</th><th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Index Number</th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject1; ?></th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject2; ?></th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject3; ?></th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3"><?php echo $subject4; ?></th> <th style="background-color: gray;color: white; text-transform: uppercase;" class="col-md-3">Grand Total</th> <th style="background-color: gray;color: white; text-transform: uppercase;"class="col-md-8">Class Position</th> </thead> <tbody> <?php include_once('../controller/config.php'); if (isset($_POST['view'])) { $term = $_POST['term']; $year = $_POST['year']; $class = $_POST['class']; $sql = "SELECT * FROM student_exam_result where year = '$year' and class = '$class' and term = '$term' ORDER BY id DESC"; $result=mysqli_query($conn,$sql); $count=0; if(mysqli_num_rows($result) > 0){ while($row=mysqli_fetch_assoc($result)){ $count++; $subject1_total = $row['subject1_test1_score'] + $row['subject1_test2_score'] + $row['subject1_exam_score']; $subject2_total = $row['subject2_test1_score'] + $row['subject2_test2_score'] + $row['subject2_exam_score']; $subject3_total = $row['subject3_test1_score'] + $row['subject3_test2_score'] + $row['subject3_exam_score']; $subject4_total = $row['subject4_test1_score'] + $row['subject4_test2_score'] + $row['subject4_exam_score']; $total = $row['grand_total']; ?> <tr> <td><?php echo $count; ?></td> <td> <?php echo $row['name']; ?> </td> <td> <?php echo $row['index_number']; ?> </td> <td> <?php echo $subject1_total; ?></td> <td> <?php echo $subject2_total; ?></td> <td> <?php echo $subject3_total; ?></td> <td> <?php echo $subject4_total; ?></td> <td> <?php echo $total; ?></td> <td><?php echo $position?> </td> </tr> <?php } } } ?> </tbody> </table> </div><!-- /.box-body --> Below is the php code i used to calculate the position // Sample array of students and their exam totals $studentScores = array($total); // Sort the array in descending order of scores arsort($studentScores); // Get the positions of students $positions = array(); $position = 1; $prevScore = null; foreach ($studentScores as $student => $score) { if ($prevScore !== null && $score < $prevScore) { $position++; } $positions[$student] = $position; $prevScore = $score; } // Print the positions of students foreach ($positions as $student => $position) { echo $position . PHP_EOL; } But it gives me 1 as every students position. Please how do i solve this
  3. Hello guys, so am developing a blog system and am having some issues...when i try to make a post, if i write little blog content it saves successfully to the DB, but when i use large text is throws an error, i have tried different data type on the column for details yet it doesnt seem to work. Please help. Below is the details of my code and the DB. -- -- Table structure for table `tblposts` -- CREATE TABLE `tblposts` ( `id` int(11) NOT NULL, `PostTitle` longtext NOT NULL, `CategoryId` int(11) NOT NULL, `SubCategoryId` int(11) NOT NULL, `PostDetails` longtext NOT NULL, `PostingDate` timestamp NOT NULL DEFAULT current_timestamp(), `UpdationDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', `Is_Active` varchar(11) NOT NULL, `PostUrl` varchar(500) NOT NULL, `PostImage` varchar(255) NOT NULL, `tags` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- -- Indexes for dumped tables -- -- -- Indexes for table `tblposts` -- ALTER TABLE `tblposts` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `tblposts` -- ALTER TABLE `tblposts` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2; My php script: <?php session_start(); include('includes/config.php'); error_reporting(0); if(strlen($_SESSION['login'])==0) { header('location:index.php'); } else{ // For adding post if(isset($_POST['submit'])) { $posttitle=$_POST['posttitle']; $catid=$_POST['category']; $subcatid=$_POST['subcategory']; $postdetails=$_POST['postdescription']; $url= $_POST['posttitle']; $imgfile=$_FILES["postimage"]["name"]; // get the image extension $extension = substr($imgfile,strlen($imgfile)-4,strlen($imgfile)); // allowed extensions $allowed_extensions = array(".jpg","jpeg",".png",".gif"); // Validation for allowed extensions .in_array() function searches an array for a specific value. if(!in_array($extension,$allowed_extensions)) { echo "<script>alert('Invalid format. Only jpg / jpeg/ png /gif format allowed');</script>"; } else { //rename the image file $imgnewfile=md5($imgfile).$extension; // Code for move image into directory move_uploaded_file($_FILES["postimage"]["tmp_name"],"postimages/".$imgnewfile); $status=1; $query="INSERT INTO tblposts(PostTitle,CategoryId,SubCategoryId,PostDetails,PostUrl,Is_Active,PostImage) VALUES('$posttitle','$catid','$subcatid','$postdetails','$url','$status','$imgnewfile')"; if(mysqli_query($con,$query)) { $msg="Post successfully added "; } else{ $error="Something went wrong . Please try again."; } } } ?> my db screenshot
  4. Hello @Strider64 it threw the below error
  5. <?php include_once('config.php'); $session_indexx=$_SESSION['index_number']; $session_termm=$_SESSION['term']; $session_classs=$_SESSION['class']; $session_yearr = $_SESSION['year']; // This is the query to generate the individual student class Position from the grand total. $sql = "SELECT rank FROM ( SELECT ord.id , @seq := @seq+1 as seq , @rank := CASE WHEN ord.grand_total = @prev THEN @rank ELSE @seq END as rank , @prev := ord.grand_total as grand_total FROM ( SELECT id , g.grand_total FROM student_exam_result g ORDER BY grand_total DESC LIMIT 18446744073709551615 ) ord JOIN (SELECT @seq:=0, @rank:=0,@prev:=0) init ) ranked JOIN student_exam_result g ON g.id = ranked.id WHERE class = ? AND index_number = ? AND term = ? AND year = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param('ssss', $session_classs, $session_indexx, $session_termm, $session_yearr); $stmt->execute(); $stmt->bind_result($position); $stmt->fetch(); ?> Hello guys, please help with issues from the above code, the issue is when i have multipe data on the database table for the exam results the student positions becomes incorrect, the code is supposed to calculate student results based on their grand_total and accepts the index_number,term, class and term as html inputs which is used to seect the students from the same class. The image below shows my database table from the table above, EKE DANIEL in JSS1 A with grand total of 979 is supposed to come second in class but the code throws 5 as shown in the resut below Please how do i solve this
  6. I appreciate your input so much, but my issue is, the updating is working but it updates every row in the table with just one data row from the csv... But when I do insert all the data from the csv gets inserted into there different rows in the table! So basically what I need is help to also update the table rows according to the csv rows and not one CSV data populating the entire table rows
  7. Ok so I need to first select the row data that will be updated using the Select * query, then assign varriables to the selected details which I will then in turn update using the update query?
  8. Are you saying that the WHERE clause is the cause of the repetition? Here i still tried it with out the where clause yet same repetitive result Can you proffer a better way to go about it please <?php if(isset($_POST["upload_by_subject"])){ $class = $_POST['class']; $term =$_POST['term']; $year = $_POST['year']; $check = "SELECT * FROM student_exam_result where class = '$class' and term = '$term' and year = '$year'"; $result=mysqli_query($conn,$check); $row=mysqli_fetch_assoc($result); $exam_class = $row['class']; $exam_term = $row['term']; $exam_year = $row['year']; $subject1 = $row['subject1']; $subject1_test_score = $row['subject1_test_score']; $subject1_exam_score = $row['subject1_exam_score']; if (empty($subject1) && empty($subject1_test_score) && empty($subject1_exam_score)) { $filename=$_FILES["result_csv"]["tmp_name"]; if($_FILES["result_csv"]["size"] > 0) { $file = fopen($filename, "r"); while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) { $sql = "UPDATE student_exam_result SET name = '$getData[0]', class = '$getData[1]',index_number = '$getData[2]',term = '$getData[3]',subject1 = '$getData[4]',subject1_test_score = '$getData[5]',subject1_exam_score = '$getData[6]', grand_total = '$grand_total'"; $result1 = mysqli_query($conn, $sql); if(!isset($result1)) { echo "<script>alert(Class Result CSV Was not Uploaded, Please try again!')</script>"; echo "<script>window.location = 'upload_class_result_teacher.php';</script>"; } else { echo "<script>alert('Class Result CSV Was Uploaded Successfully')</script>"; echo "<script> window.location = 'upload_class_result_teacher.php';</script>"; }} fclose($file); } ?>
  9. Hello Guys, Please i would some help on an issue i am having on my code, the code basically is to insert data into a table from a CSV file, the code i wrote successfully inserts the data onto the database but when it comes to updating the data it keeps inserting a particular record out of all that was supposed to be updated from the CSV fIile. Below is the screenshot of the CSV file Also below is the code for inserting data into the db by uploading the CSV file above: <?php if(isset($_POST["upload_by_subject"])){ $class = $_POST['class']; $term =$_POST['term']; $year = $_POST['year']; $check = "SELECT * FROM student_exam_result where class = '$class' and term = '$term' and year = '$year'"; $result=mysqli_query($conn,$check); $row=mysqli_fetch_assoc($result); $exam_class = $row['class']; $exam_term = $row['term']; $exam_year = $row['year']; $subject1 = $row['subject1']; $subject1_test_score = $row['subject1_test_score']; $subject1_exam_score = $row['subject1_exam_score']; if (empty($subject1) && empty($subject1_test_score) && empty($subject1_exam_score)) { $filename=$_FILES["result_csv"]["tmp_name"]; if($_FILES["result_csv"]["size"] > 0) { $file = fopen($filename, "r"); while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) { $sql = "INSERT INTO student_exam_result (class,index_number,term,subject1,subject1_test_score,subject1_exam_score) values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."','".$getData[5]."','".$getData[6]."')"; $result1 = mysqli_query($conn, $sql); if(!isset($result1)) { echo "<script>alert(Class Result CSV Was not Uploaded, Please try again!')</script>"; echo "<script>window.location = 'upload_class_result_teacher.php';</script>"; } else { echo "<script>alert('Class Result CSV Was Uploaded Successfully')</script>"; echo "<script> window.location = 'upload_class_result_teacher.php';</script>"; }} fclose($file); } ?> Below is a screenshot result of the insert code above which works fine with the CSV file upload: While below is the code for updating the table using the same CSV file: <?php if(isset($_POST["upload_by_subject"])){ $class = $_POST['class']; $term =$_POST['term']; $year = $_POST['year']; $check = "SELECT * FROM student_exam_result where class = '$class' and term = '$term' and year = '$year'"; $result=mysqli_query($conn,$check); $row=mysqli_fetch_assoc($result); $exam_class = $row['class']; $exam_term = $row['term']; $exam_year = $row['year']; $subject1 = $row['subject1']; $subject1_test_score = $row['subject1_test_score']; $subject1_exam_score = $row['subject1_exam_score']; if (empty($subject1) && empty($subject1_test_score) && empty($subject1_exam_score)) { $filename=$_FILES["result_csv"]["tmp_name"]; if($_FILES["result_csv"]["size"] > 0) { $file = fopen($filename, "r"); while (($getData = fgetcsv($file, 10000, ",")) !== FALSE) { $sql = "UPDATE student_exam_result SET name = '$getData[0]', class = '$getData[1]',index_number = '$getData[2]',term = '$getData[3]',subject1 = '$getData[4]',subject1_test_score = '$getData[5]',subject1_exam_score = '$getData[6]' WHERE class = '$exam_class' and term = '$exam_term' and year = '$exam_year'"; $result1 = mysqli_query($conn, $sql); if(!isset($result1)) { echo "<script>alert(Class Result CSV Was not Uploaded, Please try again!')</script>"; echo "<script>window.location = 'upload_class_result_teacher.php';</script>"; } else { echo "<script>alert('Class Result CSV Was Uploaded Successfully')</script>"; echo "<script> window.location = 'upload_class_result_teacher.php';</script>"; } } fclose($file); } ?> And below is the screenshot for the update result from the table in the db, here only one record from the CSV file gets inserted multiple times unlike the insert part: I have tried to tweak the code severally but it keeps giving the same output on the update case, please anyone that has a better solution to it should help profer it and i will appreciate the help. Thanks
  10. Hello Guys, my name is Royal and i am working a student result grading system which basically calculates students position from a record of averages from a table. So i have a table which contains student details and there rand total and averages from where i wish to get the individual positions. Here is my table record CREATE TABLE `student_grade` ( `grade_id` int(11) NOT NULL, `class` varchar(255) NOT NULL, `term` varchar(255) NOT NULL, `year` varchar(255) NOT NULL, `name` varchar(255) NOT NULL, `admission_no` varchar(255) NOT NULL, `grand_total` varchar(255) NOT NULL, `student_average` varchar(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -- -- Dumping data for table `student_grade` -- INSERT INTO `student_grade` (`grade_id`, `class`, `term`, `year`, `name`, `admission_no`, `grand_total`, `student_average`) VALUES (2, 'JS1', 'First Term', '2022', 'JUBILEE IHEANACHO', 'LMS1379467', '706', '235.3333333'), (5, 'JS1', 'First Term', '2022', 'EMMANUEL OKONJI', 'LMS6941511', '723', '241'), (6, 'JS1', 'First Term', '2022', 'ROYAL NJOKU', 'LMS2805220', '751', '250.3333333'); -- -- Indexes for dumped tables -- -- -- Indexes for table `student_grade` -- ALTER TABLE `student_grade` ADD PRIMARY KEY (`grade_id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `student_grade` -- ALTER TABLE `student_grade` MODIFY `grade_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7; COMMIT; and below is my code get the position using Mariadb DENSE_RANK function, yet for every student it keeps returning '1' as position where as the averages varies for the 3 student records <?php $student_class_position = mysqli_query($conn, "SELECT DENSE_RANK() OVER(ORDER BY grand_total DESC) AS d_rank, class, term, year, name, admission_no, grand_total, student_average FROM student_grade") or die(mysqli_error()); $class_position = mysqli_fetch_array($student_class_position); if ($class_position['name'] == $grade_name && $class_position['class'] == $grade_class && $class_position['term'] == $grade_term && $class_position['year'] == $grade_year ) { $position = $class_position['d_rank']; } echo $position; ?> Please i need help on this as i have tried even to use RANK() yet same result is still coming up.... also i noticed that if i " SELECT * FROM the table 'student_grade'" and vardumps the data as an array it gives me only the record for the first student record on top. attached is the screenshot of my table from phpmyadmin Your help is needed.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.