...

View Full Version : While Loop for student attendance



tha_ripmasta
04-03-2007, 04:46 PM
Hello

I created a search script to find student attendance from my db. The search works fine and I get the right results.

But what i'm trying to do is get the results to display like this:

-----------------------------------------

Student1 name - Student1 lastname

Month

day1, day2, day3

-----------------------------------------

Student2 name - Student2 lastname

Month

day1, day2, day3

--------------------------------------------


But i can't seem to figure out the right way to loop the results


Can anyone please help me and if you need more info or code ..let me know.


Here is my search code.

SEE POST BELOW

JohnDubya
04-03-2007, 04:55 PM
When you do a query that gets multiple rows of data, use a while() loop to go through them all and output the information in a certain way. For instance:



<?php
//Get all the students info
$query = mysql_query("SELECT first_name, last_name, date FROM students");

//Then to go through them one-by-one, use a while() loop
while ($y = mysql_fetch_array($query)) {
$first_name = $y['first_name'];
$last_name = $y['last_name'];
$date = $y['date'];

//Let's stop PHP code so we can easily write out HTML code
?>

<div style="margin-bottom: 5px; text-align: center;">
Name: <?=$first_name?> <?=$last_name?><br />
Date: <?=$date?>
</div>

<?php
}
?>


This will loop through every student that is found in the database and write out their name and date. As you can see, you can format how the name and date are written out however you want...use tables, CSS, etc. Good luck!

aedrin
04-03-2007, 07:08 PM
Just out of curiousity, what is this supposed to achieve?



$SearchAttendance=$_POST["search_attendance"];
if ($SearchAttendance == "") $SearchAttendance=$_GET["SearchAttendance"];

the-dream
04-03-2007, 08:08 PM
if the post variable is empty is reverts to the get variable.

I i am reading is correctly that is.

aedrin
04-03-2007, 08:43 PM
I wasn't asking what it does, but what it is supposed to achieve.

Why check both POST and GET, and why in that way.

Nightfire
04-03-2007, 08:43 PM
Think that's what it's meant to do in theory. But all that's going to do if POST isn't available is create loads of notices

Also, one other thing. Use quotes. Don't call variables as constants or strings as integars


