Solved

Why same result appear in every row

Posted on 2008-06-24
6
195 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

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

Suggested Solutions

Title # Comments Views Activity
Log in through ID 5 36
deprecated mysql extensions 1 25
Why is my $_POST not going to results page 10 36
Make an array show the subkey and put it in a query 2 25
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…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

756 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