Solved

How to add some column with | delimiter

Posted on 2008-06-24
17
215 Views
Last Modified: 2008-06-25
PHP

Hi experts,

I have this program where i called the data from database.
The datafield i need to look for is absent and reason
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 and no reason
But my problem now is say : studentID 18 has no absent and no reason it will only print one delimiter as below

|
and not
||

below is my code


i do as follow

function ABSENT()
{
 foreach($this->Getstudent as $student)
   {
       $query = "SELECT ABSENT, REASON
                 FROM $ABSENTTABLE
                 WHERE STUDENT = \"$student\"";
 
       $result = mysql_query($query);
       
       while($row = mysql_fetch_array($result))
        {
         $this->GetABSENT  .=  ''.$row['ABSENT'].'|'.$row['REASON'].'';  
        }
     
  }
  $data1 = "{$this->GetABSENT}|";
  echo $data1;
  mysql_close($dbLink);
}
 
how to solve this
0
Comment
Question by:firekiller15
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 14

Expert Comment

by:Vel Eous
ID: 21862631
Try replacing:
$data1 = "{$this->GetABSENT}|";

With:
$data1 = "|{$this->GetABSENT}|";
0
 

Author Comment

by:firekiller15
ID: 21862659
cannot do like this because
what happen is in the future i will add one or more string
yah if the string is empty then you get ||
what happen if the string have value it will become
||value|value|
0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 21862682
Are you certain?

Using $data1 = "|{$this->GetABSENT}|"; with data should produce |value|value| if there is something to be returned (according to your code), or || if there is nothing.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:firekiller15
ID: 21862896
ok as in my question
''.$row['ABSENT'].'|'.$row['REASON'].''
and you say add like  this
$data1 = "|{$this->GetABSENT}|";
that mean if there is a value the the output will be |value|value| right?
when there is not value it become ||
as clearly you can see one your have 3 x |
and another has only 2

what i want is only 2 delimiter which mean
when no value is ||
when has value is value|value|
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21862927
This will give you two || when no values.
function ABSENT()
{
 foreach($this->Getstudent as $student)
   {
       $query = "SELECT ABSENT, REASON
                 FROM $ABSENTTABLE
                 WHERE STUDENT = \"$student\"";
 
       $result = mysql_query($query);
       
       while($row = mysql_fetch_array($result))
        {
         $this->GetABSENT  .=  ''.$row['ABSENT'].'|'.$row['REASON'].'|';  
        }
     
  }
  $data1 = "{$this->GetABSENT}|";
  echo $data1;
  mysql_close($dbLink);
}

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21862933
no it will only give me one |
when the both value are empty
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21862948
can you post the results when the values are empty and when the values are not empty?
0
 

Author Comment

by:firekiller15
ID: 21862981
result is
|
|
absent|reason|
|
0
 

Author Comment

by:firekiller15
ID: 21862984
when empty it display only one | which is from
$data1 = "{$this->GetABSENT}|";
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21863008
what happens when you echo the values inside the while loop, like this:
function ABSENT()
{
 foreach($this->Getstudent as $student)
   {
       $query = "SELECT ABSENT, REASON
                 FROM $ABSENTTABLE
                 WHERE STUDENT = \"$student\"";
 
       $result = mysql_query($query);
       
       while($row = mysql_fetch_array($result))
        {
         $this->GetABSENT  .=  ''.$row['ABSENT'].'|'.$row['REASON'].'|'; 
 echo "".$row['ABSENT']."|".$row['REASON']."|";
        }
     
  }
  $data1 = "{$this->GetABSENT}|";
  //echo $data1;
  mysql_close($dbLink);
}

Open in new window

0
 

Author Comment

by:firekiller15
ID: 21863197
ehhh ....
i get the result i show you jsut now
result is
|
|
absent|reason|
|
0
 

Author Comment

by:firekiller15
ID: 21863208
|
|
absent|reason| absent|| absent|reason| |reason| absent|reason| absent|reason|
|

0
 

Author Comment

by:firekiller15
ID: 21863209
i recieved as above
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21863282
This is how you wanted right?
when there is no reason it displays ||
0
 

Author Comment

by:firekiller15
ID: 21863509
what do you mean
??
i have receive only one now | if there is no value
my aim is two ||
can ?
0
 
LVL 16

Accepted Solution

by:
CWS (haripriya) earned 250 total points
ID: 21864132
ok, try this:
function ABSENT()
{
 foreach($this->Getstudent as $student)
   {
       $query = "SELECT ABSENT, REASON
                 FROM $ABSENTTABLE
                 WHERE STUDENT = \"$student\"";
 
       $result = mysql_query($query);
       
       while($row = mysql_fetch_array($result))
        {
         $this->GetABSENT  .=  ''.$row['ABSENT'].'|'.$row['REASON'].'';  
        }
     if(mysql_num_rows($result)==0){
     $this->GetABSENT  .="|";
     }
  }
  $data1 = "{$this->GetABSENT}|";
  echo $data1;
  mysql_close($dbLink);
}

Open in new window

0
 
LVL 17

Expert Comment

by:nplib
ID: 21867421
      while($row = mysql_fetch_array($result))
        {
              if (($row['ABSENT']) && ($row['REASON'])) {
                   $this->GetABSENT  .=  ''.$row['ABSENT'].'|'.$row['REASON'].'';
              }
        }
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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
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…

856 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