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!
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.
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
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.
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.
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.
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);
session_register("counted",$members);
}
//CHECK HERE TO SEE IF THE USER_ID IS IN THE ARRAY
if (session_is_registered("counted")){
$user_id = $_GETї'id'];
$members = $_SESSIONї'members'];
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);
}
}
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
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.
<?php
session_start();
$user_id = $_GETї'id'];
if (!isset($_SESSIONї'members']))
{
$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id");
// if ($result !== FALSE)
$_SESSIONї'members'] = array($user_id);
// else
// print('initial update failed');
}
else
{
if(!in_array($user_id, $_SESSIONї'members']))
{
// if ($result !== FALSE) {
$result = query_db("UPDATE user SET views=(views + 1) WHERE user_id = $user_id");
array_push($_SESSIONї'members'], $members);
// } else
// print('update failed');
}
}
?>
-----^--- 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 )
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.