Page 1 of 1

How do I fill a field in #dataTable# with underscores

Posted: Wed Jun 19, 2013 6:25 am
by JohnKlassen
Hi,

I have a 1 page report that has about 40 fields in it. I select the original data from various tables and create #dataTable#. Since I have to use some logic to manipulate the data in #dataTable# before using it in the report, I have added many of these 40 fields to #dataTable# and have created corresponding variables for those 40 fields.

As I read the rows in #dataTable#, I check to see if the column has data. If it has data, I copy the data to the variable. If the column is empty, I want to fill the variable with underscores so it shows as an underline in the report wherever there is no data. I have tried the following code, which works fine:

Code: Select all

     if (!empty($row->peo_company))  
     	$own_company = $row->peo_company;
          else
	  $own_company = '_____________________________';
As you can see I check to see if 'peo_company' is empty or not. If it is not empty, I move the contents from the column to the variable '$own_company'. If the column is empty, I fill the variable '$own_company' with underscores. Although this code works, it means I have to carefully figure out how many underscores to put into the variable.

The only other option I could find was to fill the variable with underscores using LPAD or RPAD. I tried the following code but there must be something wrong with it because, it will no longer create the report.

Code: Select all

     if (!empty($row->peo_company))  
   	$own_company = $row->peo_company;
  else
	  $own_company = RPAD('_', 25, '_');
I need to have someone tell me what is wrong with the syntax for RPAD (or LPAD) or offer an alternative. The research I have done on RPAD and LPAD always has examples using a SELECT statement. Can I not use RPAD or LPAD in my example?

Thanks,

John

Re: How do I fill a field in #dataTable# with underscores

Posted: Wed Jun 19, 2013 3:30 pm
by massiws
John, RPAD/LPAD are SQL functions; inside PHP code you have to use PHP functions, like str_repeat():

Code: Select all

$own_company = !empty($row->peo_company) ? $row->peo_company : str_repeat('_', 25);
Max

Re: How do I fill a field in #dataTable# with underscores

Posted: Thu Jun 20, 2013 5:47 am
by JohnKlassen
Max,

Thanks for explaining that I needed to use PHP code instead of SQL code. Your example works really well and saves me a lot of code.

Thanks again.

John

Re: How do I fill a field in #dataTable# with underscores

Posted: Fri Jun 21, 2013 8:38 am
by massiws
.