Topics: 15 | Posts: 14 | Comments: 12 | Members: 233
If you have worked with PHP on a regular basis then there is the possibility that you have encountered a clean screen even with countless lines of PHP codes. This is usually caused by a missing or misplaces \';\', \'}\', \',\' or \'\'\' and you don\'t want this happening fi you have lots of unformatted codes. To resolve this, there are a few steps you can take either to find or instantly resolve the problem. First of all, this is something you should always take into consideration and recommend it to your friends. It may sound useless, but it would surely help you to faster find the point of penetration; Always comment each function or line of code you write. If you are now sure how it works, check out the sample below.
<?php
// Comment each line
fuction doSomething($string) {
// Perform function
return string;
}
?>
Next, something else you can try is that of enabling the display of all errors thrown in your PHP document. Not all errors are displayed by default so you might just be lucky and find the exact line where that error is occurring and why. You can do this by placing the following code to the top of your document where it can capture all operations coming after.
<?php
// Comment each line
ini_set(\'display_errors\',1);
error_reporting(E_ALL);
?>
Finally, one of the most important steps you can take is that of continuous testing during development. Always ensure that you test each portion of code or function before you go ahead and publish your entire document just to find out that there are issues that needs your utmost attention.
Well, that\'s basically how this works and by following these simple steps, you should be safe from any issues.
replies
If you got extensive content like descriptions and would like just a brief section of that content for example when you you briefly list the articles in blog. With this function which I\'ve whipped up, you are provided with the choice of using your own separator in which case, your words would not necessarily be separated by spaces. So whether you have \"I love my family more than my friends.\" or \"I-love-my-family-more-than-my-friends.\", you can use the this function to strip your words.
<?php
function limit_words($limit,$string,$seperator) {
$isConsistent = str_replace($seperator,\"\",$string,$isc);
if ($isc < 1) {
echo \"Sorry, but your search string does not consist of your seperation string\";
} else {
$string = explode($seperator,$string);
if ($limit > count($string)) {
echo \"Your limit is more than the number of words available in your string.\";
} else {
$i = 0;
$new_string = \"\";
foreach ($string as $word) {
$i++;
if ($i <= $limit) {
$new_string .= $word;
if ($i < $limit) { $new_string .= $seperator; }
}
}
return $new_string;
}
}
}
?>
Now, let\'s create a brief example using the above function to strip the first four (4) from our sample string above.
<?php
// Define our string or content
$string = \"I love my family more than my friends.\";
// Set our word limit
$limit = 4;
// Set our separation character
$separator = \" \";
// Begin our function
$new_string = limit_words($limit,$string,$separator);
echo $new_string; // I love my family
The above family will use the input string I love my family. and using the space [ ] separator, we would output the first four (4) which would give us: I love my family
As I said before, you are not limited to the space character. I have also built in a basic authentication system for validating the function.
replies
If you have a database which consists of numerous results which you would like users to view as they read then this might be what you are looking for. What I\'m going to do is show you the basic functionality behind continuously loading data from a MySQL Database till the end without switching pages. Let\'s say you have a table called articles which we created using the following function:
CREATE TABLE articles (
ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
title varchar(86) NOT NULL,
description text NOT NULL
)
So we are going create our first portion of our code which will be the JavaScript necessary for processing and loading the additional articles after our link is clicked.
<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/latest/jquery.js\"></script>
<script type=\"text/javascript\">
$(function()
{
$(\'.load_more\').live(\"click\",function()
{
var ID = $(this).attr(\"id\");
if(ID)
{
$(\"#load\"+ID).html(\'Loading...\');
$.ajax({
type: \"POST\",
url: \"more_articles.php\",
data: \"prevID=\"+ ID,
cache: false,
success: function(html){
$(\"#articles\").append(html);
// remove the previous load more link
$(\"#load\"+ID).remove();
}
});
}
else
{
// End or articles (no more)
$(\".load_more\").html(\'\');
}
return false;
});
});
</script>
Next we have our main articles.php file which will list the first set of our articles and show our load more button.
<div id=\"articles\">
<?php
// database connection file
include(\"config.inc.php\");
// get article information from database
$articles_data = mysql_query(\"SELECT * FROM articles ORDER BY ID DESC LIMIT 10\") or die(mysql_error());
// loop through each article and output the information
while($articles_info = mysql_fetch_array($articles_data)) {
$articleID = $articles_info[\'ID\'];
$title = $articles_info[\'title\'];
$description = $articles_info[\'description\'];
?>
<div class=\"article\">
<h1>
<?php echo $titie; ?></h1>
<p>
<?php echo $description; ?></p>
</div>
<?php } ?>
</div>
<!-- This will be our load more link with $articleID being the last article loaded -->
<div id=\"load<?php echo $articleID; ?>\" class=\"load_more\">
<a href=\"#\" class=\"load\" id=\"<?php echo $articleID; ?>\">Load More</a>
</div>
Finally we have the more_articles.php file which will receive the last Article ID via ajax and display more articles if available.
<?php
// database connection file
include(\"config.inc.php\");
// get the article ID from ajax POST
if (isset($_POST[\'prevID\']) && $_POST[\'prevID\'] != \"\") {
$lastID = mysql_real_escape_string($_POST[\'prevID\']);
// get article information from database
$articles_data = mysql_query(\"SELECT * FROM articles WHERE ID < \'$lastID\' ORDER BY ID DESC LIMIT 10\") or die(mysql_error());
while($articles_info = mysql_fetch_array($articles_data)) {
$articleID = $articles_info[\'ID\'];
$title = $articles_info[\'title\'];
$description = $articles_info[\'description\'];
?>
<div class=\"article\">
<h1>
<?php echo $titie; ?></h1>
<p>
<?php echo $description; ?></p>
</div>
<?php } ?>
<!-- This will be our load more link with $articleID being the last article loaded -->
<div id=\"load<?php echo $articleID; ?>\" class=\"load_more\">
<a href=\"#\" class=\"load\" id=\"<?php echo $articleID; ?>\">Load More</a>
</div>
<?php } ?>
This is the basic functionality behind dynamically loading database information using PHP, jQuery and Ajax.
replies
If you are looking to send email to your users or friends directly from your server, then this might be the topic for you. I’m just going to display the basic fundamentals of a PHP email along with the necessities to ensure functionality, delivery and user credibility. So basically, with php emails, we will be using a pre-built function called mail() and you can use any text editor or text based web builder like Notepad++ to create your mailer. Within this function, there are four basic components that we will need. These include;
<?php
// who is receiving the message
$to = \"[email protected]\";
// the name from which the message is being sent
$from_name = \"[myWebsite]\";
// the email address from which the message is being sent
$from_address = \"[email protected]\";
// main point of the message or why it is being sent
$subject = \"New comments of your post: \'Building a proposal\' on [myWebsite]\";
// the actual message to be sent
$body = \"
Hello Jain,
We would like to notify you that you recently receiver 15 comments on your profile post titled \'Building a proposal\'.
You can login to your account from http://mywebsite.com/login to view all updates and feedback you received from your friends on [myWebsite].
Sincerely,
John Doe
Notifications Manager
[myWebsite]\";
// required components of the email
$headers = \"MIME-Version: 1.0\\r\\n\"
.\"Content-Type: text/plain; charset=utf-8\\r\\n\"
.\"Content-Transfer-Encoding: 8bit\\r\\n\"
.\"From: =?UTF-8?B?\". base64_encode($from_name) .\"?= <$from_address>\\r\\n\"
.\"X-Mailer: PHP/\". phpversion();
// call our mail() function and send the message
mail($to, $subject, $body, $headers, \"-f $from_address\");
?>
Now with this, there is one requirement of you relative to your server. The email from which you are sending message needs to be created and valid for security reasons.
This is just the simplest method (that I know of) for sending a message to another person. For additional information or more detailed instructions, you can contact me at [email protected].
replies