Solved

fpdf row title

Posted on 2011-09-07
19
484 Views
Last Modified: 2012-05-12

Hello Experts,

Trying to understand how fpdf works... not easy....
all the examples i could find with mysql are tables with column headers.

i need something like this:

Names of partners: $name1, $name2, $name3
when the WHILE loops only on the variables AND not keep repeating the " Names of partners:"

here is part of the code from where i loop. i tired many variations with no luck.

 
while($row = mysql_fetch_array($result)) {
$partner_fname = $row['partner_fname'];
$partner_lname = $row['partner_lname'];
$ownerName = "Owner`s Name: ". $partner_fname. $partner_lname;
	
$i = 0;
$pdf->Cell(0,10,$ownerName, 0, 10);
$i = $i + 1;

Open in new window

0
Comment
Question by:Refael
  • 7
  • 7
  • 3
  • +1
19 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 36500593
Trying to understand how fpdf works... not easy....
all the examples i could find with mysql are tables with column headers.
Click on the "Manual" link and you will see the methods listed with a description of what their arguments

i need something like this:

Names of partners: $name1, $name2, $name3
when the WHILE loops only on the variables AND not keep repeating the " Names of partners:"

Try:
...
if( 0==mysql_num_rows($result) )
{
	echo 'No results found';
}
else
{
	$row = mysql_fetch_assoc($result);
	$ownerName = "Owner`s Name: ";
	do{
		$ownerName .= $row['partner_fname']. ' ' .$row['partner_lname'];
	
		$i = 0;
		$pdf->Cell(0,10,$ownerName, 0, 10);
		$i = $i + 1;
...
		$ownerName ='';
	}while($row = mysql_fetch_assoc($result));
}

Open in new window

0
 

Author Comment

by:Refael
ID: 36501921

Hi hielo

thank you! the only problem i still have is to have keep the two variables in the same line e.g. $name1, $name2, $name3. currently they appear line after line break. i tried this but it still does not work.

$pdf->Cell(0,10,$ownerName,0,0);
0
 
LVL 2

Expert Comment

by:johannod
ID: 36501967
Something like this should work:

$pdf->Write("Owner's Name: ");

$numberOfRows = mysql_num_rows($sql);
$rowNumber = 1;

while($row = mysql_fetch_array($result)) {

	$pdf->Write( $row['partner_fname'] . " " . $row['partner_lname']);
	
	// Output comma between names
	if($rowNumber < $numberOfRows)
	{
		$pdf->Write( ", " );
	}	
}

Open in new window


Also have a look at this for more stylish text output:
http://www.fpdf.org/en/tutorial/tuto6.htm
0
 

Author Comment

by:Refael
ID: 36502044

johannod thanks, but this brings some errors in regards to the write function.
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 36502124

// INITIALIZE THE STRING
$string = "Owner's Name: ";

// USE AN ITERATOR TO GET THE PARTNER NAMES
while($row = mysql_fetch_array($result)) 
{
    // ADD THE PARTNER NAMES TO THE STRING
    $string .= "$row['partner_fname'] $row['partner_lname'],";
}

// REMOVE THE EXTRANEOUS TRAILING COMMA
$string = rtrim($string, ',');

// PUT THE STRING INTO THE PDF CELL
$pdf->Cell(0,10,$string, 0, 10);

Open in new window

0
 

Author Comment

by:Refael
ID: 36502206

Hi Ray_Paseur... your knowledge in almost every 'zone' i posted in is incredible!

this line $string .= "$row['partner_fname'] $row['partner_lname'],";  have a problem so i removed the quotes but now it prints the name right after the other without the "," nor space.

thanks!

 
0
 
LVL 82

Expert Comment

by:hielo
ID: 36502452
thank you! the only problem i still have is to have keep the two variables in the same line e.g
It sounds like some of those items may have trailing newlines. Try:
...
if( 0==mysql_num_rows($result) )
{
	echo 'No results found';
}
else
{
	$row = mysql_fetch_assoc($result);
	$ownerName=array();
	while($row = mysql_fetch_assoc($result))
	{
		$ownerName[]= trim($row['partner_fname']). ' ' .trim($row['partner_lname']);
	}
	$ownerName='Owner`s Name: '.implode('', $ownerName);
	$pdf->Cell(0,10,$ownerName, 0, 0);
}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36502526
@hielo: Good idea about using trim() on the variables.  In the snippet at ID:36502452 is line 8 extraneous?  Also the implode on line 14 might need a comma between the quotes like implode(',', $ownerName)
0
 
LVL 82

Expert Comment

by:hielo
ID: 36502541
>>In the snippet at ID:36502452 is line 8 extraneous?... comma...
Good catch. I copied from my original post and completely missed that fetch() after changing from do{}while() to while().
...
if( 0==mysql_num_rows($result) )
{
	echo 'No results found';
}
else
{
	$ownerName=array();
	while($row = mysql_fetch_assoc($result))
	{
		$ownerName[]= trim($row['partner_fname']). ' ' .trim($row['partner_lname']);
	}
	$ownerName='Owner`s Name: '.implode(', ', $ownerName);
	$pdf->Cell(0,10,$ownerName, 0, 0);
}

Open in new window

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.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36502616
...knowledge in almost every 'zone' i posted in is incredible!

Thanks for your kind words.  I only pay attention to the PHP zone, but with the way EE works with multi-zone postings I may appear to be in two places at once ;-)
0
 
