Jump to content

[SOLVED] Exploding array in error message


conker87

Recommended Posts

I have a website where users can upload articles, reviews etc, etc. I have the basic upload script made (the site is quite old now) but I want to add some error catching in it. I have this so far:

<?php
if (empty($_POST['title']))
{
	$error[] = "title";
	$errorb = true;
}
if (empty($_POST['platform']))
{
	$error[] = "platform";
	$errorb = true;
}
if (empty($_POST['genre']))
{
	$error[] = "genre";
	$errorb = true;
}
if (empty($_POST['content']))
{
	$error[] = "content";
	$errorb = true;
}
if (empty($_POST['rate']))
{
	$error[] = "rate";
	$errorb = true;
}
if (empty($_POST['copyright']))
{
	$error[] = "copyright";
	$errorb = true;
}
if ($errorb == true)
{
	$body .= "<h3 style='color: red;'>You must fill in the following fields: <fields>.</h3>";
}
else
{ //etc
} ?>

Now I'm not too sure how to explode the $error array so it'll echo something like "title, content" etc if they are empty in the $body variable.

 

Can anyone shed some light?

Link to comment
https://forums.phpfreaks.com/topic/167144-solved-exploding-array-in-error-message/
Share on other sites

here you'll actually want implode(). i should also point out that you don't need to set the $errorb flag - simply checking if the $error array exists is sufficient, since it won't be defined unless one of the fields is missing:

 

if (empty($_POST['copyright']))
   {
      $error[] = "copyright";
   }
if (isset($error) && !empty($error))
   {
      $body .= "<h3 style='color: red;'>You must fill in the following fields: ".implode(', ', $error).".</h3>";
   }
else
   { //etc
   } ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.