Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    While Loop for student attendance

    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
    Last edited by tha_ripmasta; 04-04-2007 at 01:23 PM.

  • #2
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    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 Code:
    <?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!

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Just out of curiousity, what is this supposed to achieve?

    PHP Code:
    $SearchAttendance=$_POST["search_attendance"];
    if (
    $SearchAttendance == ""$SearchAttendance=$_GET["SearchAttendance"]; 

  • #4
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    if the post variable is empty is reverts to the get variable.

    I i am reading is correctly that is.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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
    PHP Code:
    // ARGGHHHHHH
    if ($_GET[p] == search){
    // Change to
    if ($_GET['p'] == "search"){ 

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Also, one other thing. Use quotes.
    Lazy syntax strikes again.

  • #8
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


    Code:
    <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 Code:
        <?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>";

        }
    ?>
    Code:
    </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

    PHP Code:
    // 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"]; 
    PHP Code:
    // 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?????
    Last edited by tha_ripmasta; 04-04-2007 at 01:05 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •