Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why same result appear in every row

Posted on 2008-06-24
6
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

715 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