Solved

Help with trivial validation function

Posted on 2009-07-06
4
246 Views
Last Modified: 2013-12-13
Hi everyone,
can anyone please help me do this right? I am trying to validate a form with category id, of which list I pull out of the DB to make sure the id is valid. But seem to get errors of invalid argument for "foreach". I am attaching the function below. I am a sort of beginner, so I am sure that everyone will have much better solution, but I am trying to do it myself (with this exception) and just need to find an error in my script.. not an entire new solution. that will be next :-)
$category_id_errors = check_cagegory_id(array('category')); 
 
 
function check_cagegory_id($check_category_id) {  //this is the funtion referred to in the code above
	$form_errors = array();
	
	$subject_set = get_all_subjects();
	if(mysql_num_rows($subject_set)>=1) {
		$subjects = array();
		while($subject = mysql_fetch_array($subject_set)) {
			$subjects[] = $subject['id'];
		}
		foreach($check_cagegory_id as $fieldname) {
			if((!isset($_POST[$fieldname]))||(!in_array($_POST[$fieldname], $subjects))) {
				$form_errors[] = $fieldname ."doesn't exist";
			}
		}
	} else {
		$form_errors[] = $fieldname." - no category to associate a new page with";
	}
	return $form_errors;

Open in new window

0
Comment
Question by:czechmate1976
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:kevin_u
ID: 24786760
In the foreach,

$check_cagegory_id

is spelled wrong

It should be:

$check_category_id

0
 

Author Comment

by:czechmate1976
ID: 24787260
Thanks, I corrected it but still get "Warning: Invalid argument supplied for foreach() in C:\xampplite\htdocs\FGEnew\siteadmin\includes\functions.inc.php on line 218"

which is the following:
foreach($check_category_id as $fieldname) {
                  if((!isset($_POST[$fieldname]))||(!in_array($_POST[$fieldname], $subjects))) {
                        $form_errors[] = $fieldname ."doesn't exist";
                  }
0
 
LVL 12

Accepted Solution

by:
kevin_u earned 250 total points
ID: 24787336
The underlying issue is that $check_category_id is not an array.  Thats the cause of the error message.   In your short example, it is surely an array.  But is it called the same way in the full script?
0
 

Author Comment

by:czechmate1976
ID: 24787404
resolved. Sorry, after days staring at the code I can hardly see.. Thanks for your help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
The viewer will learn how to count occurrences of each item in an array.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question