Page 1 of 1

Display errors in a foreach loop where i want on the page

Posted: Thu Aug 28, 2008 6:40 am
by EverythingEvolves
Hi,

I wonder if someone can help me. I'm pretty sure there is a fairly simple solution to this problem but I am fairly new to PHP so any assistance would be greatly appreciated.

I have this program that posts data from a form on a page called "add_article.php" to an SQL database. Before posting it checks that all the fields have been completed. The program works fine and it checks for errors correctly, and uploads the information before going to a page called "add_article_success.php". If however there is an error and a field has been left blank, the program sends the user back to "add_article.php" and displays a list of information about the boxes not completed. I'm just having trouble getting the error messages to display in the right place on page. Here is my existing code.

Code: Select all

<?php
/*   Program name:  scrpt_upload_article.php
 *  Description: Checks that all fields have been filled in and then uploads a new artilce into the Contribute Database 'articles' table
 */
 
$id = $_POST['id'];
$article_title = $_POST['article_title'];
$article_desc = $_POST['article_desc'];
$article_keywrd = $_POST['article_keywrd'];
$article_text = $_POST['article_text'];
$article_date = $_POST['article_date'];
//*  Check information from the form has been filled in correctly
//*  Set up the array of the field labels
$label_array = array(  "article_title" => "Section 1: The article title", 
      "article_desc" => "Section 2: The article description", 
      "article_keywrd" => "Section 3: The article keywords", 
      "article_text" => "Section 4: The main article text");
foreach ($_POST as $field => $value)
{
//*  Check there's no blank fields
if ($value == "")
 {
 $blank_array[$field] = "blank";
 }
} // end of foreach for $_POST
//*  If any of the fields were blank, redisplay form and error messages
if (@sizeof($blank_array) > 0)
{
//*  display missing information message
$error_title = "Unfortunately you did not fill in all of the required fields, please check the following:</br><br>";
$error_break = "</br><br>";
foreach ($blank_array as $field => $value)
 {
  echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
 }
include ("add_article.php");
exit();
}
else //if the data is O.K
{
$host="host"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Do the funky INSERT thing
$query = "INSERT INTO articles (id,article_title,article_desc,article_keywrd,article_text,article_date)
   VALUES ('$id','$article_title','$article_desc','$article_keywrd','$article_text','$article_date')";
$result = mysql_query($query) or die ("Could not execute query.");
include ("add_article_success.php");
}
?>
 
This displays all the errors except it does so in the top left hand corner of the page.

If I change the line

Code: Select all

echo "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
for something like

Code: Select all

$errors = "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
and then put <?php echo $errors; ?> in the "add_article.php" page it will only print one error result. I need it to print all the appropriate errors, there are a possible four.

Any help would be greatly appreciated.

Re: Display errors in a foreach loop where i want on the page

Posted: Thu Aug 28, 2008 7:09 am
by Ziq
You rewrite data in $errors every foreach loop. You should use concatenation operator '.'
Try this:

Code: Select all

 
$errors = ''; //  define $errors variable
foreach ($blank_array as $field => $value)
 {
  $errors .= "&nbsp;&nbsp;&nbsp;{$label_array[$field]}<br>";
 }
 

Re: Display errors in a foreach loop where i want on the page

Posted: Thu Aug 28, 2008 7:30 am
by EverythingEvolves
That's fantastic - thank you so much! I really really appreciate it and understand how it works now.

Brilliant!