Solved

Why same result appear in every row

Posted on 2008-06-24
6
191 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now