Little Help on Arrays? :)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Little Help on Arrays? :)

Post by JPlush76 »

I have member profiles on my site and I created a new field in the user table called "VIEWS". What I want to do is check to see if there is a session registered... if not then update the views for that user based on their user_id and update an array with that user_id in it

If it is registered I want to check that array for the user_id if its in there don't update the "VIEWS" field if its not in the array, update the VIEWS field

basically during one session you can update each record once during your session.

Code: Select all

session_start(); 

 
// SEE IF THE USER HAS VIEWED THIS MEMBER IN THE SESSION, IF NOT UPDATE THE MEMBERS VIEWS
$members = array(); 

if (!session_is_registered("counted"))
{ 
	$user_id = $_GETї'id'];
	
	$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id");
}


//CHECK HERE TO SEE IF THE USER_ID IS IN THE ARRAY
if (session_is_registered("counted")){ 
	$user_id = $_GETї'id'];
	
		if(!in_array($user_id, $members))
		{

$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id");
			array_push($members, $user_id);
			
		}
		
}
whats happening now is its updating everytime I hit refresh and its only adding the most recent user_id to the array and not saving whats already been added to it :(

any help? thanks!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

$members = array();
unconditional, so at every script-start (?) an empty array.

Code: Select all

if (!session_is_registered("counted"))
where is "counted" registered?
if the magic is in query_db(), please post the function's code

Code: Select all

if (session_is_registered("counted"))
why not simply replace it by

Code: Select all

else
? ;)
Last edited by volka on Tue Aug 27, 2002 8:07 pm, edited 1 time in total.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

ah sorry volka, I register it right under that last bracket

so basically I keep clearing out that array everytime I call the page eh?

should I test for a value first?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you register $member, too?
or where do you keep this information?

if $member has been registered it may be that you have to access it via $_SESSION['member'] depending on your php-version and settings

I don't understand the difference between the two if-bodies. In the first you update a database-record but you don't add it to the $member-array. Is that right?
Last edited by volka on Tue Aug 27, 2002 8:12 pm, edited 1 time in total.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I do

Code: Select all

session_register("counted",$members);
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

so maybe...

Code: Select all

session_start(); 


// SEE IF THE USER HAS VIEWED THIS MEMBER IN THE SESSION, IF NOT UPDATE THE MEMBERS VIEWS 

if (!session_is_registered("counted")) 
{ 
   $user_id = $_GETї'id']; 
    
   $result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
   $members = array($user_id); 

} 


//CHECK HERE TO SEE IF THE USER_ID IS IN THE ARRAY 
if (session_is_registered("counted")){ 
   $user_id = $_GETї'id']; 
    
      if(!in_array($user_id, $members)) 
      { 

$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
         array_push($members, $user_id); 
          
      } 
       
}
I moved the
$members = array();

to within that if statement
Last edited by JPlush76 on Tue Aug 27, 2002 8:15 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmmm...I should think twice before pressing the submit-button ;)

you register a variable called "counted" and a variable called $member

session_register("counted", $member) does not create a session-var called "counted" with the value of $member
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I just assume when I register the session is registers two separate values

$counted and $members and $members hold the array throughout the session?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

??
sorry, didn't get the point.
please clarify for a tired german who needs a dictionary when reading an english passport ;)
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

damn germans :) lol

when I register "counter" and $members

I realize they are two seperate values

I thought when I register it that $members will hold the array contents until the session ends

?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, i c - you want both variables registered
you're right, there are two variables and $member does contain the array's value throughout the session.

Do you need "counted" or is it simply a flag (1:1 related to $member) ?

Be aware that when register_globals is Off you can't access sessions-var-values as global vars. Use instead $_SESSION['<var name>']. But this depends on your php version.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I changed it to :

Code: Select all

session_start(); 

// SEE IF THE USER HAS VIEWED THIS MEMBER IN THE SESSION, IF NOT UPDATE THE MEMBERS VIEWS 

if (!session_is_registered("counted")) 
&#123; 
   $user_id = $_GET&#1111;'id']; 
    
   $result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
   $members = array($user_id); 
   
   session_register("counted",$members); 

