Re: [PHP] removing an array element

2002-01-11 Thread Erik Price

Because, as a dope, I forget that you can invert IF statements like 
that -- you're right, I'd rather do that than mess with the array.

Although the feedback on unset($arrayname['index']) was extremely useful 
knowledge!  Thank you everyone.

Erik


On Friday, January 11, 2002, at 12:31  PM, Rasmus Lerdorf wrote:

> Why not just add a simple check inside that while loop?
>
>if($temprow['div_id'] != $home_div_id)
>   echo "   . "\">" . $temprow['div_name'] . "\n";
>
>
> Seems more logical to me than to mess with the array itself.
>
> -Rasmus
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] removing an array element

2002-01-11 Thread Henning Sprang

Erik Price wrote:

> what function is used to remove an array element from an array?  Like 
> array_pop(), except one by which I can refer to the element by its 
> associative index rather than just the last element of the array.



> 
> 
> For more detail with what I'm doing, I have two versions of a  
> listbox -- one that includes an  with selected="yes" (if the 
> user is calling this page from submitting a form).  The other version is 
> for users who are NOT calling this page from submitting a form, i.e. for 
> the first time.
> 
> I would like, in the first version (if ($formfilled)), to REMOVE the 
> element $temprow['div_name'] from the array $temprow so that it doesn't 
> appear a second time (in the WHILE loop), since it has already been 
> echoed above the WHILE loop.
> 
> Is this even possible?



I would do it another way.

say you have all the possible choices in an array that come out of a 
database, and ypou want, if the user already selected one, this one to 
be selected, right?
Then i would set the selected atribute only if the value out of the 
database is the same as the one submitted by the form:

like this (omitting the surrounding html):

$query= "select distinct * from sometable";

$result=mysql_query($query);

// the select field is named selectfield,

echo "";
echo "";

// the variable $selectfield is, if set the value submitted by the form
$selectfield = $_POST["selectfield"];

while ($row=mysql_fetch_array($result)){
echo "" . $row[1] . "\n";
}
echo ""

echo "\n";
echo "";




i think this is a more straightforward solution for your problem...


henning









-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] removing an array element

2002-01-11 Thread Rasmus Lerdorf

> what function is used to remove an array element from an array?  Like
> array_pop(), except one by which I can refer to the element by its
> associative index rather than just the last element of the array.

   unset($array['index']);

>  echo "\n";
>  echo " selected=\"yes\">$div_name\n";
>  while ($temprow = mysql_fetch_array($tempresult)) {
>  echo ". "\">" . $temprow['div_name'] . "\n";
>  }
>  echo "\n";

Why not just add a simple check inside that while loop?

   if($temprow['div_id'] != $home_div_id)
  echo "" . $temprow['div_name'] . "\n";


Seems more logical to me than to mess with the array itself.

-Rasmus


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]