Accurax Posted March 13, 2007 Share Posted March 13, 2007 I have a form that allows a user to search for another user by sex, location and age (at least it should), i can get the age bit to work. The user can select a lower age, and an upper age, and the php that follows should select all users from the database who fall between these ages, as well as fitting the other two criterea. after the form is submitted i assign variables to the POST values; <?php $sex = $_POST['search_sex']; $loc = $_POST['search_location']; $min_age = $_POST['search_age2']; $max_age = $_POST['search_age1']; ?> I then query my database to pull all member information, and convert the stored dates of birth into ages and assign it to a variable; <?php $username = $_SESSION['username']; $query = "SELECT * FROM members"; $result = mysql_query($query) or die ("could not find name"); $row = mysql_fetch_array($result); //calculates the age $day = $row['day']; $month = $row['month']; $year = $row['year']; $birthday = $day."-".$month."-".$year; $today = date('d-m-Y'); $a_birthday = explode('-', $birthday); $a_today = explode('-', $today); $day_birthday = $a_birthday[0]; $month_birthday = $a_birthday[1]; $year_birthday = $a_birthday[2]; $day_today = $a_today[0]; $month_today = $a_today[1]; $year_today = $a_today[2]; $age = $year_today - $year_birthday; if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday)) { $age--; } ?> then i query the database again to only pull records matching my crietera; <?php $user_query = "SELECT * FROM members m INNER JOIN pictures p ON p.user_name = m.user_name WHERE sex='$sex' AND location='$loc' AND $min_age<=$age=>$max_age ORDER BY last_updated DESC". " LIMIT $offset, $rowsPerPage"; $result = mysql_query($user_query) or die ("no can do"); ?> I kind of know what the problem is ........ im trying to assign all the ages of my users to a single variable ..... but that cant be done, therefore i assume i need to use an array .... but im unsure as to how to go about this. I'd really appreciate someone taking the time to show me how to approach this problem. **(yes i know using 3 fields in my database for dob is odd but that is not the issue here) Link to comment https://forums.phpfreaks.com/topic/42481-can-someone-take-a-look-at-this-please/ Share on other sites More sharing options...
dymon Posted March 13, 2007 Share Posted March 13, 2007 Hi, I have made some modifications to your code, have a look at it. If you have any questions ask them. After the code is executed you will get an array $members, matching your criteria. <?php $username = $_SESSION['username']; $query = "SELECT * FROM members m INNER JOIN pictures p ON p.user_name = m.user_name WHERE sex='$sex' AND location='$loc' ORDER BY last_updated DESC"." LIMIT $offset, $rowsPerPage"; $result = mysql_query($query) or die ("could not find name"); $today = date('d-m-Y'); $a_today = explode('-', $today); $day_today = $a_today[0]; $month_today = $a_today[1]; $year_today = $a_today[2]; while ($row = mysql_fetch_array($result)) { //$row = mysql_fetch_array($result); //calculates the age $day = $row['day']; $month = $row['month']; $year = $row['year']; $birthday = $day."-".$month."-".$year; $a_birthday = explode('-', $birthday); $day_birthday = $a_birthday[0]; $month_birthday = $a_birthday[1]; $year_birthday = $a_birthday[2]; $age = $year_today - $year_birthday; if (($month_today < $month_birthday) || ($month_today == $month_birthday && $day_today < $day_birthday)) { $age--; } if (($age >=$min_age) && ($age <= $max_age)) $members [] = $row; } ?> The best, Dymon Link to comment https://forums.phpfreaks.com/topic/42481-can-someone-take-a-look-at-this-please/#findComment-206114 Share on other sites More sharing options...
Accurax Posted March 13, 2007 Author Share Posted March 13, 2007 your going to have to forgive me.... my understanding of arrays is not quite up to scratch yet. Could you just explain how that array would be structured .... ? would it be an associative array? ... i assum a foreach loop would be needed to display the results, am i right? Link to comment https://forums.phpfreaks.com/topic/42481-can-someone-take-a-look-at-this-please/#findComment-206126 Share on other sites More sharing options...
dymon Posted March 13, 2007 Share Posted March 13, 2007 i assum a foreach loop would be needed to display the results, am i right? Yes, you are right. If you want to see the structure is to use the following function (http://www.php.net/manual/en/function.print-r.php): print_r ($row) - to show the structure of the array that comes from the database print_r ($members) - to show the struct. of the array members and to use it: foreach ($members as $id => $values) { //Where: $id - the id from array, and $values - the array with the members info taken from database. print $values['user_name']; } I provided a small example. Don't hesitate to ask if you have other questions. Dymon Link to comment https://forums.phpfreaks.com/topic/42481-can-someone-take-a-look-at-this-please/#findComment-206131 Share on other sites More sharing options...
Accurax Posted March 13, 2007 Author Share Posted March 13, 2007 foreach ($members as $id => $values) { //Where: $id - the id from array, and $values - the array with the members info taken from database. print $values['user_name']; } $id is the key... and $values are the stored values correct? I think im going to go and find myself an arrays tutorial, because its quite confusing at the moment. Link to comment https://forums.phpfreaks.com/topic/42481-can-someone-take-a-look-at-this-please/#findComment-206135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.