Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Complex selection criteria not working

New Here ,
Sep 12, 2010 Sep 12, 2010

Copy link to clipboard

Copied

I have a submit form that allows people to enter stud dogs into a database and then search for stud dogs with criteria selected from a form.  It is complex because there are many different criteria that can be selected for searching (or not). In most cases, the selection criteria is either selected or not, but I have one case complicating the select statement even further.  A dog is a specific color, so a given dog record will have only one value for the field 'color'.  But a person can indicate selection criteria that includes up to 7 different color values (or none).

I found this lovely bit of code under another thread and I've attempted to use the longer version of tit to make my select statement work. This is the code that I've got now:

$query_rs_results = "SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry, semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main";

// Set a flag to indicate whether the query has a WHERE clause

// Set a variable to indicate if the select statement has a color value selected
$where = false;
$color = 0;

if (isset($_GET['arm']) && !empty($_GET['arm'])) {
  $query_rs_results .= ' WHERE arm = '. GetSQLValueString($_GET['arm'], 'text');
  $where = true;
}
if (isset($_GET['bite']) && !empty($_GET['bite'])) {
  if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'bite = ' . GetSQLValueString($_GET['bite'], 'text');
}
if (isset($_GET['body']) && !empty($_GET['body'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'body1 = ' . GetSQLValueString($_GET['body'], 'text');
}
if (isset($_GET['breadth']) && !empty($_GET['breadth'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'breadth = ' . GetSQLValueString($_GET['breadth'], 'text');
}
if (isset($_GET['brisket']) && !empty($_GET['brisket'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'brisket = ' . GetSQLValueString($_GET['brisket'], 'text');
}
if (isset($_GET['dentition']) && !empty($_GET['dentition'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'dentition = ' . GetSQLValueString($_GET['dentition'], 'text');
}
if (isset($_GET['color_b']) && !empty($_GET['color_b'])) {
$color = $color + 1;
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
  }
  $query_rs_results .= '(color = ' . GetSQLValueString($_GET['color_b'], 'text');
}
if (isset($_GET['color_bl']) && !empty($_GET['color_bl'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_bl'], 'text');
}
if (isset($_GET['color_br']) && !empty($_GET['color_br'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_br'], 'text');
}
if (isset($_GET['color_f']) && !empty($_GET['color_f'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_f'], 'text');
}
if (isset($_GET['color_h']) && !empty($_GET['color_h'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_h'], 'text');
}
if (isset($_GET['color_m']) && !empty($_GET['color_m'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_m'], 'text');
}
if (isset($_GET['color_o']) && !empty($_GET['color_o'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND ';
            } else {
               $query_rs_results .= ' WHERE ';
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_GET['color_o'], 'text');
}
if ($color > 0) {
$query_rs_results .= ') ';
}
if (isset($_GET['ears']) && !empty($_GET['ears'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'ears = ' . GetSQLValueString($_GET['ears'], 'text');
}
if (isset($_GET['eye_color']) && !empty($_GET['eye_color'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_color = ' . GetSQLValueString($_GET['eye_color'], 'text');
}
if (isset($_GET['eye_shape']) && !empty($_GET['eye_shape'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_shape = ' . GetSQLValueString($_GET['eye_shape'], 'text');
}
if (isset($_GET['eye_tight']) && !empty($_GET['eye_tight'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_tight = ' . GetSQLValueString($_GET['eye_tight'], 'text');
}
if (isset($_GET['f_feet']) && !empty($_GET['f_feet'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'f_feet = ' . GetSQLValueString($_GET['f_feet'], 'text');
}
if (isset($_GET['head']) && !empty($_GET['head'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'head = ' . GetSQLValueString($_GET['head'], 'text');
}
if (isset($_GET['hocks']) && !empty($_GET['hocks'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'hocks = ' . GetSQLValueString($_GET['hocks'], 'text');
}
if (isset($_GET['lip']) && !empty($_GET['lip'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'lip = ' . GetSQLValueString($_GET['lip'], 'text');
}
if (isset($_GET['longevity']) && !empty($_GET['longevity'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'longevity = ' . GetSQLValueString($_GET['longevity'], 'text');
}
if (isset($_GET['neck_length']) && !empty($_GET['neck_length'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'neck_length = ' . GetSQLValueString($_GET['neck_length'], 'text');
}
if (isset($_GET['neck_set']) && !empty($_GET['neck_set'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'neck_set = ' . GetSQLValueString($_GET['neck_set'], 'text');
}
if (isset($_GET['r_angulation']) && !empty($_GET['r_angulation'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'r_angulation = ' . GetSQLValueString($_GET['r_angulation'], 'text');
}
if (isset($_GET['r_feet']) && !empty($_GET['r_feet'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'r_feet = ' . GetSQLValueString($_GET['r_feet'], 'text');
}
if (isset($_GET['semen_f']) && !empty($_GET['semen_f'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_f = ' . GetSQLValueString($_GET['semen_f'], 'text');
}
if (isset($_GET['semen_fc']) && !empty($_GET['semen_fc'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_fc = ' . GetSQLValueString($_GET['semen_fc'], 'text');
}
if (isset($_GET['semen_fr']) && !empty($_GET['semen_fr'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_fr = ' . GetSQLValueString($_GET['semen_fr'], 'text');
}
if (isset($_GET['semen_l']) && !empty($_GET['semen_l'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_l = ' . GetSQLValueString($_GET['semen_l'], 'text');
}
if (isset($_GET['shoulders']) && !empty($_GET['shoulders'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'shoulders = ' . GetSQLValueString($_GET['shoulders'], 'text');
}
if (isset($_GET['tail_length']) && !empty($_GET['tail_length'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'tail_length = ' . GetSQLValueString($_GET['tail_length'], 'text');
}
if (isset($_GET['tail_set']) && !empty($_GET['tail_set'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'tail_set = ' . GetSQLValueString($_GET['tail_set'], 'text');
}
if (isset($_GET['temperament']) && !empty($_GET['temperament'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'temperament = ' . GetSQLValueString($_GET['temperament'], 'text');
}
if (isset($_GET['topline']) && !empty($_GET['topline'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'topline = ' . GetSQLValueString($_GET['topline'], 'text');
}
if (isset($_GET['withers']) && !empty($_GET['withers'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'withers = ' . GetSQLValueString($_GET['withers'], 'text');
}
echo "Query search = ".$query_rs_results;
$query_limit_rs_results = sprintf("%s LIMIT %d, %d", $query_rs_results, $startRow_rs_results, $maxRows_rs_results);
$rs_results = mysql_query($query_limit_rs_results, $dqdb) or die(mysql_error());
$row_rs_results = mysql_fetch_assoc($rs_results);

if (isset($_POST['totalRows_rs_results'])) {
  $totalRows_rs_results = $_POST['totalRows_rs_results'];
} else {
  $all_rs_results = mysql_query($query_rs_results);
  $totalRows_rs_results = mysql_num_rows($all_rs_results);
}
$totalPages_rs_results = ceil($totalRows_rs_results/$maxRows_rs_results)-1;

I'm echoing $query_rs_results to verify that it is building the select statement correctly, but it is not.  The select statement being echoed is:

SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry, semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main

The results page is displaying every dog in the database.

What am I missing here?

TIA,

Laura

TOPICS
Server side applications

Views

2.0K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2010 Sep 12, 2010

Copy link to clipboard

Copied

I figured out what I was doing wrong with the form values.  But I'm still trying to figure out how to get that mutiple

color selection thing working.  I'll post again if I figure it out, but any help is appreciated!

Laura

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2010 Sep 12, 2010

Copy link to clipboard

Copied

Ta Da!  It works!  Here's the code:

$query_rs_results = "SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry, semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main";

// Set a flag to indicate whether the query has a WHERE clause
// Set an indicator to check if there is more than 1 color being selected
$where = false;
$color = 0;

// Check form data for criteria selected

if (isset($_POST['s_arm']) && !empty($_POST['s_arm'])) {
  $query_rs_results .= ' WHERE arm = '. GetSQLValueString($_POST['s_arm'], 'text');
  $where = true;
}
if (isset($_POST['s_bite']) && !empty($_POST['s_bite'])) {
  if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'bite = ' . GetSQLValueString($_POST['s_bite'], 'text');
}
if (isset($_POST['s_body']) && !empty($_POST['s_body'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'body1 = ' . GetSQLValueString($_POST['s_body'], 'text');
}
if (isset($_POST['s_breadth']) && !empty($_POST['s_breadth'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'breadth = ' . GetSQLValueString($_POST['s_breadth'], 'text');
}
if (isset($_POST['s_brisket']) && !empty($_POST['s_brisket'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'brisket = ' . GetSQLValueString($_POST['s_brisket'], 'text');
}
if (isset($_POST['s_dentition']) && !empty($_POST['s_dentition'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'dentition = ' . GetSQLValueString($_POST['s_dentition'], 'text');
}
if (isset($_POST['s_color_b']) && !empty($_POST['s_color_b'])) {
$color = $color + 1;
    if ($where) {
    $query_rs_results .= ' AND (';
} else {
    $query_rs_results .= ' WHERE (';
$where = true;
  }
  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_b'], 'text');
}
if (isset($_POST['s_color_bl']) && !empty($_POST['s_color_bl'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
      $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_bl'], 'text');
}
if (isset($_POST['s_color_br']) && !empty($_POST['s_color_br'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
      $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_br'], 'text');
}
if (isset($_POST['s_color_f']) && !empty($_POST['s_color_f'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
      $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_f'], 'text');
}
if (isset($_POST['s_color_h']) && !empty($_POST['s_color_h'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
         $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_h'], 'text');
}
if (isset($_POST['s_color_m']) && !empty($_POST['s_color_m'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
      $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_m'], 'text');
}
if (isset($_POST['s_color_o']) && !empty($_POST['s_color_o'])) {
$color = $color + 1;
If ($color > 1) {
  $query_rs_results .= ' OR ';
     } else {
            if ($where) {
               $query_rs_results .= ' AND (';
            } else {
               $query_rs_results .= ' WHERE (';
      $where = true;
   }
          }

  $query_rs_results .= 'color = ' . GetSQLValueString($_POST['s_color_o'], 'text');
}
if ($color > 0) {
$query_rs_results .= ') ';
}
if (isset($_POST['s_ears']) && !empty($_POST['s_ears'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'ears = ' . GetSQLValueString($_POST['s_ears'], 'text');
}
if (isset($_POST['s_eye_color']) && !empty($_POST['s_eye_color'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_color = ' . GetSQLValueString($_POST['s_eye_color'], 'text');
}
if (isset($_POST['s_eye_shape']) && !empty($_POST['s_eye_shape'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_shape = ' . GetSQLValueString($_POST['s_eye_shape'], 'text');
}
if (isset($_POST['s_eye_tight']) && !empty($_POST['s_eye_tight'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'eye_tight = ' . GetSQLValueString($_POST['s_eye_tight'], 'text');
}
if (isset($_POST['s_f_feet']) && !empty($_POST['s_f_feet'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'f_feet = ' . GetSQLValueString($_POST['s_f_feet'], 'text');
}
if (isset($_POST['s_head']) && !empty($_POST['s_head'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'head = ' . GetSQLValueString($_POST['s_head'], 'text');
}
if (isset($_POST['s_hocks']) && !empty($_POST['s_hocks'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'hocks = ' . GetSQLValueString($_POST['s_hocks'], 'text');
}
if (isset($_POST['s_lip']) && !empty($_POST['s_lip'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'lip = ' . GetSQLValueString($_POST['s_lip'], 'text');
}
if (isset($_POST['s_longevity']) && !empty($_POST['s_longevity'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'longevity = ' . GetSQLValueString($_POST['s_longevity'], 'text');
}
if (isset($_POST['s_neck_length']) && !empty($_POST['s_neck_length'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'neck_length = ' . GetSQLValueString($_POST['s_neck_length'], 'text');
}
if (isset($_POST['s_neck_set']) && !empty($_POST['s_neck_set'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'neck_set = ' . GetSQLValueString($_POST['s_neck_set'], 'text');
}
if (isset($_POST['s_r_angulation']) && !empty($_POST['s_r_angulation'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'r_angulation = ' . GetSQLValueString($_POST['s_r_angulation'], 'text');
}
if (isset($_POST['s_r_feet']) && !empty($_POST['s_r_feet'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'r_feet = ' . GetSQLValueString($_POST['s_r_feet'], 'text');
}
if (isset($_POST['s_semen_f']) && !empty($_POST['s_semen_f'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_f = ' . GetSQLValueString($_POST['s_semen_f'], 'text');
}
if (isset($_POST['s_semen_fc']) && !empty($_POST['s_semen_fc'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_fc = ' . GetSQLValueString($_POST['s_semen_fc'], 'text');
}
if (isset($_POST['s_semen_fr']) && !empty($_POST['s_semen_fr'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_fr = ' . GetSQLValueString($_POST['s_semen_fr'], 'text');
}
if (isset($_POST['s_semen_l']) && !empty($_POST['s_semen_l'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'semen_l = ' . GetSQLValueString($_POST['s_semen_l'], 'text');
}
if (isset($_POST['s_shoulders']) && !empty($_POST['s_shoulders'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'shoulders = ' . GetSQLValueString($_POST['s_shoulders'], 'text');
}
if (isset($_POST['s_tail_length']) && !empty($_POST['s_tail_length'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'tail_length = ' . GetSQLValueString($_POST['s_tail_length'], 'text');
}
if (isset($_POST['s_tail_set']) && !empty($_POST['s_tail_set'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'tail_set = ' . GetSQLValueString($_POST['s_tail_set'], 'text');
}
if (isset($_POST['s_temperament']) && !empty($_POST['s_temperament'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'temperament = ' . GetSQLValueString($_POST['s_temperament'], 'text');
}
if (isset($_POST['s_topline']) && !empty($_POST['s_topline'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'topline = ' . GetSQLValueString($_POST['s_topline'], 'text');
}
if (isset($_POST['s_withers']) && !empty($_POST['s_withers'])) {
    if ($where) {
    $query_rs_results .= ' AND ';
  } else {
    $query_rs_results .= ' WHERE ';
    $where = true;
  }
  $query_rs_results .= 'withers = ' . GetSQLValueString($_POST['s_withers'], 'text');
}

$query_limit_rs_results = sprintf("%s LIMIT %d, %d", $query_rs_results, $startRow_rs_results, $maxRows_rs_results);
$rs_results = mysql_query($query_limit_rs_results, $dqdb) or die(mysql_error());
$row_rs_results = mysql_fetch_assoc($rs_results);

if (isset($_POST['totalRows_rs_results'])) {
  $totalRows_rs_results = $_POST['totalRows_rs_results'];
} else {
  $all_rs_results = mysql_query($query_rs_results);
  $totalRows_rs_results = mysql_num_rows($all_rs_results);
}
$totalPages_rs_results = ceil($totalRows_rs_results/$maxRows_rs_results)-1;
?>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 12, 2010 Sep 12, 2010

Copy link to clipboard

Copied

The code is still much more complex than it needs to be. For multiple selections I would suggest using PHP to extract all of the values, wrap them in quotes and then seperate with commas. Then use can use the SQL IN predicate. That will reduce about 70 lines of your code to about 5.

Next, you are testing if WHERE has been set for every field, then conditional branching. This adds 5 lines of unecessary code for each field. Just include the where statement initially and set it to a true value (Where 1=1). Then you know that if the field needs to be added to the where clause you just need to use AND.

Or even better, just loop through all of the field values to build your where clause. You could probably reduce your code above to about 20 lines. This will make it much easier to read and maintain.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 12, 2010 Sep 12, 2010

Copy link to clipboard

Copied

I'm sure there are more elegant ways of doing this, but I'm pretty thrilled to have it working all the same.   This is my very first Web app.

Thanks for your input!

Laura

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2010 Sep 26, 2010

Copy link to clipboard

Copied

Well it seems the time has come for me to improve this code. I want to put page navigation in the results page and am having some problems there (see other thread of mine).

The reason I'm testing the "where" is because while there are over 30 distinct criteria that can be selected on the form.  The user can select any one of them or several of them.  And, the user can also not select any of them, which would result in retrieving every record in the database.  I only want to have a where clause when some selection has been made.  I'm not familiar with the sQL In predicate or how to do the php coding you suggest using quotes.  Can you point me to some examples?

Thanks!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 26, 2010 Sep 26, 2010

Copy link to clipboard

Copied

I've looked at the SQL IN predicate and I see how I could use that to look for color IN "....", but I'm still not clear on how to do this without interrogating each form variable to see if it is set or not and if it is, include it in the select criteria.  I'm sure there are cleaner ways to do this than the verbose method I've done and that might lend itself much better to trying to get my page navigation on the results page working, but I'm pretty lost at this point.

If anyone can point me to some code examples I might study, it would be much appreciated!

Thanks!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2010 Sep 27, 2010

Copy link to clipboard

Copied

>but I'm still not clear on how to do this without

>interrogating each  form variable to see if it is set

>or not and if it is, include it in the  select criteria

I don't do PHP so I can't give you any  specific code, but I guarantee that you can do this with a few lines of  code that will loop through the color selections and push those into a comma separated string which can then be used with the IN predicate. I'm sure someone here with a decent working knowledge of PHP can help you.


Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 27, 2010 Sep 27, 2010

Copy link to clipboard

Copied

>I only want to have a where clause

>when some selection has been made.

Which is why I said to set it to a value which always evaluates as true. Example:

"Select * from MyTable where 1=1"

This returns all rows from the table. Then you can append additional criteria if the user entered data:

"and color = blue"

This way, you never have to worry about testing whether or not to include the Where clause because it is already there.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 28, 2010 Sep 28, 2010

Copy link to clipboard

Copied

Thanks!  This is so much common sense. 

I still have the other larger issue where the selection criteria requires interrogating the form variables to see if they are set or not, and the one set of checkboxes that can have multiple values set requiring me to build an "or" in the where clause potentially with several "and" conditions.  This code is working fine as it is, but now that I'm getting more data in my db, I want to provide page navigation through the results set.

Incorporating the page navigation in the results set has introduced an even larger issue that I've posted in another discussion thread.  When the next page is selected, the code re-executes the mysql select, but the form variables have been reset and the second page executes a select that retrieves every record in the db. I'm not clear how to prevent resetting the form variables so that my results are consistent when paging through the results set.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Sep 29, 2010 Sep 29, 2010

Copy link to clipboard

Copied

One way to solve the pagination issue is to store the search criteria in a session after the form is submitted. You could add a hidden field to your form that you can use to determine if this is a new submission, or if you need to grab the previous submission from the session. For instance, if you added a hidden field named "formStatus" with a value of "submitted" to your search form, you'd determine the data source by something like:

$formStatus = (isset($_POST['formStatus'])) ? $_POST['formStatus'] : ''; if ($formStatus == 'submitted') {      $dataSource = $_POST;      $_SESSION['search_criteria'] = serialize($_POST); } else {      $dataSource = (isset($_SESSION['search_criteria']))           ? unserialize($_SESSION['search_criteria'])           : array(); } if (!is_array($dataSource)) $dataSource = array();

So, if a $_POST field named formStatus has a value of submitted, set the data source to be the $_POST array and store the current $_POST array to a session variable for use on the next page. If no hidden field was detected, grab any search conditions from the session and use those instead. In the rest of your script, you'd use $dataSource instead of $_POST to fetch the form (or session) values.

Looking over the rest of your script, you can condense things quite a bit and make it easier to manage by using loops. An example script is below for how it could be re-written (keep in mind, this is just quickly typed and not tested at all - if you decide to use it and get errors, post back if you need help in finding them). Basically, looks like your database field names are the same as your form field names, minus the 's_' prefix (except for the body field which seems to be named body1 in the database, so that one is handled differently).

It also shows how you can streamline the multiple color selections using an IN statement in the SQL. Personally, I like to store the query conditions in an array and then parse them into a string using implode afterwards. Just yell if you have questions on what any of this means.

// start a session to store search criteria session_start(); // list of basic field names (normal handling) $basicFields = array(      'arm', 'bite', 'breadth', 'brisket', 'dentition', 'ears',      'eye_color', 'eye_shape', 'eye_tight', 'f_feet', 'head',      'hocks', 'lip', 'longevity', 'neck_length', 'neck_set',      'r_angulation', 'semen_f', 'semen_fc', 'semen_fr', 'semen_l',      'shoulders', 'tail_length', 'tail_set', 'temperament',      'topline', 'withers' ); // list of color field names (multiple conditons) $colorFields = array(      'color_b', 'color_bl', 'color_br', 'color_f',      'color_h', 'color_m', 'color_o' ); // fields where db field name does not match form field name $mismatchFields = array('body'); // determine if the data is coming from a form or the stored session $formStatus = (isset($_POST['formStatus'])) ? $_POST['formStatus'] : ''; if ($formStatus == 'submitted') {      $dataSource = $_POST;      $_SESSION['search_criteria'] = serialize($_POST); } else {      $dataSource = (isset($_SESSION['search_criteria']))           ? unserialize($_SESSION['search_criteria'])           : array(); } if (!is_array($dataSource)) $dataSource = array(); // fetch and prepare form values - add s_ prefix to match names $formData = array(); $allFields = array_merge($basicFields, $colorFields, $mismatchFields); foreach ($allFields as $f) {      if (isset($dataSource['s_'.$f]))           $formData[$f] = GetSQLValueString($dataSource['s_'.$f], 'text');      else $formData[$f] = ''; } // build array to hold query conditionals $where = array(); // add basic fields to query conditions foreach ($basicFields as $f) {      if (!empty($formData[$f])) $where[] = $f.'='.$formData[$f]; } // handle mutli-option color fields $in = array(); foreach ($colorFields as $f) {      if (!empty($formData[$f])) $in[] = $formData[$f]; } if (count($in) > 0) $where[] = 'color IN ('.implode(', ', $in).')'; // handle mis-matched fields individually if (!empty($formData['body'])) $where[] = "body1=".$formData['body']; // build SQL using conditionals if (count($where) > 0)      $whereSQL = 'WHERE '.implode(' AND ', $where); else $whereSQL = ''; $query_rs_results = "      SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod,      reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,      semen_l, semen_f, semen_fc, semen_fr, photo      FROM dog_main      {$whereSQL}      "; $query_limit_rs_results = sprintf(      "%s LIMIT %d, %d", $query_rs_results,      $startRow_rs_results, $maxRows_rs_results ); $rs_results = mysql_query($query_limit_rs_results, $dqdb) or die(mysql_error()); $row_rs_results = mysql_fetch_assoc($rs_results); if (isset($_POST['totalRows_rs_results'])) {   $totalRows_rs_results = $_POST['totalRows_rs_results']; } else {   $all_rs_results = mysql_query($query_rs_results);   $totalRows_rs_results = mysql_num_rows($all_rs_results); } $totalPages_rs_results = ceil($totalRows_rs_results/$maxRows_rs_results)-1;

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 29, 2010 Sep 29, 2010

Copy link to clipboard

Copied

Thanks so much for your rich response!  The body1 is a typo - fixed it in the online version, but forgot to fix it in the test version of the code.

I have looked through this briefly and get the jist of it.  I will digest this over the weekend and see what progress I can make.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 02, 2010 Oct 02, 2010

Copy link to clipboard

Copied

This is sweet!  I have modified the code as you suggested and it is much shorter, but more difficult for my novice brain to comprehend.

I'm having a bit of trouble with the results.

If I select color = fawn and search, I expect 2 pages of dogs with 10 on the first page.  I've got echo statements in there to keep track of variables.  Here are their values when the first page displays:

formStatus = submitted
submitted search criteria for session = a:4:{s:9:"s_color_f";s:1:"f";s:6:"button";s:6:"Search";s:10:"formStatus";s:9:"submitted";s:8:"redirect";s:14:"danequest.html";}
where = Array
query_rs_results = SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main WHERE color IN ('f')
query_limit_rs_results = SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main WHERE color IN ('f') LIMIT 0, 10
row_rs_results = Array
totalPages_rs_results = 2

So the code you gave me to shorten building the where clause is working very nicely!

When I click "next" to go to page 2, I have once again lost my where condition and all records in the db are retrieved.  Here are the values in the variables:

formStatus =
else search criteria for session =
where =
query_rs_results = SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main
query_limit_rs_results = SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod, reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,semen_l, semen_f, semen_fc, semen_fr, photo FROM dog_main LIMIT 0, 10
row_rs_results = Array
totalPages_rs_results = 5

This is the same thing that was happening before.  All form variables are reset on the second select statement and I've lost my criteria and all dogs in the db are selected.  It looks like it is resetting the session variables too.

What is also happening is that, no matter what my selection critieria is, the first page has the correct dogs displayed.  But, instead of starting with the 11th dog in the selected records on the second page, the first dog in the db is at the top of the page and every page there after starts with the first dog in the db  and shows the same 10 dogs.  So it is not setting the start row for the next page correctly.

I need to let this soak into my brain a bit.  After it marinates for a while I'll figure out what to try next and post again.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 02, 2010 Oct 02, 2010

Copy link to clipboard

Copied

Can you post a link to the live page? Or, paste in all the code you're currently using, PHP and HTML? I don't mind helping troubleshoot, but hard to guess what's going on with the details.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 02, 2010 Oct 02, 2010

Copy link to clipboard

Copied

The live page is http://www.daneaffaire.com/danwquest.html.  If you click on the search button, change the url to append "_test" to reach the test results page that is showing this behavior.

Here is the code:

<?php require_once('connections/dqdb.php'); ?>
<?php


if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;   
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_dqdb, $dqdb);

// check to see if this is a new form submitted or we need to use the session variable

$formStatus = (isset($_POST['formStatus'])) ? $_POST['formStatus'] : '';
if ($formStatus == 'submitted') {
$dataSource = $_POST;
$_SESSION['search_criteria'] = serialize($_POST);
} else {
  $dataSource = (isset($_SESSION['search_criteria']))
  ? unserialize($_SESSION['search_criteria']) : array();
}
if (!is_array($dataSource)) $dataSource = array();

// start a session to store search criteria session_start();

// list of basic field names (normal handling)

$basicFields = array('arm', 'bite', 'body', 'breadth', 'brisket', 'dentition', 'ears', 'eye_color', 'eye_shape', 'eye_tight', 'f_feet', 'head', 'hocks', 'lip', 'longevity', 'neck_length', 'neck_set', 'r_angulation', 'semen_f', 'semen_fc', 'semen_fr', 'semen_l', 'shoulders', 'tail_length', 'tail_set', 'temperament', 'topline', 'withers');

// list of color field names (multiple conditons)

$colorFields = array('color_b', 'color_bl', 'color_br', 'color_f', 'color_h', 'color_m', 'color_o');


// determine if the data is coming from a form or the stored session

$formStatus = (isset($_POST['formStatus'])) ? $_POST['formStatus'] : '';
echo "formStatus = ".$formStatus."<br />";
if ($formStatus == 'submitted') {
$dataSource = $_POST;
$_SESSION['search_criteria'] = serialize($_POST);
echo "submitted search criteria for session = ".$_SESSION['search_criteria']."<br />";
} else {
echo "else search criteria for session = ".$_SESSION['search_criteria']."<br />";
$dataSource = (isset($_SESSION['search_criteria']))
          ? unserialize($_SESSION['search_criteria'])
          : array();
}
if (!is_array($dataSource)) $dataSource = array();

// fetch and prepare form values - add s_ prefix to match names
$formData = array();
$allFields = array_merge($basicFields, $colorFields);
foreach ($allFields as $f) {
if (isset($dataSource['s_'.$f]))
$formData[$f] = GetSQLValueString($dataSource['s_'.$f], 'text');
else $formData[$f] = ''; }

// build array to hold query conditionals $where = array();

// add basic fields to query conditions

foreach ($basicFields as $f)
{
     if (!empty($formData[$f])) $where[] = $f.'='.$formData[$f]; }

// handle mutli-option color fields

$in = array();
foreach ($colorFields as $f)
{
     if (!empty($formData[$f])) $in[] = $formData[$f]; }
  if (count($in) > 0) $where[] = 'color IN ('.implode(', ', $in).')';

// build SQL using conditionals

$maxRows_rs_results = 10;
echo "where = ".$where."<br />";

if (count($where) > 0)
     $whereSQL = 'WHERE '.implode(' AND ', $where);
  else
  $whereSQL = '';
  $query_rs_results = "SELECT dogid, b_email, b_fname, b_lname, b_phone, b_url, dob, dod,      reg_name, reg_no, o_email, o_fname, o_lname, o_phone, o_url, registry,semen_l, semen_f, semen_fc, semen_fr, photo     
  FROM dog_main
  {$whereSQL}      ";
  echo "query_rs_results = ".$query_rs_results."<br />";
  $query_limit_rs_results = sprintf(      "%s LIMIT %d, %d", $query_rs_results,    $startRow_rs_results, $maxRows_rs_results );
  echo "query_limit_rs_results = ".$query_limit_rs_results."<br />";
  $rs_results = mysql_query($query_limit_rs_results, $dqdb) or die(mysql_error());
  $row_rs_results = mysql_fetch_assoc($rs_results);
  echo "row_rs_results = ".$row_rs_results."<br />";
  if (isset($_POST['totalRows_rs_results'])) {
   $totalRows_rs_results = $_POST['totalRows_rs_results'];
   } else {
    $all_rs_results = mysql_query($query_rs_results);
    $totalRows_rs_results = mysql_num_rows($all_rs_results);
     }
 
   
$totalPages_rs_results = ceil($totalRows_rs_results/$maxRows_rs_results)-1;
echo "totalPages_rs_results = ".$totalPages_rs_results."<br />";

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Dane Quest Search Results</title>
<link href="dq_style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
body,td,th {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
body {
background-color: #c6cacd;
}
-->
</style>
</head>

<body>
<h1 align="center"><img src="./images/studfinder_logo.jpg" width="212" height="219" alt="Dane Quest Logo" /></h1>
<div align="center">
  <table width="800" border="5" bordercolor="#333333" cellspacing="0" cellpadding="10">
    <tr class="table_border">
      <th scope="col"><a href="danequest.html">Home</a></th>
      <th scope="col"><a href="dq_about.html">About</a></th>
      <th scope="col"><a href="dq_search.html">Search</a></th>
      <th scope="col"><a href="dq_submit.html">Submit</a></th>
      <th scope="col"><a href="dq_resources.html">Resources</a></th>
      <th scope="col"><a href="mailto:studinfo@daneaffaire.com">Contact</a></th>
    </tr>
  </table>
</div>
</br>
<div align="center">
  <?php do  {  ?>
    <table width="850px" border="5" align="center" cellpadding="5" cellspacing="0" summary="Contains idenfication information about the dog, his breeder and owner">
      <tr>
        <td colspan="4" align="center" class="search_results_data"><img src = "./uploads/<?php echo $row_rs_results['photo']; ?>" width = "450"  /></td>
      </tr>
      <tr>
        <td colspan="4" rowsp align="center" class="search_results_data"><p align="center"><h2><?php echo $row_rs_results['reg_name']; ?></h2>          </p></td>
      </tr>
      <tr>
        <td><div align="left">
          <div align="left">Registry: <?php echo $row_rs_results['registry']; ?></div></td>
        <td><div align="left">
          <div align="left">Reg No: <?php echo $row_rs_results['reg_no']; ?></div></td>
        <td><div align="left">
          <div align="left">DOB: <?php echo $row_rs_results['dob']; ?></div></td>
        <td><div align="left">
          <div align="left">DOD: <?php echo $row_rs_results['dod']; ?></div></td>
      </tr>
      <tr>
        <td><div align="left">Live Cover: <?php echo $row_rs_results['semen_l']; ?></div></td>
        <td><div align="left">Fresh Semen: <?php echo $row_rs_results['semen_f']; ?></div></td>
        <td><div align="left">Fresh Chilled: <?php echo $row_rs_results['semen_fc']; ?></div></td>
        <td><div align="left">Frozen: <?php echo $row_rs_results['semen_fr']; ?></div>       
      </tr>
      <tr>
        <td colspan="2" align="center" class="search_results_data"><h3 align="center">Breeder</h3></td>
        <td colspan="2" align="center" class="search_results_data"><h3 align="center">Owner</h3></td>
      </tr>
      <tr class="search_results_data">
        <td colspan="2"><div align="left">Name :<?php echo $row_rs_results['b_fname']; ?> <?php echo $row_rs_results['b_lname']; ?></div></td>
        <td colspan="2"><div align="left">Name: <?php echo $row_rs_results['o_fname']; ?> <?php echo $row_rs_results['o_lname']; ?></div></td>
      </tr>
      <tr class="search_results_data">
        <td colspan="2"><div align="left">Phone: <?php echo $row_rs_results['b_phone']; ?></div></td>
        <td colspan="2"><div align="left">Phone: <?php echo $row_rs_results['o_phone']; ?></div></td>
      </tr>
      <tr class="search_results_data">
        <td colspan="2"><div align="left">E-mail: <?php echo $row_rs_results['b_email']; ?></div></td>
        <td colspan="2"><div align="left">E-mail: <?php echo $row_rs_results['o_email']; ?></div></td>
      </tr>
      <tr class="search_results_data">
        <td colspan="2"><div align="left">Webpage: <?php echo $row_rs_results['b_url']; ?></div></td>
        <td colspan="2"><div align="left">Webpage: <?php echo $row_rs_results['o_url']; ?></div></td>
      </tr>
    </table>
    <?php } while ($row_rs_results = mysql_fetch_assoc($rs_results))  ?>
<table border="0">
    <tr>
      <td><?php if ($pageNum_rs_results > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_rs_results=%d%s", $currentPage, 0, $queryString_rs_results); ?>">First Page</a>
          <?php } // Show if not first page ?></td>
      <td><?php if ($pageNum_rs_results > 0) { // Show if not first page ?>
          <a href="<?php printf("%s?pageNum_rs_results=%d%s", $currentPage, max(0, $pageNum_rs_results - 1), $queryString_rs_results); ?>">Previous Page</a>
          <?php } // Show if not first page ?></td>
      <td><?php if ($pageNum_rs_results < $totalPages_rs_results) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_rs_results=%d%s", $currentPage, min($totalPages_rs_results, $pageNum_rs_results + 1), $queryString_rs_results); ?>">Next Page</a>
          <?php } // Show if not last page ?></td>
      <td><?php if ($pageNum_rs_results < $totalPages_rs_results) { // Show if not last page ?>
          <a href="<?php printf("%s?pageNum_rs_results=%d%s", $currentPage, $totalPages_rs_results, $queryString_rs_results); ?>">Last Page</a>
          <?php } // Show if not last page ?></td>
    </tr>
  </table>
</div>
</body>
</html>
<?php
mysql_free_result($rs_results);
?>

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 02, 2010 Oct 02, 2010

Copy link to clipboard

Copied

Sorry - fat fingered that url - here it is: http://www.daneaffaire.com/dq_search_test.html - if you select some criteria and click on search, this will take you to the test results page.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

There might be more to it than this, but the first thing that jumps out at me is that you're not creating the session first with session_start() before checking for and storing the search parameters (first 2 lines in my full sample code). You need to start the session before using it, so in your copy it could be:

// create a session

session_start();

// check to see if this is a new form submitted or we need to use the session variable $formStatus = (isset($_POST['formStatus'])) ? $_POST['formStatus'] : ''; if ($formStatus == 'submitted') { $dataSource = $_POST; $_SESSION['search_criteria'] = serialize($_POST); } else {   $dataSource = (isset($_SESSION['search_criteria']))   ? unserialize($_SESSION['search_criteria']) : array(); } if (!is_array($dataSource)) $dataSource = array();

Looking it over again, you do have the session_start() code on your page, but it's after the block that needs it, and it's commented out by a couple // slashes. If you fix that, any better luck? If not, post back and I'll take a closer look.

http://us2.php.net/manual/en/function.session-start.php

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

Nate,

Yes, I did figure out that I needed the session_start.  Thanks again!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

Yep, looks like we posted a few minutes apart :-). Glad you got it working.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 07, 2010 Oct 07, 2010

Copy link to clipboard

Copied

Hey Nate,

Interesting things going on here this week.  I thought I had this issue fixed and then it reappeared.  It seemed to be intermittantly working, but I could not figure out what was different between times that it worked and times that it did not - in terms of moving through the site or the results set.

Then I was looking at it today and at the very bottom of the code, there I saw it -

<?php mysql_free_result($rs_results); ?>  DUH! 

I still don't know why it was appearing to work some of the time and not others, but removing this code seems to have fixed the issue finally.  At least it is working right now and I haven't been able to break it.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 11, 2010 Oct 11, 2010

Copy link to clipboard

Copied

LATEST

Hello Dane & Nate,

Thanks for the pieces of code examples to get complex selection queries working!

I have a question however. I'm a beginner with Dreamweaver.

My problem is: everytime a make modifications in the (generated) code of my application

red exclamation points appear in the server behaviour panel. (and I cannot get rid of them )

Is that a problem? What should I do about this behaviour? Or is this normal?

I cannot edit the server behavior nor can I create a new one.

My application is an application to search in a bibliography presenting a window with a list (grid) with records and a navigator. Users can use a form

with some fields in which they can enter selection criteria.

After submitting the form I build the (complex) selection criterium and push that into the recordset selector.

So I have to modify the generated recordset server behaviour (in the code).

Hope I'm clear.

Thanks,

LexvS

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 03, 2010 Oct 03, 2010

Copy link to clipboard

Copied

I've got it working.  I took the new & improved where clause and added a session_start() before the "submitted" test , then combined it with the pagination code I had originally and voila, it works. 

Thanks for all your generous assistance!

The next improvement will be to display the number of records retrieved with a default of 10 per page and allow the user to select the number of records per page to display. 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines