Solved

Why same result appear in every row

Posted on 2008-06-24
6
193 Views
Last Modified: 2008-07-03
PHP

HI experts,

I need to find When a student is absent and the fieldname is ABSEND
I have 3 value that i put it in an array e.g: $student = array(18,22,34) where i retrieved from database
i called this array as ID's
since one week have 7 days so i create an array as array(1,2,3,4,5,6,7)

i do as follow

function ABSENt()
{
$IndexOrder = array(1,2,3,4,5,6,7);

  foreach($this->Getstudent as $student)
   {
      for ($j=0; $j<sizeof($IndexOrder);$j++)
     {
        $query = "SELECT ABSENT
                   FROM $ABSENTTABLE
                  WHERE STUDENT = \"$student\" AND INDEXORDER = $IndexOrder[$j]";
 
        $result = mysql_query($query);
       
         while($row = mysql_fetch_array($result))
           {
          $this->GetABSEND  .=  ''.$row['ABSENT'].'|';  
           }
     }
  }
  mysql_close($dbLink);
}
 
 
when i echo the result i get result like this

18|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent
22|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent
34|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|absent|absent|no_absent|no_absent|no_absent


AS SEE ABOVE I DONT KNOW WHY THE IT WILL KEEP CONTINUE PRINT OUT THE PREVIOUS RESULT FIRST BEFORE IT PRINT THE NEW RESULT

how to solve this
0
Comment
Question by:firekiller15
  • 3
  • 2
6 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 21853614
$student is not defined. Either pass it in to the function as a parameter, or if it defined externally to the function declare it as a global. Passing it as a parameter is "best-practice" compared to using "global".

So, either this (preffered)

function ABSENt( $student )
{
$IndexOrder = array(1,2,3,4,5,6,7);

  foreach($this->Getstudent as $student)
   {



or this



function ABSENt()
{

global $student;

$IndexOrder = array(1,2,3,4,5,6,7);

  foreach($this->Getstudent as $student)
   {
0
 

Author Comment

by:firekiller15
ID: 21853675
hence this $this->Getstudent is to defined the student and i dont understand why need to return the value student again
furthermore my problem now is not that i cant get the student value but the result repeat again and again as shown above.

that mean everytime before it execute a new data it will execute the old data from the beginning
why like that
0
 

Author Comment

by:firekiller15
ID: 21853678
output
18|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent
22|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent
34|absent|absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|no_absent|absent|absent|no_absent|no_absent|no_absent
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:firekiller15
ID: 21853682
suppose 18,22,and 34 should have same column
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 21854321
"hence this $this->Getstudent is to defined the student and i dont understand why need to return the value student"

Sorry - I missed the foreach. $student is defined. Even experts mess up!!! :-(

Moving on from that, make the following changes

 $query = "SELECT ABSENT
                   FROM $ABSENTTABLE
                  WHERE STUDENT = \"$student\" AND INDEXORDER = $IndexOrder[$j]";

echo "$query<br>";
 
        $result = mysql_query($query);
       
         while($row = mysql_fetch_array($result))
           {

print_r( $row ); echo "<br>";

          $this->GetABSEND  .=  ''.$row['ABSENT'].'|';  
           }


So that you can be sure that your query is constructed correctly and that it is returning the expected results
0
 
LVL 16

Accepted Solution

by:
CWS (haripriya) earned 250 total points
ID: 21854623
empty the variable for each student.
Try this:
function ABSENt()
{
$IndexOrder = array(1,2,3,4,5,6,7);
 
  foreach($this->Getstudent as $student)
   {
$this->GetABSEND="";
      for ($j=0; $j<sizeof($IndexOrder);$j++)
     {
        $query = "SELECT ABSENT
                   FROM $ABSENTTABLE
                  WHERE STUDENT = \"$student\" AND INDEXORDER = $IndexOrder[$j]";
 
        $result = mysql_query($query);
       
         while($row = mysql_fetch_array($result))
           {
          $this->GetABSEND  .=  ''.$row['ABSENT'].'|';  
           }
     }
  }
  mysql_close($dbLink);
}

Open in new window

0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question