Jump to content

Recommended Posts

Hi,

The title for this topic isn't very helpfull, but couldn't think of a better one..

I've got an online form which Im writting an error check for it. I have several inputs with the names 'download%' where '%' is a number.

Is there a way of getting a statement to check all downloads with any number at the end?

What I've got is a foreach statement checking all inputs, with a switch to get the names of each post.

<?php
foreach($_POST as $key => $value){
switch($key){
	case 'download1':
		//do something
	break;
	case 'download2':
		//do something
	break;
	case 'download3':
		//do something
	break;
}
}
?>

now instead of doing a seperate case for each download (as they'll all be identical) could I somehow have it so the one case gets all of them with a number check of some sort?

so the % counts as any number, so any post titled download followed by a number will be checked by the same case statement..

<?php
foreach($_POST as $key => $value){
switch($key){
	case 'download%':
		//do something
	break;
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/54779-variable-gets-any-number-put-to-it/
Share on other sites

I could, but then I'd need another statement to find that.

At the moment, when I submit the form it goes through each post using the foreach. I've got different cases for each post variable such as:

				case 'time%20period':
				if(array_search($value, $timepa) === false && $_POST['time%20num'] != ''){
					$error[$erid] = "Please select the length of advertising (week, month) from the drop-down box"; //adds the errors to an array
					$erid++;
				}
			break;

I don't want to check what number the download input is, then do something with it, I want php to run through all of the inputs as it's doing but for the 'download%' to capture any of the downloads (download1, download2, download3 etc) without having to set a number to the case like this:

case 'download1':

I thought there might be a simple function that simply reads any number in a given string..

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.