Dragen Posted June 8, 2007 Share Posted June 8, 2007 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 More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 I might have misunderstood you, but can't you use preg_match() to fetch out the number from the input then deal with it accordingly..? Link to comment https://forums.phpfreaks.com/topic/54779-variable-gets-any-number-put-to-it/#findComment-270888 Share on other sites More sharing options...
Dragen Posted June 8, 2007 Author Share Posted June 8, 2007 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.. Link to comment https://forums.phpfreaks.com/topic/54779-variable-gets-any-number-put-to-it/#findComment-270894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.