Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should have some kind of an array of the category IDs you want to check for, for example:</p> <pre><code>$categories = array(8, 4); </code></pre> <p>Then you could use something like the <code>in_array(needle, haystack)</code> built-in function of PHP.</p> <p>Your if condition would become like that one: <code>if (!in_array($item['single']['catid'], $categories)) {</code>.</p> <p>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:</p> <p>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... }</p> <p>To be used as <code>if (exists($item['single']['catid'], $categories)) {</code>.</p> <p>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 <code>if</code> statement, and store the result in a variable, or use a function (which PHP provides).</p> <p>Hopefully the concept will help you fir more complex problems...</p> <p><strong>Note</strong>: this assumes your input is in the form of an array, which you can build via various ways, if not provided as is directly.</p> <p><strong>Update</strong>:</p> <p>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 <strong>as a string</strong> as it was entered in the text field:</p> <pre><code>$categories_string = $_POST['the_name_field_in_the_input_tag']; </code></pre> <p>After that you have to understand it as a list of items, let's say separated by commas: <code>1,3,5,8</code>. Then this is simply separating by commas. You can use <code>explode($delimiter, $string)</code>. Like that:</p> <pre><code>$categories_array = explode(',', $_POST['categories']); </code></pre> <p>But you cannot trust the input, so you could get something like <code>1, 2, 3,5,6</code>. The spaces will mess it up, because you will have spaces all around. To remove them you can use <code>trim</code> for example.</p> <pre><code>$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... } </code></pre> <p>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.</p> <p>Hope it's clear enough :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload