Jump to content

Should Messages go in PHP or DB?


doubledee

Recommended Posts

I have two messages in my PHP code, and one is displayed depending on whether or not an INSERT succeeds...

 

// Verify Insert.
if (mysqli_stmt_affected_rows($stmt)==1){
	// Insert Succeeded.
	echo '<div id="box_Content_700b">';
	echo	'<h1>Member Account Created</h1>';
	echo	'<p>Congratulations!</p>
		<p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p>
		<p>Please click on the link in that e-mail to activate your account.</p>';
	echo '</div>';
	// Send Email.

}else{
	// Insert Failed.
	echo '<div id="box_Content_700b">';
	echo	'<h1>Account Creation Failed</h1>';
	echo	'<p>You could not be registered due to a system error.</p>';
	echo	'<p>Please contact the System Administrator.</p>';
	echo '</div>';
}// End of VERIFY INSERT.

 

 

Questions:

1.) Would it be better if I put those messages in MySQL and just queried the one I needed?

 

2.) If so, can I put the entire message and markup in the database, so everything is ready to display?

 

3.) Should I use VARCHAR() or TEXT()?

 

Thanks,

 

 

Debbie

 

 

Link to comment
Share on other sites

Yes you can do this. If you are putting this entire thing in:

echo '<div id="box_Content_700b">';
	echo	'<h1>Member Account Created</h1>';
	echo	'<p>Congratulations!</p>
		<p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p>
		<p>Please click on the link in that e-mail to activate your account.</p>';
	echo '</div>';

 

Then definitely use text.

 

Going off a lot of your posts with such code in it, I have concluded that you really need to use a template solution. I would recommend that even if you don't understand or want to dive into OOP yet, that you at least learn the concepts and principles behind MVC. Mostly the part about separating business and presentation code.

 

Consider the following:

function load_template($template, $data = array())
{
// extract $data array into variables
extract($data);

// is the template file found?
if (is_readable('template_path/' . $template . '.php')) {
	require 'template_path/' . $template . '.php';
} else {
	die('Could not find template "' . $template . '"');
}
}

 

Now your template folder might look like this:

header.php
footer.php
account_success.php

 

header.php would contain the "top" of your site. In other words, it would contain all of the HTML markup up to the point where your main content would be. So like

<!DOCTYPE html>
<html>
  <head>
    <title>blah</title>
  </head>
  <body>
    <div id="content">

 

And then footer.php would close any open tags from header.php. So like

    </div>
  </body>
</html>

 

Then in each template file, you simply include these files at the top and bottom of your template, and put the content in between. So your "account_success" file would be like

<?php require 'header.php'; ?>

<div id="box_Content_700b">
<h1>Member Account Created</h1>
<p>Congratulations!</p>
<p>Your account has been created, and a confirmation e-mail sent to: "<?php echo $email; ?>"</p>
<p>Please click on the link in that e-mail to activate your account.</p>';
</div>

<?php require 'footer.php'; ?>

 

Finally, here is the usage:

if (mysqli_stmt_affected_rows($stmt)==1){
load_template('account_success', array('email' => $email));
} else {
load_template('account_failed');
}

 

Here is the huge benefit from this. You can create "success" and "failure" templates, and then simply pass in a title and a message - now you can display a message with one line of code, and if you ever change the markup, you don't have to change it in multiple places.

 

Hope this gives you something to think about.

Link to comment
Share on other sites

You should use as little db activity as possible, because it's always faster to get things from the file system. This is what I would do:

 

<div id="box_Content_700b">

<?php
// If db insert successful
if( mysqli_stmt_affected_rows( $stmt ) == 1 )
{
?>

<h1>Member Account Created</h1>
<p>Congratulations!</p>
<p>Your account has been created, and a confirmation e-mail sent to: "<?php echo $email; ?>"</p>
<p>Please click on the link in that e-mail to activate your account.</p>

<?php	
}
// If db insert failed
else
{
?>

<h1>Account Creation Failed</h1>
<p>You could not be registered due to a system error.</p>
<p>Please contact the System Administrator.</p>

<?php
}
?>

</div>

Link to comment
Share on other sites

You should use as little db activity as possible, because it's always faster to get things from the file system. This is what I would do:

 

Not always, but in this case yes, it is.

 

<div id="box_Content_700b">

<?php
// If db insert successful
if( mysqli_stmt_affected_rows( $stmt ) == 1 )
{
?>

<h1>Member Account Created</h1>
<p>Congratulations!</p>
<p>Your account has been created, and a confirmation e-mail sent to: "<?php echo $email; ?>"</p>
<p>Please click on the link in that e-mail to activate your account.</p>

<?		
}
// If db insert failed
else
{
?>

<h1>Account Creation Failed</h1>
<p>You could not be registered due to a system error.</p>
<p>Please contact the System Administrator.</p>

<?php
}
?>