&#125; 


//CHECK HERE TO SEE IF THE USER_ID IS IN THE ARRAY 
if (session_is_registered("counted"))&#123; 
   $user_id = $_GET&#1111;'id']; 
   $members = $_SESSION&#1111;'members']; 
	
      if(!in_array($user_id, $members)) 
      &#123; 

$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
         array_push($members, $user_id); 
          
      &#125; 
        
&#125;
and I'm getting the errors:

Warning: Undefined index: members in c:\inetpub\wwwroot\dd\st_det.php on line 26

Warning: Wrong datatype for second argument in call to in_array in c:\inetpub\wwwroot\dd\st_det.php on line 28

Warning: First argument to array_push() needs to be an array in c:\inetpub\wwwroot\dd\st_det.php on line 32

doesn't seem to like:

Code: Select all

$members = $_SESSION&#1111;'members'];
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Warning: Wrong datatype for second argument in call to in_array in c:\inetpub\wwwroot\dd\st_det.php on line 28
Warning: First argument to array_push() needs to be an array in c:\inetpub\wwwroot\dd\st_det.php on line 32
me <- blind
you swapped the parameters of array_push: first p. is the array, second (and following) the values you want to append.

if you don't need counted for something else try

Code: Select all

<?php
session_start(); 
$user_id = $_GET&#1111;'id']; 
if (!isset($_SESSION&#1111;'members'])) 
&#123; 
   $result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id");
   // if ($result !== FALSE)
   $_SESSION&#1111;'members'] = array($user_id);
   // else
   // print('initial update failed');
&#125; 
else
&#123;
  if(!in_array($user_id, $_SESSION&#1111;'members'])) 
  &#123; 
  	// if ($result !== FALSE) &#123;
		$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
    array_push($_SESSION&#1111;'members'], $members); 
    // &#125; else 
    // print('update failed');
  &#125; 
&#125;
?>
-----^--- sorry, not even tested by compiler (I screw my apache - grrrrrrrr - tomorrow I'll have a little conversation with my PC about who is master and who is servant - to be honest I fear the answer :D )

edit: in the body of "if (!isset($_SESSION['members']))" shouldn't you set views to 1 instead of (views+1). There may be an old value stored.

2. edit: no you shouldn't :D
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

I had:

Code: Select all

array_push($members, $user_id);
doesn't that take the array $members and then add $user_id to the end of that table?

I copied your code and it updates the view field everytime I hit refresh :(

<-- runs and cries

arrays are evil lol
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

i tried

Code: Select all

<html><body><?php
function query_db($query)
&#123;
	$conn = mysql_connect("localhost", "dbuser", "dbpass");
	mysql_select_db("testdb", $conn);
	return mysql_query($query);
&#125;

session_start();
if (isset($_GET&#1111;'id']))
&#123;
	$user_id = $_GET&#1111;'id'];
	print('<pre>session :'); print_r($_SESSION);print('<pre>');
	if (!isset($_SESSION&#1111;'members'])) 
	&#123; 
		 print('init session');
	   $result = query_db("UPDATE user SET views=1 WHERE user_id = $user_id");
	   if ($result !== FALSE)
	   	$_SESSION&#1111;'members'] = array($user_id);
	   else
	   	print('initial update failed');
	&#125; 
	else
	&#123;
		print('update session');
	  if(!in_array($user_id, $_SESSION&#1111;'members'])) 
	  &#123; 
	  	if ($result !== FALSE) &#123;
				$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id"); 
	    	array_push($_SESSION&#1111;'members'], $user_id); 
	    &#125; else 
	     print('update failed');
	  &#125; 
	&#125;
	print('<pre> session:'); print_r($_SESSION); print('<pre>');
&#125;
?><hr>
<form method="GET">
	<select name="id">
		<option>1</option>
		<option>2</option>
		<option>3</option>
		<option>4</option>
		<option>5</option>
		<option>6</option>
		<option>7</option>
	</select>
	<input type="submit"/>
</form>
</body></html>
on my new apache and it worked as it should.
Is a PHP-session identifier appended to the link of the form's action?
Post Reply