Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get multiple value from user input from text field and want to explode or keep adding into if condition statement

Here is my code

foreach ($list['post'] as $item) {
    if( ($item['single']['catid'] != 8) AND ($item['single']['catid'] != 4) ){ 
        $this->list_post($item); 
    }
}

Now what exactly I am looking for is in if( ($item['single']['catid'] != 8) AND ($item['single']['catid'] != 4) ) I want allow user to add multiple category ID and each ID will add AND and further id code AND ($item['single']['catid'] != 4)

I never done this before and don't know either this is proper way to do or any other possible better way.

Thanks a lot

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

You should have some kind of an array of the category IDs you want to check for, for example:

$categories = array(8, 4);

Then you could use something like the in_array(needle, haystack) built-in function of PHP.

Your if condition would become like that one: if (!in_array($item['single']['catid'], $categories)) {.

You should be using the above, but I am going to give you an idea of how it works, so you can understand the principle for more complex issues:

function exists($target, $array) { foreach ($array as $element) { // Go through each element in the array if ($element == $target) { // Check to see if any element there is what you are searching for return true; // Return true, that it does exist, and stop there. } else { // Just ignore it... } } return false; // If you get here, it means nothing returned true, so it does not exist... }

To be used as if (exists($item['single']['catid'], $categories)) {.

It wouldn't work if it was "inside" the if statement because you have to do some processing before evaluating if it exists or not. So you either could have done that before the if statement, and store the result in a variable, or use a function (which PHP provides).

Hopefully the concept will help you fir more complex problems...

Note: this assumes your input is in the form of an array, which you can build via various ways, if not provided as is directly.

Update:

The input you get via the input field is sent through form, to which you specify either POST or GET as a method. Assuming it is POST that you are using, this is how you'd get the input as a string as it was entered in the text field:

$categories_string = $_POST['the_name_field_in_the_input_tag'];

After that you have to understand it as a list of items, let's say separated by commas: 1,3,5,8. Then this is simply separating by commas. You can use explode($delimiter, $string). Like that:

$categories_array = explode(',', $_POST['categories']);

But you cannot trust the input, so you could get something like 1, 2, 3,5,6. The spaces will mess it up, because you will have spaces all around. To remove them you can use trim for example.

$categories = array(); // Create the array in which the processed input will go
foreach ($categories_array as $c) { // Go through the unprocessed one
    $categories[] = trim($c) * 1; // Process it, and fill the result. The times one is just so that you get numbers in the end and not strings...
}

Then you can use it as shown earlier, but keep in mind that this is just an example, and you might not even need all these steps, and there are much more efficient ways to process this input (regular expressions for example). But the concern here is not sanitizing input, but keep in mind you will need to do that eventually.

Hope it's clear enough :)

share|improve this answer
 
Thanks a lot for great detailed description and understanding. Appreciate. I am trying both now to understand and get back to you. Yes I am using form ( input field ) to get IDs so user can add something like 1, 5, 10, 52..... –  Jatin Soni Nov 5 '12 at 17:26
 
@pixelngrain You're welcome :) I updated the answer, check to see if it's clear! –  jadkik94 Nov 5 '12 at 17:37
 
Great help and explanation. Thanks a lot and working fine. –  Jatin Soni Nov 5 '12 at 17:50
add comment

You might be better off with in_array() for checking a value against a variable number of possibilities.

share|improve this answer
 
Thanks for quick help. Can you please give me some example? –  Jatin Soni Nov 5 '12 at 17:04
add comment

I'm not sure I understand your problem. You want user to be able to input different values, e.g.:

$string = "5, 6, 7, 8, 10";

Afterwards, you want to check if 'catid' is not in that array and if it isn't you want to run $this->list_post($item);

If so, then you should use something like this:

$values = explode(", ", $string); //make array from values
foreach ($list['post'] as $item) {
    if (!in_array($item['single']['catid'], $values)) { //check whether catid is in array
        $this->list_post($item); // execute whatever you want
    }
}

Hope it helps.

share|improve this answer
 
Thanks Dygestro for your code. I will check and get back to you –  Jatin Soni Nov 5 '12 at 17:26
 
Your answer is definite solution but jadkik94 gives some details knowledge. I appreciate your help and effort. --Tanks a lot –  Jatin Soni Nov 5 '12 at 17:51
 
Sure thing, glad I could help. –  Dygestor Nov 5 '12 at 18:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.