Jump to content

Recommended Posts

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

[code]
<?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());?>
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/16058-form-processing-help/
Share on other sites

nevermind I think I solved it by doing

[code]
<?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());
?>
[/code]

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
Link to comment
https://forums.phpfreaks.com/topic/16058-form-processing-help/#findComment-66176
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.