Solved

How to add some column with | delimiter

Posted on 2008-06-24
17
213 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Parts and Products table schema in mysql 6 42
nagios 4 php error after installation 6 69
myqsl update statement on phpMyAdmin 8 21
mcrypt_create_iv() is deprecated 4 67
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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…
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.

948 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

19 Experts available now in Live!

Get 1:1 Help Now