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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace one element of an array in different pages 2

Status
Not open for further replies.

Kicket

Technical User
Jun 15, 2002
155
US
okay i have two pages

num1.php and num2.php
in num1.php i have an array:
$var=array ("0"=>"god",
"1"=>"dog",
"2"=>"gdo",
......
"10"=>"ooo";)

okay something like that
i have a script doing calculation of each element of the array,i want a variable return the key of "dog", then carried it to num2.php, then the user will change "dog" to "cat" or "pig", bring it back to num1.php, replace the 2nd elements of the array, which WAS dog before.

i just want to know, how to set a var represent the key of "dog", so i can replace it after i come back from num2.php. help please

ff
 
Use session variables to pass the array and the index of the array element to edit. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
1. session variable?
2. how to return the index?
3. ...

ff
 
You need to read up on PHP session management in the online documentation. It's not hard. You use session_start() to start session handling. You add a script variable by issuing session_register() with the name of your variable. You then issue session_start() in the next script, and the variables are there. Read up on these topics beginning here:

The only gotcha is that you can't have issued any HTML before doing all this.

Find the index of "dog", then store that key in a variable that has been registered in the session.


I guess you put off learning PHP's session handling until the second week? ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
Warning: Cannot send session cookie - headers already sent by (output started at /home/hlan/public_html/testing.php:2) in /home/hlan/public_html/testing.php on
line 3

Warning: Cannot send session cache limiter - headers already sent (output started at /home/hlan/public_html/testing.php:2) in /home/hlan/public_html/testing.php
on line 3







ff
 
I told you about that gotcha......... ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
how can u delete the post u post out previously?

anyway
can't really find a good funtion to return the key of an array
my script is something like this


for ($hhh = 0; $hhh <10; $hhh ++)
if ($var[$hhh]==&quot;dog&quot;)
{
here,how to return the key?;
}
elseif ($var[$hhh]==&quot;cat&quot;)
{
$now=key($var);which doesn't work
}
else
{
blahblahblah
}



ff
 
what if i have


......
&quot;5&quot;=>&quot;dog&quot;
...
...
...
&quot;11&quot;=>&quot;dog&quot;
...
...
&quot;13&quot;=>&quot;dog&quot;
.....


ff
 
Use it as advertised. Your associative array is the haystack, your animal type is the needle.

The only difference is that with a numerical array, it will return 0 for the first element, 1 for the second, etc.

With an associative array, it will return the key itself. So if you have the array:

[tt]
Code:
$foo = array (1 => &quot;dog&quot;, 2 => &quot;cat&quot;, 3 => &quot;fish&quot;, 4 => &quot;bird&quot;);

array_search (&quot;dog&quot;, $foo)
[/tt] will return 1.

Again, if you have the array:

[tt]
Code:
$foo = array (&quot;cat&quot; => &quot;dog&quot;, &quot;fish&quot; => &quot;cat&quot;, &quot;bird&quot; => &quot;fish&quot;, &quot;dog&quot; => &quot;bird&quot;);
[/tt]

Then:

[tt]
Code:
$bar = array_search (&quot;bird&quot;, $foo);
[/tt]

will return &quot;dog&quot;. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
but
in

$foo = array (1 => &quot;dog&quot;, 2 => &quot;cat&quot;, 3 => &quot;dog&quot;, 4 => &quot;dog&quot;);

array_search(&quot;dog&quot;,$foo);

will return 4, not 1,3,4

ff
 
array_search() is working as expected. You didn't mention wanting all duplicate entries found.

Try this functio on for size. It operates like array_search(), but returns an array containing a list of those keys which match the search.

Take the return of this function, stash it in a variable, and register that variable with the session handling system.

[tt]
Code:
<?php

function find_all_in_array ($needle, $haystack, $strict = FALSE)
{
	$retval = array();
	
	foreach ($haystack as $key => $value)
	{
		if ($strict === FALSE)
		{
			if ($value == $needle)
			{
				$retval[] = $key;
			}
		}
		else
		{
			if ($value === $needle)
			{
				$retval[] = $key;
			}
		}
	}
	
	if (sizeof($retval) == 0)
	{
		$retval = FALSE;
	}

	return $retval;
}


$foo = array (1 => &quot;dog&quot;, 2 => &quot;cat&quot;, 3 => &quot;dog&quot;, 4 => &quot;dog&quot;);

$bar = find_all_in_array (&quot;dog&quot;, $foo);

print &quot;<html><body><pre>&quot;;

if ($bar)
{
	print_r ($bar);
}
else
{
	print &quot;Not found&quot;;
}

print &quot;</pre></body></html>&quot;;

?>
[/tt] ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
thanks for the help man
the function above worked fine!

i have another question
my code is

$msg=array(1=>&quot;<a href=num2.php?number=$number>okay|<a>&quot;,
2=>&quot;<a href=num2.php?number=$number>okay|<a>&quot;,
3=>&quot;<a href=num2.php?number=$number>okay|<a>&quot;);

for ($var = 1; $var = 4; $var++)
{
$number=$var;
print $msg[$var];
}


the reason to have the number in there
is i want to know which variable it is i clicked to get on the the 2nd page
it suppose to equal to $var, right?
however, it's not working, any idea?


ff
 
Of course that's not going to work. Think about it -- when are the elements of your array initialized? When is the substring &quot;$number&quot; interpolated into a value and put into the array element? Hint: It's not during the for loop.

To accomplish what you seem to be trying, I suggest you take a look at either printf ( or sprintf (
Another thing: It's better to get into the habit of using a foreach loop with associative arrays, rather than a for loop.

Anyway, why are you using associative arrays? ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
this script is very lengthy....

anyway, my output would looks like
msg|msg|msg|msg|........

right?
each of the is msg[1].msg[2].msg[3]..... right?
how can i return the number
so i know where i am?
like when i click on the msg[1], it return to msg[1]
i can bypass the number in the url
?
ff
 
this script is very lengthy....

anyway, my output would looks like
msg|msg|msg|msg|........

right?
each of the is msg[1].msg[2].msg[3]..... right?
how can i return the number
so i know where i am?
like when i click on the msg[1], it returns [1]
i can bypass the number in the url?
ff
 
hmmm i find another way around seems to fix this problem
hehe
any good sugestions?

ff
 
The data you're trying to output is so regular, I wouldn't even bother with an array.

Just loop through a for loop, printing the strings you have up to the second equals sign, then the number separately, then the rest of the string. The only part that changes is the number. ______________________________________________________________________
Never forget that we are
made of the stuff of stars
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top