</div>

 

Honestly, I absolutely loathe code like this. It is so annoying to maintain.

Link to comment
Share on other sites

scootstah,

 

I am really tired right now, and my brain isn't functioning so well, however I *think* see what you were doing in your example.  (Thanks by the way!!)

 

One thing, however, that strikes me as weird is how your example *implies* that I would have all of these hard-coded templates (e.g. success message, failure message, etc)

 

What I am trying to do is set up a generic "message template" with a header and footer include, but then populate the center of the template with the actual message needed.

 

That way I only need on template for messages, and I can grab what I need either from MySQL or a file.

 

Also, I didn't follow in your example what I would do if I had a whole bunch of places where I needed variable and what I would do if I needed a variable to hold something like:

<h1>Some Header</h1><p>Some long message</p><p>More text here</p>

 

Not saying your example doesn't have its own merits, but am still trying to relate it to what i am trying to do...

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

If you make a "success" and "failure" (or error) template, then it enables you to more easily reuse code. At the end of the day, reused code is efficient code.

 

If you had this template:

<?php require 'header.php'; ?>

<div class="success">
     <h1><?php echo $heading; ?><h1>
     <?php echo $message; ?>
</div>

<?php require 'footer.php'; ?>

 

Now, anywhere that you want to display a "success" message, you simply do:

load_template('success', array(
'title' => 'Member Account Created',
'message' => '<p>Congratulations!</p>
<p>Your account has been created, and a confirmation e-mail sent to: "' . $email . '"</p>
<p>Please click on the link in that e-mail to activate your account.</p>';
));

 

Now, you could improve these even further by having a "language" file... which sort of answers your original question. Basically a language file is just a php file containing a big array of messages and text that appears on your website. The idea is that in a multilingual environment, all you need to do is simply translate the english text to spanish (for example) and call it spanish.php, include it, and now all your text and messages are in spanish.

 

But, even if you will only ever use one language, language files are still awesome. So if you had a file named english.php, you could do:

$lang['account_create_success'] = '<p>Congratulations!</p>
<p>Your account has been created, and a confirmation e-mail sent to: "%s"</p>
<p>Please click on the link in that e-mail to activate your account.</p>';

 

In case you need a variable in the text, you can use sprintf syntax to accomplish that. So in place of $email we used %s. Then when you call it just use sprintf();

load_template('success', array(
'title' => 'Member Account Created',
'message' => sprintf(lang('account_create_success'), $email)
));

 

Also, a side note - you don't need all those <p> tags in there...it's kind of old school. Just use line breaks (<br />).

 

Link to comment
Share on other sites

Finally, here is the usage:

if (mysqli_stmt_affected_rows($stmt)==1){
load_template('account_success', array('email' => $email));
} else {
load_template('account_failed');
}

 

Here is one place where I am struggling to understand what you are proposing...

 

The code you have above would presumably be in my original create_account.php web page where the form was at.

 

If you load the template here by using require, aren't you just appending it to the current create_account.php page??

 

What I was trying to do was have create_account.php be where the User registers and then results.php be a separate page where the outcome of the User's registration is displayed.  (Data validations would - of course - be handled on the form on the first page!)

 

You code is loading a message either appended to the end of the first page of it is over-writing the first page, but it really seems I need two pages here.

 

I'll wait for you to respond and tell me what you have in mind...

 

 

Here is the huge benefit from this. You can create "success" and "failure" templates, and then simply pass in a title and a message - now you can display a message with one line of code, and if you ever change the markup, you don't have to change it in multiple places.

 

Hope this gives you something to think about.

 

Okay, but how is that different than what I was trying to do?

 

My work flow was this...

 

- Display "Create an Account" Form (page 1)

- User completes Form (page 1)

- Form validates data and displays any error message as needed (page 1)

- Script attempts to INSERT User into database (page 1)

- The outcome of this attempt is assigned to a variable, e.g. Pass/Fail (page 1)

- Outcome is assigned to $_SESSION

- Script redirects to Outcome Page (page 2)

- Values in $_SESSION are read (page 2)

- PHP takes these values and determines which message to display (page 2)

- Query MySQL for the correct Outcome Message (page 2)

- Script inserts the correct Outcome Message into the results.php page (page 2)

 

So, with how I was envisioning things, I have one Form and one Display Template that is populated with the appropriate data based on the values it gets from the first page.

 

Anyways, I'll wait to hear back from you, scootstah, and see how close or far off I am?!  :shrug:

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

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.