The script below is to search the db for the query ( $matric_no ), so that it returns the names(first, last, other name) and other student informations. It works fine if it only returns one row , but whenever two or more rows are returned the names (which are the same for all the rows) are also iterated the same times as the number of rows returned. How can i make it return just a single instance of the student names irrespective of the no of affected rows?
I have also tried using LIMT and GROUP BY but to no avail.
I typed on a mobile, hence the irregular formatting. I meant no disrespect to the community.
Thanks in advance.
<?php session_start(); ?>
<?php require('includes/dbconnect.php'); ?>
<?php require 'includes/header.inc.php'; ?>
<?php
$matric_no = mysql_real_escape_string($_POST['matric_no']) ;
if ( $_POST['matric_no'] == "")
{
echo"<div id=\"contentRight\">";
echo"<div id=\"msg\">" ;
echo "You didn't enter a <span style=\"color:red\"> Matric Number</span>";
echo"</div>" ;
echo"</div>" ;
exit();
}
$query = "SELECT matric_no
FROM main_table
WHERE main_table.matric_no = '$_POST[matric_no]'";
$result = mysql_query($query) ;
$duplicates = mysql_num_rows($result);
if ($duplicates < 1)
{
echo"<div id=\"contentRight\">";
echo"<idv id=\"msg\">" ;
echo "You dont have a record for <span style=\"color:red\">
$matric_no</span>" ;
echo"</div>" ;
echo"</div>" ;
exit();
}
{
$query = mysql_query("SELECT main_table.matric_no ,
main_table.session,
main_table.semester , main_table.course_name ,
(test+exam+practical) AS Total,
students.first_name, students.last_name, students.other_name,
students.level
FROM main_table INNER JOIN students ON
main_table.matric_no = students.matric_no
WHERE main_table.matric_no = '$_POST[matric_no]'")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
echo $row["matric_no"] ;
echo "<br />";
echo $row["session"]; echo "<br />";
echo $row["semester"];
echo "<br />";
echo $row["course_name"];
echo "<br />";
echo $row["first_name"];
echo "<br />";
echo $row["last_name"];
echo "<br />";
echo $row["other_name"];
echo "<br />";
echo $row["level"]; echo "<br />";
}
}
?>
This is just a suggestion to your question since I don't know which field you want to stop duplicating. Here I am proposing you a way to stop printing the first_name if it is duplicating again and again. Here you have to make sure your query is ordered by that value set as well.
Please tr this code.
EDITED.
<?php session_start(); ?>
<?php require('includes/dbconnect.php'); ?>
<?php require 'includes/header.inc.php'; ?>
<?php
$matric_no = mysql_real_escape_string($_POST['matric_no']) ;
if ( $_POST['matric_no'] == "")
{
echo"<div id=\"contentRight\">";
echo"<div id=\"msg\">" ;
echo "You didn't enter a <span style=\"color:red\"> Matric Number</span>";
echo"</div>" ;
echo"</div>" ;
exit();
}
$query = "SELECT matric_no
FROM main_table
WHERE main_table.matric_no = '$_POST[matric_no]'";
$result = mysql_query($query) ;
$duplicates = mysql_num_rows($result);
if ($duplicates < 1)
{
echo"<div id=\"contentRight\">";
echo"<idv id=\"msg\">" ;
echo "You dont have a record for <span style=\"color:red\">
$matric_no</span>" ;
echo"</div>" ;
echo"</div>" ;
exit();
}
{
$query = mysql_query("SELECT main_table.matric_no ,
main_table.session,
main_table.semester , main_table.course_name ,
(test+exam+practical) AS Total,
students.first_name, students.last_name, students.other_name,
students.level
FROM main_table INNER JOIN students ON
main_table.matric_no = students.matric_no
WHERE main_table.matric_no = '$_POST[matric_no]' ORDER BY students.first_name")
or die (mysql_error());
$matchingString = "";
while ($row = mysql_fetch_assoc($query)) {
echo $row["matric_no"] ;
echo "<br />";
echo $row["session"]; echo "<br />";
echo $row["semester"];
echo "<br />";
echo $row["course_name"];
echo "<br />";
$checkingString = $row["first_name"] . $row["last_name"] . $row["other_name"] . $row["level"];
if($matchingString != $checkingString)
{
echo $row["first_name"];
echo "<br />";
echo $row["last_name"];
echo "<br />";
echo $row["other_name"];
echo "<br />";
echo $row["level"]; echo "<br />";
$matchingString = $checkingString;
}
}
}
?>
ANSWER AFTER THE QUESTION MODIFICATION
Just change your two loops as bellow and see.
for ($i = 0; $i < ($number_cols - 4); $i++)