Reputation: 77
I have the following code which gets a users data from a table based on their log in details
//==========================================
// CONNECT TO THE LOCAL DATABASE
//==========================================
$user_name = "xxxx";
$pass_word = "xxxxx";
$database = "xxxx";
$server = "xxxxxx";
$db_handle = mysql_connect($server, $user_name, $pass_word);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SELECT * FROM students WHERE L1 = '$uname' AND L2 = '" .md5 ($_POST['password'])."'";
$result = mysql_query($SQL);
$num_rows = mysql_num_rows($result);
//====================================================
// CHECK TO SEE IF THE $result VARIABLE IS TRUE
//====================================================
if ($result) {
if ($num_rows > 0) {
$color="1";
$result = mysql_query("SELECT * FROM entry, students WHERE entry.studentName = students.studentName AND students.L1='$uname' ") or die(mysql_error());
echo "<p>Welcome "; echo $row[studentName];
echo "<p>You records as of ";
echo date('l jS \of F Y h:i:s A');
echo "<table border='1' cellpadding='2' cellspacing='0'>";
echo "<tr> <th>Date</th><th>Student Name</th> <th>Tutor name</th> <th>Procedure name</th> <th>Grade</th><th>Student Reflection</th><th>Tutor Comments</th><th>Professionalism</th> <th>Communication</th> <th>Alert</th> <th>Dispute</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
if($color==1){
echo "<tr bgcolor=#DDD ><td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."</td></tr>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#CCC'><td>".$row['date']."</td><td>".$row['studentName']."</td><td>".$row['tutorName']."</td><td>".$row['procedureName']."</td><td>".$row['grade']."</td><td>".$row['studentReflection']."</td><td>".$row['tutorComments']."</td><td>".$row['professionalism']."</td><td>".$row['communication']."</td><td>".$row['alert']."</td><td>".$row['dispute']."</td></tr>";
// Set $color back to 1
$color="1";
}
}
echo '</table>';
}
What I want to do it is echo or print out the users name above the table but I am having difficulty doing so. The only want I can get it it to work is to add it to the while loop but then it prints it out as many times as the user has records.
Can you help?
Upvotes: 1
Views: 117
Reputation: 4676
You have mistake at your logic... You are making table of studens and you want to echo Welcome to the student which is logged_in
But you use echo "<p>Welcome "; echo $row[studentName];
Where $row is not set... This name should come from a previous select ..
Upvotes: 0
Reputation: 116110
You will need to fetch the first row in order to gain access to the user information. That means you will have to call mysql_fetch_array
once before the loop.
That will get you in trouble, because you will also need to call it in the loop. You could hack around that by using all kinds of boolean flags or copies of the row, but the best way is to change the structure of your code a little.
Use a do-while loop
, combined with an if statement
. This allows you to fetch a single row first, and take special action if none is found. After that, you got a loop that does what it does now, only it checks if there is a next row after the iteration instead of before, otherwise the first row would be skipped in the table output.
if ($row = mysql_fetch_array( $result )) {
// Print user info
// Print table header
do {
// Print table row
} while ($row = mysql_fetch_array( $result ));
// Print table footer
}
else
{
// User not found. Print error or whatever.
}
Upvotes: 1