Solved

How to add some column with | delimiter

Posted on 2008-06-24
3
202 Views
Last Modified: 2012-05-05
PHP

Hi experts,

I have this program where i called the data from database.
The datafield i need to look for is absent and only absent will print out on the screen
In this database i need to retrieved data that with condition 1 to 7 (as day)
I have 3 value that i put it in an array e.g: $student = array(18,22,34) where i retrieved from database
I should have output of |||||||| if the student is not absent from day 1 to 7
But my problem now is say : studentID 18 only absent once on tuesday is will only print the string as below

absent|
and not
|absent||||||

below is my code

I need to find When a student is absent and the fieldname is ABSENT
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()
{
$DAY= 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 = $DAY[$j]";
 
        $result = mysql_query($query);
       
         while($row = mysql_fetch_array($result))
           {
          $this->GetABSENT  .=  ''.$row['ABSENT'].'|';  
           }
     }
  }
  mysql_close($dbLink);
}
 
THE CURRENT RESULT I GET IS EXAMPLE 22 ABSENT 2 DAYS ON MONDAY AND FRIDAY IT ONLYT PRINT OUT
ABSENT|ABSENT|
AND NOT
ABSENT||||ABSENT|||

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

Accepted Solution

by:
Rurne earned 250 total points
ID: 21862437
The problem is in your while loop.  You're only appending when you hit the while($row  = mysql_fetch_array($result)) loop, which only triggers when an absent day.  You may want to shift your logic up a bit:
function ABSENT()
{
$DAY= array(1,2,3,4,5,6,7);
 
  foreach($this->Getstudent as $student)
   {
      for ($j=0; $j<7;$j++)
     {
        $query = "SELECT ABSENT
                   FROM $ABSENTTABLE
                  WHERE STUDENT = \"$student\" AND INDEXORDER = $j";
 
        $result = mysql_query($query);
       
        $this->GetABSENT .= (mysql_num_rows($result) > 0 
                              ? mysql_result($result, 0, 0) 
                              : '');
        $this->GetABSENT .= '|';
        
     }
  }
  mysql_close($dbLink);
}

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21862527
this INDEXORDER = $j"; meaning i no need $DAY= array(1,2,3,4,5,6,7);??
0
 
LVL 9

Expert Comment

by:Rurne
ID: 21862567
Exactly.

You may actually want to fix line 11 though (since it'll loop from 0 to 6, you need to bump it up one to replace the 1..7 array):
                  WHERE STUDENT = \"$student\" AND INDEXORDER = ".($j + 1);

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.htaccess 5 39
Using javascript confirmation for a delete in mysql 5 34
PHP Syntax Error 4 27
Cpanel file manager 8 25
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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
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…

832 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