Solved

How to add some column with | delimiter

Posted on 2008-06-24
17
216 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Send email using HTML and PHP in separate file 5 41
Google Dork query 7 66
What is define("__APPROOT__", __DIR__); 6 32
Why is my implode function empty 8 18
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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.

726 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