Solved

Why same result appear in every row

Posted on 2008-06-24
6
190 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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.

762 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

19 Experts available now in Live!

Get 1:1 Help Now