roldahayes Posted November 23, 2011 Share Posted November 23, 2011 Hi, I'l try and explain the problem as best I can.... I have a CSV file that populates a database. The database runs a search facility that displays "Make" and "Model" of cars and vans and then displays the products that are available for each. I am trying to add a new "category" for "Motorbikes" The pages are stripped down and live here. http://2012.gkmedia.co.uk/van_roof_racks.php http://2012.gkmedia.co.uk/car_roof_racks.php http://2012.gkmedia.co.uk/bike_racks.php The CSV has a "Default_Code" that denotes where the info is moved to i.e Cars or Vans. The default Code for Cars is "LE" and if that field is left out - the vehicles display in the Vans section... I have added a motorbike default code of "MB" to the CSV and edited the page as best I could which now displays the Motorbikes in their own set of results. The problem that I am having, is that no matter what I try, the Motorbikes are also showing in the Van search page.... I hope that I have explained this well enough for someone to be able to help! Cheers. van_roof_racks.php <?php include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } $sqlSelect = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code <> 'LE' and Car_Make <> 'n/a'"; $sqlquery = $sqlSelect; $result = mysql_query($sqlquery); if (!$result || (mysql_num_rows($result) == 0)){ } else{ $count = mysql_num_rows($result); } $totalOutput = ""; while ($row = mysql_fetch_assoc($result)) { $strMake = $row["Car_Make"]; $outMake = "regions[\"".$strMake."\"] = "; $sqlSelectModel = "SELECT DISTINCT Car_Model FROM products WHERE Car_Make = '".$strMake."' and Default_Code <> 'LE'"; $sqlqueryModel = $sqlSelectModel; $resultModel = mysql_query($sqlqueryModel); if (!$resultModel || (mysql_num_rows($resultModel) == 0)){ } else{ $countModel = mysql_num_rows($resultModel); } $i = $countModel; $outModel = "[\""; while ($rowModel = mysql_fetch_assoc($resultModel)) { $strModel = $rowModel["Car_Model"]; $outModel = $outModel.$strModel; if ($i > 1){ $outModel = $outModel."\", \""; } else{ $outModel = $outModel; } $i = $i-1; } $outModel = $outModel."\"];"; $carOutput = $outMake.$outModel; $totalOutput = $totalOutput.$carOutput."\n"; } ?> <script language="javascript"> regions = new Array(); <?php echo $totalOutput; ?> function update(form) { selectedRegion = form.make.options[form.make.selectedIndex].value; while (form.model.options.length > 1) form.model.options[1] = null; if (selectedRegion.length > 0) { current_array=regions[selectedRegion]; for (j=0;j<current_array.length;j++) { var optionName = new Option(current_array[j], current_array[j], false); form.model.options[form.model.length] = optionName; } } } </script> <html> <head> <title>Van Roof Racks</title> </head> <body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" bgcolor="#FFFFFF" link="#990000" vlink="#999966"> <div align="center"> <table border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td valign="top" background=""><table width="850" border="0" cellspacing="0" bordercolor="#666666" align="center" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="1"> <tr> <td bgcolor="#000000"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td bgcolor="#FFFFFF"> </td> </tr> <tr> <td bgcolor="#FFFFFF"><div align="center"> <table width="850" align="center" cellspacing="0"> <tr align="center"> <td height="82"><table width="850" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="80" align="center" valign="top"><form action="results.php"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="29" bgcolor="#FFFF99"><div align="center" class="arial11"><strong>VAN ROOF RACKS</strong></div></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr bgcolor="#FFFF99"> <td width="34%" class="arial11">.</td> <td width="9%"><?php echo "<select name=\"make\" onChange=\"update(this.form)\">\n<option>MAKE</option>\n"; // get all the distinct values for Process from the db $query = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code <> 'LE' and Car_Make <> 'n/a'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) foreach ($row as $data) echo("<option value=\"" . $data . "\">" . $data . "</option>\n"); echo "</select>"; ?> <div align="center"></div></td> <td width="57%"><div align="left"> <select name="model"> <option>MODEL </option> </select> </div></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> <td width="55%" bgcolor="#FFFF99"><div align="right"> <input name="submit3" type="submit" value="Click here to search." /> </div></td> <td width="45%" bgcolor="#FFFF99" class="arial11"> </td> </tr> </table> </form></td> </tr> </table> <div align="center"><br /> </div></td> </tr> </table> <br /> </div> </td> </tr> </table></td> </tr> </table></td> </tr> </table> <br /> <?php //close db mysql_close(); ?> <br /> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><div align="center"></div></td> </tr> </table> <br /></td> </tr> </table> <br /> </div> </body> </html> car_roof_racks.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="shortcut icon" href="favicon.ico" /> <title>Car Roof Racks</title> <?php include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } $sqlSelect = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code = 'LE' and Car_Make <> 'n/a'"; // assign the basic sqlquery $sqlquery = $sqlSelect; //echo $sqlquery; //get the result set $result = mysql_query($sqlquery); // if no matches found from query if (!$result || (mysql_num_rows($result) == 0)){ //echo ("<b><font class=small>No matches were found.</font></b>"); } // display the results else{ // use rowcount to display total results found $count = mysql_num_rows($result); //echo $count; //echo("<p>Total Matches Found = " . $rowCount . "</p>"); //*debug } //initalize make and model output string $totalOutput = ""; while ($row = mysql_fetch_assoc($result)) { // out put car make $strMake = $row["Car_Make"]; $outMake = "regions[\"".$strMake."\"] = "; //echo $outMake; //*******************loop through each model $sqlSelectModel = "SELECT DISTINCT Car_Model FROM products WHERE Car_Make = '".$strMake."' and Default_Code = 'LE'"; //echo "<br>".$sqlSelectModel; // assign the basic sqlquery $sqlqueryModel = $sqlSelectModel; //echo $sqlqueryModel; //get the result set $resultModel = mysql_query($sqlqueryModel); // if no matches found from query if (!$resultModel || (mysql_num_rows($resultModel) == 0)){ //echo ("<b><font class=small>No matches were found.</font></b>"); } // display the results else{ // use rowcount to display total results found $countModel = mysql_num_rows($resultModel); } $i = $countModel; $outModel = "[\""; while ($rowModel = mysql_fetch_assoc($resultModel)) { // out put car make $strModel = $rowModel["Car_Model"]; $outModel = $outModel.$strModel; //echo $strModel; if ($i > 1){ //echo $strModel.", "; $outModel = $outModel."\", \""; } else{ //echo $strModel; $outModel = $outModel; } $i = $i-1; //end model while loop } //write end of model string $outModel = $outModel."\"];"; //concatenate make and model strings $carOutput = $outMake.$outModel; //build up complete string of all makes and models $totalOutput = $totalOutput.$carOutput."\n"; //*************************finish looping thorugh each model //end make while } ?> <script language="javascript"> regions = new Array(); <?php echo $totalOutput; ?> function update(form) { selectedRegion = form.make.options[form.make.selectedIndex].value; // this bit resets the cities select list to nothing. while (form.model.options.length > 1) form.model.options[1] = null; // this bit populates the cities select list with the required cities. if (selectedRegion.length > 0) { current_array=regions[selectedRegion]; for (j=0;j<current_array.length;j++) { var optionName = new Option(current_array[j], current_array[j], false); form.model.options[form.model.length] = optionName; } } } </script> </head> <body> <table width="101%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><p> </p> <div id="leisure-wrapper"> <table width="850" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td><div align="center"> <p align="center" class="arial11"> </p> <table width="750" align="center" cellpadding="0" cellspacing="0"> <tr align="center"> <td height="82"><table width="850" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="80" align="center" valign="top"><form action="car_results.php"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr valign="middle" bgcolor="#000000"> <td colspan="3" class="arial11"><table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td width="15%"> </td> <td width="85%"><h1 align="center"><strong><font color="#FFFFFF">CAR ROOF BAR SEARCH</font></strong></h1></td> <td width="15%" bgcolor="#006699"> </td> </tr> </table></td> </tr> <tr bgcolor="#FFFF99"> <td colspan="3" class="arial11"> </td> </tr> <tr bgcolor="#FFFF99"> <td width="34%" class="arial11"> </td> <td width="16%"><strong> <?php echo "<select name=\"make\" onChange=\"update(this.form)\">\n<option>MAKE</option>\n"; // get all the distinct values for Process from the db $query = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code = 'LE' and Car_Make <> 'n/a'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) foreach ($row as $data) echo("<option value=\"" . $data . "\">" . $data . "</option>\n"); echo "</select>"; ?> </strong> <div align="center"></div></td> <td width="50%" bgcolor="#FFFF99"><div align="left"><strong> <select name="model"> <option>MODEL </option> </select> </strong></div></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr bgcolor="#FFFF99"> <td width="55%"><div align="right"> <strong> <input name="submit" type="submit" value="Click here to search." /> </strong></div></td> <td width="45%" class="arial11"> </td> </tr> </table> <strong><br /> </strong> </form></td> </tr> </table></td> </tr> </table> </div> <p class="arial11"> </p></td> </tr> </table> <div id="toTop"></div> </div> <p> </p> <div class="special" id="footer"> <div align="center"> <hr width="650" size="1" noshade="noshade" /> </div> <div align="center"></div> </div></td> </tr> </table> </body> </html> <?php // the following code should be placed at the very end of each page that contains a link. //--------------'CLOSE PAGE'-------------------------- //close db mysql_close(); //--------------'END CLOSE PAGE'-------------------------- ?> bike_racks.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="shortcut icon" href="favicon.ico" /> <title>Car Roof Racks</title> <?php include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } $sqlSelect = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code = 'MB' and Car_Make <> 'n/a'"; // assign the basic sqlquery $sqlquery = $sqlSelect; //echo $sqlquery; //get the result set $result = mysql_query($sqlquery); // if no matches found from query if (!$result || (mysql_num_rows($result) == 0)){ //echo ("<b><font class=small>No matches were found.</font></b>"); } // display the results else{ // use rowcount to display total results found $count = mysql_num_rows($result); //echo $count; //echo("<p>Total Matches Found = " . $rowCount . "</p>"); //*debug } //initalize make and model output string $totalOutput = ""; while ($row = mysql_fetch_assoc($result)) { // out put car make $strMake = $row["Car_Make"]; $outMake = "regions[\"".$strMake."\"] = "; //echo $outMake; //*******************loop through each model $sqlSelectModel = "SELECT DISTINCT Car_Model FROM products WHERE Car_Make = '".$strMake."' and Default_Code = 'MB'"; //echo "<br>".$sqlSelectModel; // assign the basic sqlquery $sqlqueryModel = $sqlSelectModel; //echo $sqlqueryModel; //get the result set $resultModel = mysql_query($sqlqueryModel); // if no matches found from query if (!$resultModel || (mysql_num_rows($resultModel) == 0)){ //echo ("<b><font class=small>No matches were found.</font></b>"); } // display the results else{ // use rowcount to display total results found $countModel = mysql_num_rows($resultModel); } $i = $countModel; $outModel = "[\""; while ($rowModel = mysql_fetch_assoc($resultModel)) { // out put car make $strModel = $rowModel["Car_Model"]; $outModel = $outModel.$strModel; //echo $strModel; if ($i > 1){ //echo $strModel.", "; $outModel = $outModel."\", \""; } else{ //echo $strModel; $outModel = $outModel; } $i = $i-1; //end model while loop } //write end of model string $outModel = $outModel."\"];"; //concatenate make and model strings $carOutput = $outMake.$outModel; //build up complete string of all makes and models $totalOutput = $totalOutput.$carOutput."\n"; //*************************finish looping thorugh each model //end make while } ?> <script language="javascript"> regions = new Array(); <?php echo $totalOutput; ?> function update(form) { selectedRegion = form.make.options[form.make.selectedIndex].value; // this bit resets the cities select list to nothing. while (form.model.options.length > 1) form.model.options[1] = null; // this bit populates the cities select list with the required cities. if (selectedRegion.length > 0) { current_array=regions[selectedRegion]; for (j=0;j<current_array.length;j++) { var optionName = new Option(current_array[j], current_array[j], false); form.model.options[form.model.length] = optionName; } } } </script> </head> <body> <table width="101%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><p> </p> <div id="leisure-wrapper"> <table width="850" border="0" cellspacing="0" cellpadding="0" align="center"> <tr> <td><div align="center"> <p align="center" class="arial11"> </p> <table width="750" align="center" cellpadding="0" cellspacing="0"> <tr align="center"> <td height="82"><table width="850" border="0" cellspacing="0" cellpadding="0"> <tr> <td height="80" align="center" valign="top"><form action="bike_results.php"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr valign="middle" bgcolor="#000000"> <td colspan="3" class="arial11"><table width="100%" border="0" cellspacing="0" cellpadding="10"> <tr> <td width="15%"> </td> <td width="85%"><h1 align="center"><strong><font color="#FFFFFF">MOTORBIKE SEARCH</font></strong></h1></td> <td width="15%" bgcolor="#006699"> </td> </tr> </table></td> </tr> <tr bgcolor="#FFFF99"> <td colspan="3" class="arial11"> </td> </tr> <tr bgcolor="#FFFF99"> <td width="34%" class="arial11"> </td> <td width="16%"><strong> <?php echo "<select name=\"make\" onChange=\"update(this.form)\">\n<option>MAKE</option>\n"; // get all the distinct values for Process from the db $query = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code = 'MB' and Car_Make <> 'n/a'"; $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) foreach ($row as $data) echo("<option value=\"" . $data . "\">" . $data . "</option>\n"); echo "</select>"; ?> </strong> <div align="center"></div></td> <td width="50%" bgcolor="#FFFF99"><div align="left"><strong> <select name="model"> <option>MODEL </option> </select> </strong></div></td> </tr> </table></td> </tr> </table> <table width="100%" border="0" cellspacing="0" cellpadding="5"> <tr bgcolor="#FFFF99"> <td width="55%"><div align="right"> <strong> <input name="submit" type="submit" value="Click here to search." /> </strong></div></td> <td width="45%" class="arial11"> </td> </tr> </table> <strong><br /> </strong> </form></td> </tr> </table></td> </tr> </table> </div> <p class="arial11"> </p></td> </tr> </table> <div id="toTop"></div> </div> <p> </p> <div class="special" id="footer"> <div align="center"> <hr width="650" size="1" noshade="noshade" /> </div> <div align="center"></div> </div></td> </tr> </table> </body> </html> <?php // the following code should be placed at the very end of each page that contains a link. //--------------'CLOSE PAGE'-------------------------- //close db mysql_close(); //--------------'END CLOSE PAGE'-------------------------- ?> Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/ Share on other sites More sharing options...
mikosiko Posted November 23, 2011 Share Posted November 23, 2011 you just need to modify the 2 SELECTS that you have in your van_roof_rack.php file to exclude the new category (MB) here $sqlSelect = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code <> 'LE' and Car_Make <> 'n/a'"; and here $sqlSelectModel = "SELECT DISTINCT Car_Model FROM products WHERE Car_Make = '".$strMake."' and Default_Code <> 'LE'"; try replacing Default_Code <> 'LE' with Default_Code NOT IN ( 'LE', 'MB') Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1290727 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2011 Share Posted November 23, 2011 Can Anyone Help With This... You should NOT have a separate page for each different category/product type, where the only thing that changes is the actual data you are operating on. You are making more work for yourself and by having so much duplicated code spread out in several pages you cannot see the forest for the trees to add on one more category. What you need to do is have one page that accepts a ?category=x (x=1,2,3,...) get parameter on the end of the URL that tells the code on that single page what the page should do. Edit: You also mentioned CSV several time. There's nothing in your hundred's and hundred's of lines of code having anything to do with a CSV data (that I can see - i.e. the forest for the trees problem.) Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1290766 Share on other sites More sharing options...
roldahayes Posted November 28, 2011 Author Share Posted November 28, 2011 When I mention CSV, the products, categories etc are added to the database by uploading an Excel CSV. This code was written years ago for us and I'm just trying to modify it to suit new purposes. Would it not make more sense to just include the Default_code instead of listing codes we DON'T want on the page? Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1291796 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2011 Share Posted November 28, 2011 If your Default_Code value for vans is an empty string in the database table, yes it would be better to use - Default_Code = '' On the subject of having only one page that you must maintain, in just the little bit that I looked at your code, you have a lot of inconsistencies between the pages in your markup and a lot of invalid markup. If you did something like the following at the start of the page, you can use one page for all three categories - <?php // either use an array in a configuration include file (shown inline for this example) or store/get this information in/from a database table $cat_info[1] = array('title'=>'Car Roof Racks','default_code'=>"= 'LE'"); // car "LE" $cat_info[2] = array('title'=>'Van Roof Racks','default_code'=>"= ''"); // van (if it is an empty string in your table) $cat_info[3] = array('title'=>'Motorbike Racks','default_code'=>"= 'MB'"); // "MB" // condition the 'category' input/set default $cat = isset($_GET['category']) ? (int)$_GET['category'] : 1; // default to 1 (Car) // validate the requested category if(isset($cat_info[$cat])){ // category found, set up common variables used on the page $title = $cat_info[$cat]['title']; // page title $default_code = $cat_info[$cat]['default_code']; // query search term $search_header = strtoupper($title); // strong header used in form } else { // category value is not supported, handle that condition here... die('Invalid information requested!'); } In the code on the page, you would use the values that are set in the above code - <title><?php echo $title; ?></title> $sqlSelect = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code $default_code and Car_Make <> 'n/a'"; $sqlSelectModel = "SELECT DISTINCT Car_Model FROM products WHERE Car_Make = '".$strMake."' and Default_Code $default_code"; <td height="29" bgcolor="#FFFF99"><div align="center" class="arial11"><strong><?php echo $search_header; ?></strong></div></td> $query = "SELECT DISTINCT Car_Make FROM products WHERE Car_Make <> 'all' and Car_Make <> 'link' and Default_Code $default_code and Car_Make <> 'n/a'"; The menu where you allow the visitor to pick the category would need to be modified to use the ?category=x, but with the category information stored in an array, you can let php produce that menu for you - <?php // build menu - $cat_menu = "<ul>"; foreach($cat_info as $key=>$arr){ $cat_menu .= "<li><a href='?category=$key'>{$arr['title']}</a></li>"; } $cat_menu .= "</ul>"; echo $cat_menu; // output menu where you need it ?> You would also use the ?category=x method in your the form action="..." attribute on that page so that you only have one form processing page. Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1291817 Share on other sites More sharing options...
roldahayes Posted November 28, 2011 Author Share Posted November 28, 2011 Thank you for the code, I will start to have a play with it. I'm not sure if I have totally explained what the pages need to do though.... There has to be 3 different search pages, one for the vans, one for the cars and one for the bikes - These dropdowns can onl have the relevant vehicles in them The uploaded CSV files has all of the data in there but essentially, the pages have to be like three separate websites.... People who go to the car search page will not want to see van or bikes in there and vice versa. What is confusing me is that products entered into the database with a default code of MB are still showing on the results page for the default code of CV It seems to be that if the page dons't understand a code it then defaults to the van results... Maybe I need to start again with this from scratch... Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1291829 Share on other sites More sharing options...
PFMaBiSmAd Posted November 28, 2011 Share Posted November 28, 2011 There has to be 3 different search pages There will be, they will just be all the same actual single page of code. The URLs of the 3 different search pages will be your_page.php?category=1, your_page.php?category=2, and your_page.php?category=3. The code I suggested WILL only display the correct information for whatever category (Car, Van, Motorbike) that was chosen. Give it a try. Based on the code you did post, the few modified lines that I posted are the only things you would need to change. This method I have suggested is used all over the Internet The forum you are on now only has one page - index.php. Everything you do on this site that is forum related simply uses GET parameters on the end of the URL to tell the code in index.php what it should do for any page request. Quote Link to comment https://forums.phpfreaks.com/topic/251680-can-anyone-help-with-this-please/#findComment-1291831 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.