LVL 2

Assisted Solution

by:johannod
johannod earned 150 total points
ID: 36502646
Yes, the Write function requires an extra argument specifyin the line height. You can experiment with that. Here is a correction.

And again the tip also given by hielo: use the manual. :-)

$pdf->Write(10, "Owner's Name: ");

$numberOfRows = mysql_num_rows($sql);
$rowNumber = 1;

while($row = mysql_fetch_array($result)) {

	$pdf->Write( $row['partner_fname'] . " " . $row['partner_lname']);
	
	// Output comma between names
	if($rowNumber < $numberOfRows)
	{
		$pdf->Write( 10, ", " );
	}	
}

Open in new window

0
 

Author Comment

by:Refael
ID: 36505472

Guys, thank you all so much!

i am learning... that's good :-) now, i am trying to add the next line. e.g.:

Names of partners: $name1, $name2, $name3
Company Emails: $email1, $email2, $email3

i am trying to add the next line learning from your examples and i get something like this:

Names of partners: $name1, Company Emails: $email1.....

can you please direct me. if find this fpdf very confusing and not really easy!

0
 
LVL 82

Accepted Solution

by:
hielo earned 200 total points
ID: 36505579
...
if( 0==mysql_num_rows($result) )
{
      echo 'No results found';
}
else
{
      $ownerName=array();
      $emails=array();
      while($row = mysql_fetch_assoc($result))
      {
            $ownerName[]= trim($row['partner_fname']). ' ' .trim($row['partner_lname']);
            $email[]= trim($row['email']);
      }
      $ownerName='Owner`s Name: '.implode(', ', $ownerName);
      $pdf->Cell(0,10,$ownerName, 0, 0);

      $emails='Company Emails: '.implode(', ', $emails);
      $pdf->Cell(0,10,$emails, 0, 0);

}
0
 
LVL 82

Expert Comment

by:hielo
ID: 36505590
>>$email[]= trim($row['email']);
should have been $emails...
...
if( 0==mysql_num_rows($result) )
{
      echo 'No results found';
}
else
{
      $ownerName=array();
      $emails=array();
      while($row = mysql_fetch_assoc($result))
      {
            $ownerName[]= trim($row['partner_fname']). ' ' .trim($row['partner_lname']);
            $emails[]= trim($row['email']);
      }
      $ownerName='Owner`s Name: '.implode(', ', $ownerName);
      $pdf->Cell(0,10,$ownerName, 0, 0);

      $emails='Company Emails: '.implode(', ', $emails);
      $pdf->Cell(0,10,$emails, 0, 0);

}

Open in new window

0
 

Author Comment

by:Refael
ID: 36505736

hielo thank you. i feel like this would never end....

i took you script and run it....
if the company have 3 owners and only 1 email it prints the email 3 times....
just tell me if i this is a problem in the mysql select and not the script.
0
 
LVL 82

Expert Comment

by:hielo
ID: 36505799
It sounds like each of those owners are using the same email OR your query is wrong.  If you think the query is correct and you want unique email list, then change:
 $emails='Company Emails: '.implode(', ', $emails);

to:
 $emails='Company Emails: '.implode(', ', array_unique($emails) );

BUT, if your db has (like I said, "It sounds like each of those owners are using the same email" ):
John, a@b.com
Sally, a@b.com
Mary, X@b.com

then using the array_unique() fix, will result in:
John, Sally, Mary
a@b.com, X@b.com

making it seem as if though Sally "owns" X@b.com and Mary doesn't have an email. I don't know about you, but given that specific example I would rather have:

John, Sally, Mary
a@b.com, a@b.com, X@b.com
0
 

Author Comment

by:Refael
ID: 36506234

hi hielo, thank you, thank you...

i am trying the mysql query in phpmyadmin... the problem is solved having the array_unique. the problem was not in query... for some reason it was printing the same email 3 times...

the problem in query is that i have 3 WHERE statements and when one return false unfortunately i have a blank page. i should i guess create multiple sql queries.
0
 
LVL 82

Expert Comment

by:hielo
ID: 36506723
>>hi hielo, thank you, thank you...
You are welcome.
0
 

Author Comment

by:Refael
ID: 36506995

Guys, special thank you for all the experts! i wish i could give each of you 500 points!
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

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 …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 …

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

20 Experts available now in Live!

Get 1:1 Help Now