TLN_CANADA
asked on
PHP if statement as one variable
Hi all,
I have setup a function using fpdf which allows the exporting of a user's text entries records to a pdf document. In order to export it all of the output needs to be saved in the variable called $text.
Here is the code I am using to get all of the user's entries and am printing them on the screen for the user to see. I would like them to be displayed similarly in the pdf document I am creating for them. If someone could show me how to save the results in this variable that would be great.
I have setup a function using fpdf which allows the exporting of a user's text entries records to a pdf document. In order to export it all of the output needs to be saved in the variable called $text.
Here is the code I am using to get all of the user's entries and am printing them on the screen for the user to see. I would like them to be displayed similarly in the pdf document I am creating for them. If someone could show me how to save the results in this variable that would be great.
<?php
if ( ! isset($_POST["search_journal"]) )
{
if ( isset($_GET["dt"]) ) // show 1 day, selected from calendar
{
$query = mysql_query("SELECT * FROM journal_table WHERE user_id = '$username' AND DATE_FORMAT(entry_timestamp, '%Y%m%d') = '{$_GET["dt"]}' ORDER BY entry_id DESC") or die ( mysql_error() );
}
else
{
$select_all = false;
if ( ! isset($_POST["FilterRow"]) )
{
$limit = 5; // default
}
else if ( $_POST["FilterRow"] == "all" )
{
$select_all = true;
}
else
{
$limit = (int)$_POST["FilterRow"];
}
if ( $select_all )
{
$query = mysql_query("SELECT * FROM journal_table WHERE user_id = '$username' ORDER BY entry_id DESC") or die ( mysql_error() );
}
else
{
$query = mysql_query("SELECT * FROM journal_table WHERE user_id = '$username' ORDER BY entry_id DESC LIMIT $limit") or die ( mysql_error() );
}
}
$total_things = mysql_num_rows($query);
if ( $total_things == 0 )
{
echo "You do not seem to have posted any journal! Why don't you start? :) ";
}
else
{
while ( $row = mysql_fetch_array($query) )
{
$post_id = $row["entry_id"];
$date = $row["entry_timestamp"];
$day = date('d',strtotime($date));
$month = date('F',strtotime($date));
?>
<div id = "Content">
<div id = "Side">
<div id = "Entry_Month"><?php echo $month; ?></div>
<div id = "Entry_Day"><?php echo $day; ?></div>
<img height = "70px" width = "50px" src = "<?php echo $URL; ?>">
</div>
<div id = "Journal">
<p id = "Journal_Title"><?php echo $row["entry_title"]; ?></p>
<p id = "Journal_Sender"><?php echo "By: " . $username; ?></p>
<p id = "Journal_Content"><?php echo $row["entry_text"];?></p>
<div class = "Seperator"><hr></div>
<p id = "Journal_Timestamp"><?php echo $date . " - <a href = 'editjournal.php?id=$post_id'>Edit</a> - <a href = 'deletejournal.php?id=$post_id'>Delete</a>";?></p>
</div>
<div class = "ClearFloat"></div>
<div class = "Seperator"><hr></div>
</div>
<?php
}
}
}
else
{
$term = $_POST["search_journal"];
$query = mysql_query("SELECT * FROM journal_table WHERE entry_text LIKE '%$term%' OR entry_title LIKE '%$term%' ORDER BY entry_id DESC") or die ( mysql_error() );
$total_things = mysql_num_rows($query);
if ( $total_things == 0 )
{
echo "No Result For The Term Selected.<br><br>";
}
else
{
while ( $row = mysql_fetch_array($query) )
{
$post_id = $row["entry_id"];
?>
<div id = "Content">
<div id = "Journal" style = "width: 100%;">
<p id = "Journal_Title"><?php echo $row["entry_title"]; ?></p>
<p id = "Journal_Sender"><?php echo "By: " . $row["user_id"]; ?></p>
<p id = "Journal_Content"><?php echo $row["entry_text"];?></p>
<div class = "Seperator"><hr></div>
<p id = "Journal_Timestamp"><?php echo $row["entry_timestamp"] . " - <a href = 'editjournal.php?id=$post_id'>Edit</a> - <a href = 'deletejournal.php?id=$post_id'>Delete</a>";?></p>
</div>
<div class = "ClearFloat"></div>
<div class = "Seperator"><hr></div>
</div>
<?php
unset($_POST["search_journal"]);
}
}
}
?>
You can use ob_start and ob_get_clean
put ob_start() before you start outputting the text. When you are done doe the following
$text = ob_get_clean();
$text now has what would have been on the screen.
Refer to the man page for ob_get_clean for more information and samples
http://php.net/manual/en/function.ob-get-clean.php
put ob_start() before you start outputting the text. When you are done doe the following
$text = ob_get_clean();
$text now has what would have been on the screen.
Refer to the man page for ob_get_clean for more information and samples
http://php.net/manual/en/function.ob-get-clean.php
ob_start() will enable you to capture all of the browser output. However that will include the HTML statements and those are probably not exactly what you want in the PDF document.
What you've got here is a common problem for new PHP programmers. You can bounce back and forth between PHP and HTML, but just because you can does not mean that you should. Programming like that will make your work products unmaintainable and unextensible. It's fine if you want to work alone but as soon as you start trying to get cooperation from another programmer, you have a mess on your hands.
I recommend two things. First, learn about HEREDOC notation. It's a great way to get PHP variable values embedded into HTML documents. And second, accept the fact that PDF is a document layout language, whereas HTML is a semantic markup language. These are not quite as different as fish and bicycles, but almost that different. You will find the work will go well when you write two separate scripts - one for HTML and a separate one for PDF. The data acquisition methods can be shared between these scripts, but the output processes need to be completely separate.
Best regards and best of luck with it, ~Ray
What you've got here is a common problem for new PHP programmers. You can bounce back and forth between PHP and HTML, but just because you can does not mean that you should. Programming like that will make your work products unmaintainable and unextensible. It's fine if you want to work alone but as soon as you start trying to get cooperation from another programmer, you have a mess on your hands.
I recommend two things. First, learn about HEREDOC notation. It's a great way to get PHP variable values embedded into HTML documents. And second, accept the fact that PDF is a document layout language, whereas HTML is a semantic markup language. These are not quite as different as fish and bicycles, but almost that different. You will find the work will go well when you write two separate scripts - one for HTML and a separate one for PDF. The data acquisition methods can be shared between these scripts, but the output processes need to be completely separate.
Best regards and best of luck with it, ~Ray
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Ray for your helpful advice.
ASKER
Hi hielo,
This does not print any output by putting the text variable in this position. Let me know if there is anything else I can do it with.
Thanks,
D
This does not print any output by putting the text variable in this position. Let me know if there is anything else I can do it with.
Thanks,
D
ASKER
Hi julian,
Could you give me an example of how to do this using the query I provided? I've tried this diferrent ways but any way I do it, it does not print the original query information on the page.
Thanks,
D
Could you give me an example of how to do this using the query I provided? I've tried this diferrent ways but any way I do it, it does not print the original query information on the page.
Thanks,
D
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
are you including the pdf exporter from within a function and $text is declared outside that function?
If yes, try putting:
global $text;
at the beginning of that function.
If yes, try putting:
global $text;
at the beginning of that function.
ASKER
Thanks hielo, I'll give that a go.
http://en.wikipedia.org/wiki/Encapsulation_%28object-oriented_programming%29
If you're working alone and nobody else will ever see or contribute to your code, you can probably ignore these concepts, and the world will not start hating on your program code because it will never uncover your program code.
Good programming practices are not "good" for some abstract reason; they are good because they reduce errors, especially run-time errors, such as variable name collisions. If your work has some economic value you might want to get some structured training in the principles of computer science and especially the principles of object-oriented programming as implemented in PHP. Or at least give yourself some time to study the basics.
Best of luck with your project, ~Ray
If you're working alone and nobody else will ever see or contribute to your code, you can probably ignore these concepts, and the world will not start hating on your program code because it will never uncover your program code.
Good programming practices are not "good" for some abstract reason; they are good because they reduce errors, especially run-time errors, such as variable name collisions. If your work has some economic value you might want to get some structured training in the principles of computer science and especially the principles of object-oriented programming as implemented in PHP. Or at least give yourself some time to study the basics.
Best of luck with your project, ~Ray
ASKER
Thank you Ray for this advice. I will check these links out.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks julianH, that's helpful. I think we almost have it now but the formatting is a real pain on it.
ASKER
Thanks everyone, it took a while but we have this formatted now :)
ASKER
Open in new window