php - POST method and arrays -
this first php project. have created website users can upload picture , view pictures of other users, 1 person @ time (similar old hotornot.com). code below works follows:
- i create array (called $allusers) containing members except user logged in ($user).
- i create array (called $usersiviewed) of members $user has either liked (stored in likeprofile table) or disliked (stored in dislikeprofile table). first column of likeprofile , dislikeprofile has name of users did liking/disliking, second column contains name of member liked/disliked.
i use array_diff strip out $usersiviewed $allusers. list of users $user can view (ie, people have not liked or disliked in past). problem when click button, updates likeprofile table name of next person in array (i.e., not person who's picture looking @ person who's picture appears next). additionally, if refresh current page, person who's profile appears on current page automatically gets 'liked' me. appreciate advice on this.
<?php // viewprofiles.php include_once("header.php"); echo $user.' logged in<br><br>'; echo <<<_end <form method="post" action="viewprofiles.php"><pre> <input type="submit" name ="choice" value="like" /> <input type="submit" name ="choice" value="next profile" /> </pre></form> _end; $allusers = array(); //create $allusers array, comprised of users except me $result = querymysql("select * members"); $num = mysql_num_rows($result); ($j = 0 ; $j < $num ; ++$j) { $row = mysql_fetch_row($result); if ($row[0] == $user) continue; $allusers[$j] = $row[0]; } //create $i_like_these_users array, comprised of users liked $result = querymysql("select * likeprofile user='$user'"); $num = mysql_num_rows($result); ($j = 0 ; $j < $num ; ++$j) { $row = mysql_fetch_row($result); $i_like_these_users[$j] = $row[1]; } //create $i_dislike_these_users array, comprised of users disliked $result = querymysql("select * dislikeprofile user='$user'"); $num = mysql_num_rows($result); ($j = 0 ; $j < $num ; ++$j) { $row = mysql_fetch_row($result); $i_dislike_these_users[$j] = $row[1]; } //create $usersiviewed array, comprised of users have either liked or disliked if (is_array($i_like_these_users) && is_array($i_dislike_these_users)) { $usersiviewed = array_merge($i_like_these_users,$i_dislike_these_users); } elseif(is_array($i_like_these_users)) { $usersiviewed = $i_like_these_users; } else { $usersiviewed = $i_dislike_these_users; } // removes array $allusers (i.e., profiles can view) $usersviewed (i.e., profiles have either liked/disliked) if (is_array($usersiviewed)) { $peopleicanview = array_diff($allusers, $usersiviewed); $peopleicanview = array_values($peopleicanview); // re-indexes array } else { $peopleicanview = $allusers; $peopleicanview = array_values($peopleicanview); // re-indexes array } $current_user_profile = $peopleicanview[0]; echo 'check out '.$current_user_profile.'s picture <br />'; if (file_exists("$current_user_profile.jpg")) {echo "<img src='$current_user_profile.jpg' align='left' />";} // if or dislike person, likeprofile or dislikeprofile table updated name , name of person liked or disliked if (isset($_post['choice']) && $_post['choice'] == 'like') { $ilike = $current_user_profile; $query = "insert likeprofile values" . "('$user', '$ilike')"; if (!querymysql($query)) echo "insert failed: $query<br />" . mysql_error() . "<br /><br />"; } if (isset($_post['choice']) && $_post['choice'] == 'next profile') { $idontlike = $current_user_profile; $query = "insert dislikeprofile values" . "('$user', '$idontlike')"; if (!querymysql($query)) echo "insert failed: $query<br />" . mysql_error() . "<br /><br />"; } ?>
because when refresh page sends previus value of form again...and problem when u user being liked next user.. there there in yor loop while fetching row ...insted of loop try once while loop ...i hope solve ur problem
Comments
Post a Comment