Page 1 of 2
Little Help on Arrays? :)
Posted: Tue Aug 27, 2002 6:59 pm
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!
Posted: Tue Aug 27, 2002 8:02 pm
by volka
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
?

Posted: Tue Aug 27, 2002 8:06 pm
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?
Posted: Tue Aug 27, 2002 8:09 pm
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?
Posted: Tue Aug 27, 2002 8:11 pm
by JPlush76
I do
Code: Select all
session_register("counted",$members);
Posted: Tue Aug 27, 2002 8:14 pm
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
Posted: Tue Aug 27, 2002 8:15 pm
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
Posted: Tue Aug 27, 2002 8:17 pm
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?
Posted: Tue Aug 27, 2002 8:23 pm
by volka
??
sorry, didn't get the point.
please clarify for a tired german who needs a dictionary when reading an english passport

Posted: Tue Aug 27, 2002 8:26 pm
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
?
Posted: Tue Aug 27, 2002 8:31 pm
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.
Posted: Tue Aug 27, 2002 8:37 pm
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"))
{
$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
doesn't seem to like:
Code: Select all
$members = $_SESSIONї'members'];
Posted: Tue Aug 27, 2002 8:43 pm
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ї'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.
2. edit: no you shouldn't

Posted: Tue Aug 27, 2002 9:12 pm
by JPlush76
I had:
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
Posted: Wed Aug 28, 2002 8:51 am
by volka
i tried
Code: Select all
<html><body><?php
function query_db($query)
{
$conn = mysql_connect("localhost", "dbuser", "dbpass");
mysql_select_db("testdb", $conn);
return mysql_query($query);
}
session_start();
if (isset($_GETї'id']))
{
$user_id = $_GETї'id'];
print('<pre>session :'); print_r($_SESSION);print('<pre>');
if (!isset($_SESSIONї'members']))
{
print('init session');
$result = query_db("UPDATE user SET views=1 WHERE user_id = $user_id");
if ($result !== FALSE)
$_SESSIONї'members'] = array($user_id);
else
print('initial update failed');
}
else
{
print('update session');
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'], $user_id);
} else
print('update failed');
}
}
print('<pre> session:'); print_r($_SESSION); print('<pre>');
}
?><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?