weisshagen Posted December 9, 2008 Share Posted December 9, 2008 Hello, I have a website with a MySQL database behind it and I made a search form, a results.php page and a details.php page. One of the fields in a table from the database is "products" where it is suppose to be a big amount of information (text). When a user is searching for a company for example, a table with 10 records per page is displayed on the results.php and from there, the user can go to each company's details by clicking on its name. Now, being so many products for each company, I would like to know how can I display on the results.php, for each company, only the product the user is being searched for, IF that product is already in the database, of course. Also, is there a way to have a "read more..." link on the same products field? Do I need to post some code here? Please let me know. I am not proficient in PHP and MySQL, just started to learn so a little detailed help would be greatly appreciated. Thank you very much for your help. Sieg. Link to comment https://forums.phpfreaks.com/topic/136189-database-search-certain-results/ Share on other sites More sharing options...
genericnumber1 Posted December 9, 2008 Share Posted December 9, 2008 Post what you have, what you've tried, we're all very hesitant to code you something from scratch, and I especially am when my IDE isn't installed. Link to comment https://forums.phpfreaks.com/topic/136189-database-search-certain-results/#findComment-710374 Share on other sites More sharing options...
redarrow Posted December 9, 2008 Share Posted December 9, 2008 try this... don't need the session's but i was using them for fun. <?php session_start(); if($_GET['read']=="more"){ $_SESSION['txt']="my name is redarrow and i love php"; echo $_SESSION['txt']; }else{ $_SESSION['txt']="my name is redarrow and i love php"; $limit=substr($_SESSION['txt'],0,19); echo $limit; } echo "<br><br> <a href='".$_SERVER['PHP_SELF']."?read=more '>Read More</a> <br><br>"; ?> better way <?php session_start(); $_SESSION['txt']="my name is redarrow and i love php"; if($_GET['read']=="more"){ echo $_SESSION['txt']; }else{ $limit=substr($_SESSION['txt'],0,19); echo $limit; } echo "<br><br> <a href='".$_SERVER['PHP_SELF']."?read=more '>Read More</a> <br><br>"; ?> Link to comment https://forums.phpfreaks.com/topic/136189-database-search-certain-results/#findComment-710392 Share on other sites More sharing options...
weisshagen Posted December 9, 2008 Author Share Posted December 9, 2008 Ok, so here goes: <!-- Starts the connection code on each page --> <?php require_once('Connections/bcom.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $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_bcom, $bcom); $query_Recordset1 = "SELECT * FROM main_table"; $Recordset1 = mysql_query($query_Recordset1, $bcom) or die(mysql_error()); $row_Recordset1 = mysql_fetch_assoc($Recordset1); $totalRows_Recordset1 = mysql_num_rows($Recordset1); ?> <!-- End of the connection code on each page --> Now, the search form itself: <!--Form starts here--> <form action="results.php" method="get" name="form1" id="form1"> <table> <tr valign="baseline"> <th nowrap="nowrap" align="right"> </th> <td><span class="style4"><strong>Area:</strong></span><br /> <select name="area2" class="fieldback"> <option value=""></option> <?php $colValueArray = array(); do { $colvalue = $row_Recordset1['area]; $index =0; $colValueFound = false; if ($colvalue != null ) { if ($colvalue == "") $colvalue = " " + $colvalue; for ($index=0; $index < count($colValueArray); $index++) { if (trim(strtolower($colValueArray[$index])) == trim(strtolower($colvalue))) { $colValueFound = true; break; } } } else { $colValueFound = true; } if ($colValueFound == false) { ?> <option value="<?php echo $row_Recordset1['area']?>" <?php if (!(strcmp($row_Recordset1['area'], $row_Recordset1['area]))) {echo "SELECTED";} ?>><?php echo $colvalue; ?></option> <?php array_push ($colValueArray, $colvalue); } } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); mysql_data_seek($Recordset1, 0); ?> </select> </td> </tr> <tr valign="baseline"> <th nowrap="nowrap" align="right"> </th> <td><span class="style6">Field:</span><br /> <select name="field2" class="fieldback"> <option value=""></option> <?php $colValueArray = array(); do { $colvalue = $row_Recordset1['field']; $index =0; $colValueFound = false; if ($colvalue != null ) { if ($colvalue == "") $colvalue = " " + $colvalue; for ($index=0; $index < count($colValueArray); $index++) { if (trim(strtolower($colValueArray[$index])) == trim(strtolower($colvalue))) { $colValueFound = true; break; } } } else { $colValueFound = true; } if ($colValueFound == false) { ?> <option value="<?php echo $row_Recordset1['field']?>" <?php if (!(strcmp($row_Recordset1['field'], $row_Recordset1['field']))) {echo "SELECTED";} ?>><?php echo $colvalue; ?></option> <?php array_push ($colValueArray, $colvalue); } } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); mysql_data_seek($Recordset1, 0); ?> </select> </td> </tr> <tr valign="baseline"> <th nowrap="nowrap" align="right"> </th> <td><span class="style4"><strong>Company:</strong></span><br /> <input name="company" type="text" class="fieldback" value="" size="28" /> </td> </tr> <tr valign="baseline"> <th nowrap="nowrap" align="right"> </th> <td><span class="style4"><strong>Adress:</strong></span><br /> <input name="adress" type="text" class="fieldback" value="" size="28" /> </td> </tr> <tr valign="baseline"> <th nowrap="nowrap" align="right"> </th> <td><span class="style4"><strong>Products:</strong></span><br /> <input name="products" type="text" class="fieldback" value="" size="28" /> </td> </tr> <tr valign="baseline"> <td nowrap="nowrap" align="right"> </td> <td> <input type="hidden" name="AdvSearch" value="AdvSearch" /> <input type="submit" value="Search" /> </td> </tr> </table> <br /> </form> <!--Form ends here--> Now, the results page: <!-- Results code starts here --> <?php require_once('Connections/bcom.php'); ?><?php $currentPage = $_SERVER["PHP_SELF"]; if (isset($HTTP_GET_VARS['AdvSearch']) == false || $HTTP_GET_VARS['AdvSearch']=="") exit(); $hasSQLWhereClause=false; $whereClause=""; $MM_whereConst=" "; $fieldValue = $HTTP_GET_VARS['area2']; if(isset($HTTP_GET_VARS['zona2']) && $HTTP_GET_VARS['area2'] != "") { if($hasSQLWhereClause == false) { $hasSQLWhereClause=true; $whereClause=$whereClause." rtrim(ltrim(upper(area))) = rtrim(ltrim(upper('$fieldValue')))"; } else { $whereClause=$whereClause." and rtrim(ltrim(upper(area))) = rtrim(ltrim(upper('$fieldValue')))"; } } $fieldValue = $HTTP_GET_VARS['field2']; if(isset($HTTP_GET_VARS['field2']) && $HTTP_GET_VARS['field2'] != "") { if($hasSQLWhereClause == false) { $hasSQLWhereClause=true; $whereClause=$whereClause." rtrim(ltrim(upper(field))) = rtrim(ltrim(upper('$fieldValue')))"; } else { $whereClause=$whereClause." and rtrim(ltrim(upper(field))) = rtrim(ltrim(upper('$fieldValue')))"; } } $fieldValue = $HTTP_GET_VARS['company']; if(isset($HTTP_GET_VARS['company']) && $HTTP_GET_VARS['company']!= "") { if($hasSQLWhereClause == false) { $hasSQLWhereClause=true; $whereClause=$whereClause." rtrim(ltrim(upper(company))) like rtrim(ltrim(upper('%$fieldValue%')))"; } else { $whereClause=$whereClause." and rtrim(ltrim(upper(company))) like rtrim(ltrim(upper('%$fieldValue%')))"; } } $fieldValue = $HTTP_GET_VARS['adress']; if(isset($HTTP_GET_VARS['adress']) && $HTTP_GET_VARS['adress']!= "") { if($hasSQLWhereClause == false) { $hasSQLWhereClause=true; $whereClause=$whereClause." rtrim(ltrim(upper(adress))) like rtrim(ltrim(upper('%$fieldValue%')))"; } else { $whereClause=$whereClause." and rtrim(ltrim(upper(adress))) like rtrim(ltrim(upper('%$fieldValue%')))"; } } $fieldValue = $HTTP_GET_VARS['products']; if(isset($HTTP_GET_VARS['products']) && $HTTP_GET_VARS['products']!= "") { if($hasSQLWhereClause == false) { $hasSQLWhereClause=true; $whereClause=$whereClause." rtrim(ltrim(upper(products))) like rtrim(ltrim(upper('%$fieldValue%')))"; } else { $whereClause=$whereClause." and rtrim(ltrim(upper(products))) like rtrim(ltrim(upper('%$fieldValue%')))"; } } if($whereClause != "") $MM_whereConst="WHERE"; $maxRows_RecordSet1 = 10; $pageNum_RecordSet1 = 0; if (isset($HTTP_GET_VARS['pageNum_RecordSet1'])) { $pageNum_RecordSet1 = $HTTP_GET_VARS['pageNum_RecordSet1']; } $startRow_RecordSet1 = $pageNum_RecordSet1 * $maxRows_RecordSet1; mysql_select_db($database_bcom, $bcom); $query_RecordSet1 = "SELECT * FROM main_table $MM_whereConst $whereClause order by area desc"; $query_limit_RecordSet1 = sprintf("%s LIMIT %d, %d", $query_RecordSet1, $startRow_RecordSet1, $maxRows_RecordSet1); $RecordSet1 = mysql_query($query_limit_RecordSet1, $bcom) or die(mysql_error()); $row_RecordSet1 = mysql_fetch_assoc($RecordSet1); if (isset($HTTP_GET_VARS['totalRows_RecordSet1'])) { $totalRows_RecordSet1 = $HTTP_GET_VARS['totalRows_RecordSet1']; } else { $all_RecordSet1 = mysql_query($query_RecordSet1); $totalRows_RecordSet1 = mysql_num_rows($all_RecordSet1); } $totalPages_RecordSet1 = ceil($totalRows_RecordSet1/$maxRows_RecordSet1)-1; $queryString_RecordSet1 = ""; if (!empty($_SERVER['QUERY_STRING'])) { $params = explode("&", $_SERVER['QUERY_STRING']); $newParams = array(); foreach ($params as $param) { if (stristr($param, "pageNum_RecordSet1") == false && stristr($param, "totalRows_RecordSet1") == false) { array_push($newParams, $param); } } if (count($newParams) != 0) { $queryString_RecordSet1 = "&" . htmlentities(implode("&", $newParams)); } } $queryString_RecordSet1 = sprintf("&totalRows_RecordSet1=%d%s", $totalRows_RecordSet1, $queryString_RecordSet1); ?> <!-- Results code ends here --> Now the results table itself: <!-- Results table starts here --> <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="10%" align="center"><strong>Area</strong></td> <td width="10%" align="center"><strong>Field</strong></td> <td width="17%" align="center"><strong>Company</strong></td> <td width="18%" align="center"><strong>Adress</strong></td> <td width="20%" align="center"><strong>Products</strong></td> <td width="6%"align="center"><strong>Coupon</strong></td> </tr> <?php do { ?> <tr> <td width="10%" align="center" valign="top"><?php echo $row_RecordSet1['area']; ?> </td> <td width="10%" align="center" valign="top"><?php echo $row_RecordSet1['field']; ?> </td> <td align="center" valign="top"><a href="details.php?AdvSearch=<?php echo urlencode($row_RecordSet1['ID']); ?>"><?php echo $row_RecordSet1['company']; ?> </a> </td> <td align="center" valign="top"><?php echo $row_RecordSet1['adress']; ?> </td> <td align="center" valign="top"><div align="left"><?php echo $row_RecordSet1['products']; ?> </div></td> <td width="6%" align="center" valign="top"><?php echo $row_RecordSet1['coupon']; ?> </td> </tr> <?php } while ($row_RecordSet1 = mysql_fetch_assoc($RecordSet1)); ?> </table> <br /> <br /> <br /> <br /> <table border="0"> <tr> <td><?php if ($pageNum_RecordSet1 > 0) { // Show if not first page ?> <a href="<?php printf("%s?pageNum_RecordSet1=%d%s", $currentPage, 0, $queryString_RecordSet1); ?>">First</a> <?php } // Show if not first page ?> </td> <td><?php if ($pageNum_RecordSet1 > 0) { // Show if not first page ?> <a href="<?php printf("%s?pageNum_RecordSet1=%d%s", $currentPage, max(0, $pageNum_RecordSet1 - 1), $queryString_RecordSet1); ?>">Previous</a> <?php } // Show if not first page ?> </td> <td><?php if ($pageNum_RecordSet1 < $totalPages_RecordSet1) { // Show if not last page ?> <a href="<?php printf("%s?pageNum_RecordSet1=%d%s", $currentPage, min($totalPages_RecordSet1, $pageNum_RecordSet1 + 1), $queryString_RecordSet1); ?>">Next</a> <?php } // Show if not last page ?> </td> <td><?php if ($pageNum_RecordSet1 < $totalPages_RecordSet1) { // Show if not last page ?> <a href="<?php printf("%s?pageNum_RecordSet1=%d%s", $currentPage, $totalPages_RecordSet1, $queryString_RecordSet1); ?>">Last</a> <?php } // Show if not last page ?> </td> </tr> </table> <!-- Results table ends here --> And finally, the details page: <!-- Details code starts here --> <?php require_once('Connections/bcom.php'); ?><?php $maxRows_RecordSet1 = 10; $pageNum_RecordSet1 = 0; if (isset($HTTP_GET_VARS['pageNum_RecordSet1'])) { $pageNum_RecordSet1 = $HTTP_GET_VARS['pageNum_RecordSet1']; } $startRow_RecordSet1 = $pageNum_RecordSet1 * $maxRows_RecordSet1; $AdvSearch = $HTTP_GET_VARS['AdvSearch']; mysql_select_db($database_bcom, $bcom); $query_RecordSet1 = "SELECT * FROM main_table WHERE ID = $AdvSearch"; $query_limit_RecordSet1 = sprintf("%s LIMIT %d, %d", $query_RecordSet1, $startRow_RecordSet1, $maxRows_RecordSet1); $RecordSet1 = mysql_query($query_limit_RecordSet1, $bcom) or die(mysql_error()); $row_RecordSet1 = mysql_fetch_assoc($RecordSet1); if (isset($HTTP_GET_VARS['totalRows_RecordSet1'])) { $totalRows_RecordSet1 = $HTTP_GET_VARS['totalRows_RecordSet1']; } else { $all_RecordSet1 = mysql_query($query_RecordSet1); $totalRows_RecordSet1 = mysql_num_rows($all_RecordSet1); } $totalPages_RecordSet1 = ceil($totalRows_RecordSet1/$maxRows_RecordSet1)-1; ?> <!-- Details code ends here --> And the details table itself: <!-- Details table starts here --> <table width="99%" align="center" cellpadding="2" cellspacing="2" style="border: 1px solid rgb(153,0,0)"> <tr> <td width="14%" bgcolor="#CCCCCC"><div align="left"><strong>Area</strong></div></td> <td width="86%"><div align="left"><?php echo $row_RecordSet1['area']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Field</strong></div></td> <td><div align="left"><?php echo $row_RecordSet1['field']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Company</strong></div></td> <td bgcolor="#CCCCCC"><div align="left"><?php echo $row_RecordSet1['company']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Company's details</strong></div></td> <td bgcolor="#CCCCCC"><div align="left"><?php echo $row_RecordSet1['company_details']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Address</strong></div></td> <td><div align="left"><?php echo $row_RecordSet1['adress']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Address details</strong></div></td> <td><div align="left"><?php echo $row_RecordSet1['address_details']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Products</strong></div></td> <td bgcolor="#CCCCCC"><div align="left"><?php echo $row_RecordSet1['products']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Products' details</strong></div></td> <td bgcolor="#CCCCCC"><div align="center"><?php echo $row_RecordSet1['products_details']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Hiring</strong></div></td> <td><div align="left"><?php echo $row_RecordSet1['hiring']; ?> </div></td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="left"><strong>Coupon</strong></div></td> <td><div align="left"><?php echo $row_RecordSet1['coupon']; ?> </div></td> </tr> </table> <!-- Details table ends here --> Sorry for the awfull long post... Sieg. Link to comment https://forums.phpfreaks.com/topic/136189-database-search-certain-results/#findComment-710393 Share on other sites More sharing options...
weisshagen Posted December 10, 2008 Author Share Posted December 10, 2008 Thank you very much for your solution. Unfortunately, it didn't work, maybe because I don't know how to correctly integrate your code in my pages. A little bit more help would be extremely useful, considering that I am still "eating" the PHP book every time I want to accomplish something. Thank you again. Sieg. try this... don't need the session's but i was using them for fun. <?php session_start(); if($_GET['read']=="more"){ $_SESSION['txt']="my name is redarrow and i love php"; echo $_SESSION['txt']; }else{ $_SESSION['txt']="my name is redarrow and i love php"; $limit=substr($_SESSION['txt'],0,19); echo $limit; } echo "<br><br> <a href='".$_SERVER['PHP_SELF']."?read=more '>Read More</a> <br><br>"; ?> better way <?php session_start(); $_SESSION['txt']="my name is redarrow and i love php"; if($_GET['read']=="more"){ echo $_SESSION['txt']; }else{ $limit=substr($_SESSION['txt'],0,19); echo $limit; } echo "<br><br> <a href='".$_SERVER['PHP_SELF']."?read=more '>Read More</a> <br><br>"; ?> Link to comment https://forums.phpfreaks.com/topic/136189-database-search-certain-results/#findComment-711220 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.