Jump to content

Recommended Posts

Hi, I'm using a php script to make an invitation - its my very first time using php or any other programming language, so I'm a real newbie :)

 

What I wanted to do, was making a php script that printed multiple invitations using fwrite();. I managed to set up a MySQL database with all the data about the guests (Name, Email, Phone, Facebook, etc.). I used a while(); loop to print an personal invitation to each of my guests, and its all working great. If i add a new guest to the database, I run the script and the invitation is printed/written.

 

One last think i would like though, is a list of all the guests Names at the bottom of each invitation. Each name should then be a clickable link, linking to their Facebook page. Any suggestion on how to do this? I have tried the following.

 

while($row = mysql_fetch_row($result)) {
$fburl = $row[4];
$name = $row[1];
$href = "href=\"$fburl\"";
$link = "<li><a $href>$name</a></li>";
echo $link;
echo "\n";
}

 

This successfully gets me a clickable list of all my guests names, linking to their individual Facebook page, but how I put that loop into a $string, so I can print it with my write($fh, $string); I still dont know :(

 

Any suggestions are highly appreciated - even if it means I have to redo my whole build.

Lets say I have a database looking like this;

 

Name

FBurl

John

fb.com/john

Matt

fb.com/matt

James

fb.com/james

 

Then I want a script (preferably php with MySQL - since that the only thing I know a little about) that can print out an invitation (.pdf or similar) for each of the three people in the database, looking something like this for the first person in the database (John);

 

Dear John

Please join me this friday at 6pm.

Total quest list:

John

Matt

James

Kind regards

 

What troubles me is getting that guest list to work, the rest I think i know how to handle.

Thanks for your time

Inside your while loop, concatenate the string.

<?php //syntax highlighting
$facebook_links = NULL; //start a variable (else you will get a notice that it is undefined).
while($row = mysql_fetch_row($result)) {
$fburl = $row[4];
$name = $row[1];
$href = "href=\"$fburl\"";
$link = "<li><a $href>$name</a></li>";
echo $link;
echo "\n";
        $facebook_links .= '<br />' . $link; //concatenate variables with .=
}
echo $facebook_links;  //echo the links AFTER the while loop.

is this what you are looking for

	<?php 
// connect to your database
$sql = mysql_query("SELECT * FROM yourTableName");
while($row = mysql_fetch_aray($sql){
	echo "Dear"." ".$row['name']."<br />";
	echo "Please join me this friday at 6pm."."<br />;
	echo "Total quest list";
	while($row = mysql_fetch_aray($sql)){
	echo"<li><a href='".$row['fburl']."'>".$row['name']."</a></li>";
	}
	echo "Kindly regards";
}
?>

is this what you are looking for

	<?php 
// connect to your database
$sql = mysql_query("SELECT * FROM yourTableName");
while($row = mysql_fetch_aray($sql){
	echo "Dear"." ".$row['name']."<br />";
	echo "Please join me this friday at 6pm."."<br />;
	echo "Total quest list";
	while($row = mysql_fetch_aray($sql)){
	echo"<li><a href='".$row['fburl']."'>".$row['name']."</a></li>";
	}
	echo "Kindly regards";
}
?>

 

No, not only will that not run how you think it will, it will not even parse.

as a bigner who is striving to learn am sory i missed some brasses and semi-colonies but this is what i intended to post.

 

Though there still needs someone to explain a better way of using a while loop inside a while loop just so we can learn that as well.

the second loop only runs once

	<?php 
// connect to your database
$connect = mysql_connect("localhost", "root","");
$db  = mysql_select_db("test");
$sql = mysql_query("SELECT * FROM table1");
$sql1 = mysql_query("SELECT * FROM table1");
echo "<table>";
while($row = mysql_fetch_array($sql)){
	echo "<tr><td>";
	echo "Dear"." ".$row['name']."<br />";
	echo "Please join me this friday at 6pm."."<br />";
	echo "Total quest list <br />";
	//echo"<li><a href='".$row['fburl']."'>".$row['name']."</a></li>";
	while($row1 = mysql_fetch_array($sql1)){
	echo"<li><a href='".$row1['fburl']."'>".$row1['name']."</a></li>";
	}
	echo "Kindly regards </td></tr>";
}
echo "</table>";
?>

No need for 2 while loops.  Store everything in array's, that way you have a complete list of everyone's facebook links.  Then go through the 1st array making invitations, then implode the facebook links to the bottom.

 

<?php //syntax highlighting

while($row = mysql_fetch_row($result)) { //while rows exist in the database.
$names[] = $row[1]; //store names in an array.
        $links[] = '<a href="' . $row[4] . '">' . $row[1] . '</a>';  //store facebook links in an array.
}

foreach($names as $name) { //loop through the names, and write them a letter.
//heredoc syntax.
     echo <<<EOF
Dear {$name},<br />
Please join me this friday at 6pm.<br />
Total quest list: <br />
EOF;
echo implode('|',$links); //implode all the links with a pipe in between each one.
}

No need for 2 while loops.  Store everything in array's, that way you have a complete list of everyone's facebook links.  Then go through the 1st array making invitations, then implode the facebook links to the bottom.

 

<?php //syntax highlighting

while($row = mysql_fetch_row($result)) { //while rows exist in the database.
$names[] = $row[1]; //store names in an array.
        $links[] = '<a href="' . $row[4] . '">' . $row[1] . '</a>';  //store facebook links in an array.
}

foreach($names as $name) { //loop through the names, and write them a letter.
//heredoc syntax.
     echo <<<EOF
Dear {$name},<br />
Please join me this friday at 6pm.<br />
Total quest list: <br />
EOF;
echo implode('|',$links); //implode all the links with a pipe in between each one.
}

 

this is what that code produces and it very different from what is needed here.

 

 

Dear fb.com/john,

Please join me this friday at 6pm.

Total quest list:

fb.com/john|fb.com/matt|fb.com/jamesDear fb.com/matt,

Please join me this friday at 6pm.

Total quest list:

fb.com/john|fb.com/matt|fb.com/jamesDear fb.com/james,

Please join me this friday at 6pm.

Total quest list:

fb.com/john|fb.com/matt|fb.com/james

 

the intended code should produce this and how to do it?

 

Dear John

Please join me this friday at 6pm.

Total quest list:

John

Matt

James

Kind regards

 

Sorry, I was under the impression that multiple invitations were to be printed.  I changed a line or two that should satisfy your interest, although, the code is merely an example.

<?php //syntax highlighting

while($row = mysql_fetch_row($result)) { //while rows exist in the database.
$names[] = $row[4]; //store names in an array. *CHANGED looks like you had 4 and 1 reversed from what you gave.
        $links[] = '<a href="' . $row[1] . '">' . $row[4] . '</a>';  //store facebook links in an array. *CHANGED looks like you had 4 and 1 reversed from what you gave.
}

foreach($names as $name) { //loop through the names, and write them a letter.
//heredoc syntax.
     echo <<<EOF
Dear {$name},<br />
Please join me this friday at 6pm.<br />
Total quest list: <br />
EOF;
echo implode('<br />',$links); //implode all the links with a pipe in between each one. *CHANGED the pipe to a break rule*
echo '<hr />'; //*ADDED a horizontal rule. FOR DISPLAY PURPOSES.
}

Thanks for both of your answers, I managed to get both of them to give me the right output, but still only in my browser window (see attached image). What i wanted, was an individual printed invitations (.pdf or similar) for each of the guests. I though I could use fwrite($fh, $string) to do the work for me, but i can't see how to construct it, to get your loops stored in $string, so I can print it. Any suggestions?

post-135625-13482403675833_thumb.png

Sorry, I was under the impression that multiple invitations were to be printed.  I changed a line or two that should satisfy your interest, although, the code is merely an example.

<?php //syntax highlighting

while($row = mysql_fetch_row($result)) { //while rows exist in the database.
$names[] = $row[4]; //store names in an array. *CHANGED looks like you had 4 and 1 reversed from what you gave.
        $links[] = '<a href="' . $row[1] . '">' . $row[4] . '</a>';  //store facebook links in an array. *CHANGED looks like you had 4 and 1 reversed from what you gave.
}

foreach($names as $name) { //loop through the names, and write them a letter.
//heredoc syntax.
     echo <<<EOF
Dear {$name},<br />
Please join me this friday at 6pm.<br />
Total quest list: <br />
EOF;
echo implode('<br />',$links); //implode all the links with a pipe in between each one. *CHANGED the pipe to a break rule*
echo '<hr />'; //*ADDED a horizontal rule. FOR DISPLAY PURPOSES.
}

thanks for that i also learned something.

Any suggestions?

If you want to create one PDF per guest, then you'll need to use a PDF generator class. Then store the generated content in the PDF object, instead of using echo to print it out. At the bottom of the construction loop you'll want to use file_put_contents () to save it to a file.

 

Should be plenty of examples on this site on PDF generation classes, and how to use them. So a search is highly recommended. ;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.