// ARGGHHHHHH
if ($_GET[p] == search){
// Change to
if ($_GET['p'] == "search"){

aedrin
04-03-2007, 08:44 PM
Also, one other thing. Use quotes.

Lazy syntax strikes again. ;)

tha_ripmasta
04-04-2007, 02:01 PM
Thank you all for responding and thanks for the tips.

I tried what you all suggested, but can't get it working.

Well it seems like i still not getting it, so here i try to break it down again...

I created a search script to find student attendance from my db.

But i can't seem to figure out the right way to loop the results

Here is my search code (REVISED).

==========================================>> DB TABLES

CREATE TABLE students (
student_id int(11) NOT NULL auto_increment,
organization_id int(11) NOT NULL default '0',
group_id int(11) NOT NULL default '0',
student_first_name varchar(70) NOT NULL default '',
student_last_name varchar(70) NOT NULL default '',
student_dob date NOT NULL default '0000-00-00',
student_address varchar(100) NOT NULL default '',
student_homephone varchar(24) NOT NULL default '',
student_cellphone varchar(24) NOT NULL default '',
student_email varchar(100) NOT NULL default '',
student_gender varchar(7) NOT NULL default '',
student_nationality varchar(35) NOT NULL default '',
student_pob varchar(35) NOT NULL default '',
school_id int(11) NOT NULL default '0',
hobbies varchar(100) NOT NULL default '',
end_date varchar(12) NOT NULL default '',
registered_date varchar(20) NOT NULL default '',
registered_by varchar(70) NOT NULL default '',
doctor_id int(11) NOT NULL default '0',
image varchar(255) NOT NULL default '',
status int(1) NOT NULL default '1',
PRIMARY KEY (student_id)
) TYPE=MyISAM;

CREATE TABLE attendance (
id int(21) NOT NULL auto_increment,
studentid int(11) NOT NULL default '0',
organization_id int(11) NOT NULL default '0',
attend_date date NOT NULL default '0000-00-00',
attend_option int(2) NOT NULL default '0',
PRIMARY KEY (id)
) TYPE=MyISAM;



==========================================>> SEARCH FORM



<table width="545" border="0" cellspacing="3" cellpadding="5" bordercolor="#CCCCCC">

<form method="post" name="formquery1" action="<?php echo '?id=searchdb_attendance&&p=search' ?>">
<tr>
<td align="left">
<span class="small-txt-1">Student:</span><br />
<select name="studentid" style="width: 50%" class="input">
<option value="">Any</option>


<?php
$ses_organization = $_SESSION['organization_id'];
$selstudent="SELECT * from students where organization_id ='$ses_organization' AND status='1' order by student_last_name ASC";
$selstudent2=mysql_query($selstudent) or die("Could not select students / Contact Administrator or Developer");

while($selstudent3=mysql_fetch_array($selstudent2))
{
$student_id=$selstudent3['student_id'];
$student_first_name=stripslashes($selstudent3["student_first_name"]);
$student_last_name=stripslashes($selstudent3["student_last_name"]);

echo "<option value='$student_id'>$student_last_name $student_first_name </option>";

}
?>




</select>
</td>
</tr>
<tr>
<td align="left">
<span class="small-txt-1">Attendance:</span>
<br>
<select name="search_attendance" style="width: 50%" class="input">
<option value="1">General Attendance</option>
<option value="2">Homework Attendance</option>
<option value="3">Tutoring Attendance</option>
</select>
</td>
</tr>
<tr>
<td align="left">
<span class="small-txt-1">Month:</span>
<br>
<select name="month_att" style="width: 50%" class="input">
<option value="">Any</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
</td>
</tr>
<tr>
<td align="left">
<span class="small-txt-1">Year:</span><br />
<select name="year_att">
<option value="">Any</option>
<option>2006</option>
<option>2007</option>
<option>2008</option>
<option>2009</option>
<option>2010</option>
<option>2011</option>
<option>2012</option>
<option>2013</option>
<option>2014</option>
<option>2015</option>
</select>
</td>
</tr>
<tr>
<td align="left">
<span class="small-txt-1">Date:</span><br />
<input type="text" name="attendance_date"> example (11-21-2006)
</td>
</tr>
<tr>
<td align="left">
<input name="btnSubmitsearch" type="submit" value="Search" class="btn">
</td>
</tr>
</form>

</table>

==========================================>> END SEARCH FORM


==========================================>> SEARCH SCRIPT



// get search choices
///////////////////////////////

$SearchAttendance=$_POST["search_attendance"];

$SearchStudentid=$_POST["studentid"];

$SearchMonth=$_POST["month_att"];

$SearchYear=$_POST["year_att"];

$SearchDate=$_POST["attendance_date"];

$SearchDate2=$_POST["attendance_date"];





// query
///////////////////////////////

$query= "Select * from attendance WHERE organization_id='$ses_organization' AND ";
if ($SearchStudentid !=""){
$query .= "studentid ='$SearchStudentid' AND ";
}
if ($SearchMonth !=""){
$query .= "MONTH(attend_date) ='$SearchMonth' AND ";
}
if ($SearchYear !=""){
$query .= "YEAR(attend_date) ='$SearchYear' AND ";
}
if ($SearchDate !=""){

$changedate_act_search="$SearchDate";
list($month_act,$day_act,$year_act) = split('[-./]', $changedate_act_search);
$SearchDate=$year_act."-".$month_act."-".$day_act;

$query .= "attend_date ='$SearchDate' AND ";
}
if ($SearchAttendance !=""){
$query .= "attend_option = '$SearchAttendance' AND ";
}

$query2 = substr($query,0,-4);

$SearchResult=mysql_query("$query2") or die(mysql_error());

$NumberOfResults=mysql_num_rows($SearchResult);




Now I want to display the results like this:

Student Attendance

-----------------------------------------

Terry James

December
1, 5, 9

-----------------------------------------

Paul Martin
June

8, 2, 20

--------------------------------------------

How do I get that to?????



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum