Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add some column with | delimiter

Posted on 2008-06-24
17
Medium Priority
?
219 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
[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
  • 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
Technology Partners: 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: 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 1000 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

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!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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…
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.
Suggested Courses

609 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