Solved

How to add some column with | delimiter

Posted on 2008-06-24
17
211 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:Tchuki
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:Tchuki
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
 

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now