Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter data from text box to form 1

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi need help with a couple things here. At the bottom is the code I'm using the problem I'm having is I have a text box which stores the values for the people who're allowed to do certain features to my script (ie whoview = who can view it).

What my script does is checks if any of values in the array is in the specific text box and then sets a unique variable tobe selected (this is for my form).

An example of what the text box may look like (Super Administrator, Moderator, Member, Username1, Username2,). This is where I need the help because I also need to set a variable (ie for whoview use $whoview) tobe equal to the values of the textbox which is not in the array. I was also wondering if there was an easier way of doing my solution without setting up any functions.

Hope that makes sense and I'd be greatful if you could help. Thanx

Here's my script:

$ranks = array("Super Administrator", "Administrator", "Super Moderator", "Moderator", "Member");

for($i = 0; $i < count($ranks); $i++) {
if(eregi($ranks[$i], $set[whoview])) {
$rank = $ranks[$i] //HOW DO I GET IT SO THAT IT CONVERTS SPACES TO UNDERSCORES
$rank = &quot;whoview$rank&quot;;
$$rank = &quot;selected&quot;;
}

if(eregi($ranks[$i], $set[whocreate])) {
$rank = $ranks[$i] //HOW DO I GET IT SO THAT IT CONVERTS SPACES TO UNDERSCORES
$rank = &quot;whoview$rank&quot;;
$$rank = &quot;selected&quot;;
}

if(eregi($ranks[$i], $set[whoadd])) {
$rank = $ranks[$i] // HOW DO I GET IT SO THAT IT CONVERTS SPACES TO UNDERSCORES
$rank = &quot;whoview$rank&quot;;
$$rank = &quot;selected&quot;;
}
}
 
An example of what the text box may look like (Super Administrator, Moderator, Member, Username1, Username2,). This is where I need the help because I also need to set a variable (ie for whoview use $whoview) tobe equal to the values of the textbox which is not in the array.

This is the bit I'm really interested in. Thanx for the preg bit though.
 
I assumed you misread it, basically this is what I want, say I have a variable $text, which can look like the following (without the quotes):

&quot;Super Administrator, Moderator, Member, Username1, Username2,&quot;

or

&quot;Administrator, Moderator, Member, Username1, Username2,&quot;

etc

Based on this array:

$ranks = array(&quot;Super Administrator&quot;, &quot;Administrator&quot;, &quot;Super Moderator&quot;, &quot;Moderator&quot;, &quot;Member&quot;);

I wish tobe able to declare a variable equal to the contents of $text but without the text from the array ie

&quot;Administrator, Moderator, Member, Username1, Username2,&quot;

becomes

&quot;Username1, Username2,&quot;
 
I would use explode() or preg_split() to split the value of the text box into an array.

I would then loop through the values of the new array, and look for the value in your baseline $ranks array.

If the value from the new array appears as a value in $ranks, unset() that element to remove it from the new array.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanx, why do I get:


When I try:

<?

$ranks = array(&quot;Super Administrator&quot;, &quot;Administrator&quot;, &quot;Super Moderator&quot;, &quot;Moderator&quot;, &quot;Member&quot;);

$test = &quot;Super Administrator, Administrator, Moderator, User1, User2,&quot;;

$test = explode(&quot;,&quot;, $test);

for($i = 0; $i < count($ranks); $i++) {
for($j = 0; $j < count($test); $j++) {
if($test[$j] == $ranks[$i]) {
$rank = str_replace(&quot; &quot;, &quot;_&quot;, $ranks[$i]);
$rank = &quot;whoview$rank&quot;;
$$rank = &quot;selected&quot;;
echo &quot;|$rank|<br>&quot;;
} else {
$whoview .= &quot;, $ranks[$i] - $test[$j] - $test[$j]&quot;;
}
}
}

echo &quot;|||$whoview|||&quot;;

?>

I understand my code isn't perfect yet but I'm experimenting. The problem I am having is I am querying - if($test[$j] == $ranks[$i]) but when I echo $test[$j] - $test[$i] they're are displayed even though they're the same which it should do.
 
I don't know why you get the output you do -- but to be honest, I haven't looked. I generally don't debug other members' code.

What I was talking about was something nice and simple:

Code:
<?php
$ranks = array(&quot;Super_Administrator&quot;, &quot;Administrator&quot;, &quot;Super Moderator&quot;, &quot;Moderator&quot;, &quot;Member&quot;);
$test = &quot;Super Administrator, Administrator, Moderator, User1, User2,&quot;;

$test = preg_replace ('/\W*$/', '', $test);
$test = preg_replace ('/, */', '|', $test);
$test = preg_replace ('/ /', '_', $test);  

$test = explode ('|', $test);

foreach ($test as $key => $value)
{
	reset ($ranks);
	if (array_search($value, $ranks) !== FALSE)
	{
		unset ($test[$key]);
	}
}

print_r ($test);

?>

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanx, I might aswell learn this stuff whilst I've got the chance.

What does each of the following lines do:-

$test = preg_replace('/\W*$/', '', $test);
$test = preg_replace('/, */', '|', $test);
$test = preg_replace('/ /', '_', $test);

And what's the difference between != and !==

I tried using != but that did not display the results I wanted. Thanx again
 
The three preg_replace lines:

The first one removes any non-word characters that appear at the end of the value. Your example had a comma as its last character.

The second replaces all instances of commas and optional following spaces with pipe characters.

The third replaces all spaces with underscores.


Both &quot;!=&quot; and &quot;!==&quot; mean roughly &quot;not equal to&quot;. The second operator, though, is aware of different types of values.

The problem is that array_search() will return the array index of the value if the value is found in the array. Otherwise it returns FALSE. But what happens if the found value is in array index 0 (zero)?

Code:
zero != FALSE
will evaluate to FALSE. The &quot;==&quot; and &quot;!=&quot; operators consider all non-zero values to be equivalent to TRUE and all zero values equivalent to FALSE.

Code:
zero !== FALSE
will evaluate to TRUE. The &quot;===&quot; and &quot;!==&quot; operators do not consider non-zero values equivalent to TRUE and zero values equivalent to FALSE. These operators know the difference between numbers and boolean values.

I always make a point of using &quot;===&quot; and &quot;!==&quot; with functions that can return both numeric and boolean values, as does array_search(). It makes it possible to detect the difference between a return of success or failure.

The &quot;===&quot; and &quot;!==&quot; operators were added to the language in PHP 4. They are documented here:
Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top