Jump to content


Photo

form processing help


  • Please log in to reply
1 reply to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 31 July 2006 - 12:17 AM

hey guys,

I'm trying to figure out how I can process a form that has several elements but might not all be "filled" for instance a user can select to search by sexe and/or age and/or location

How can I make the query dynamic?

so far I have this

<?php
foreach($_POST as $k => $v)
switch ($k) {
         case 'sexe':
               if (trim(stripslashes($v)) != '')
                   $qtmp[] = "gender ='" . mysql_real_escape_string(trim(stripslashes(implode ("','", $v)))) . "'";
                break;
                        
               }
$query = "SELECT * FROM " . $prefix . "users WHERE " . implode(', ', $qtmp) . "";
$_SESSION['custom_browse'] = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());?>

so how can I make it so that if other fields are filled it will add "AND" and the conditions that are required? And if the radio button Both is checked, then it doesn't need to add WHERE gender=...

oh and I'm also getting this notice and was wondering if it's a bad thing or not Notice: Array to string conversion

#2 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 31 July 2006 - 01:09 AM

nevermind I think I solved it by doing

<?php
$qtmp = array();
foreach($_POST as $k => $v)
           switch ($k) {
                    case 'sexe':
                        	$v = implode ("','", $v);
		if ($v != 'both') {
                            $qtmp[] = "WHERE gender ='" . mysql_real_escape_string(trim(stripslashes($v))) . "' AND ";
		} else {
		     $qtmp[] = "WHERE ";
		     }
                            break;
                        
         }
$query = "SELECT * FROM " . $prefix . "users " . implode(', ', $qtmp) . "level='2' ORDER by ID";
$_SESSION['custom_browse'] = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>

now my only problem is to keep the radio button selected... so if I select Male then after I submit the form, Male is still checked.. I think I know how to do it but it might be messy




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users