roldahayes Posted February 13, 2012 Share Posted February 13, 2012 Hi, I have added this piece of code to my page: <?php $query = "SELECT Image_Van FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'"; // the result of the query $result = mysql_query($query) or die("Invalid query: " .mysql_error()); $pic = mysql_fetch_array($result); // show the image echo "<img src='images/product_images/".$pic['Image_Van']."'/>"; ?> Which now works well in showing an image that is stored in the database. The problem I have now, is that this new code stops the original search results from displaying. All the tables show as empty with a product price of £0.00 - Now if I move the new code to the bottom of the page - out side where the search results end, then everything works as it should. I have tried to clean up as many link breaks as possible from the page code to post it here, but please be aware that the page is quite large! Thanks for any help than you will be able to offer me, <?php //info bars colour $color_info="#D6D685"; // Almost Silver //FFFFCC $color1="#E9E9E9";// Light Blue $color2="#EAD5FF";// Light Blue include_once("func_lib.php"); include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } //test that make and model are passed in, if they are assign them if (($_GET ['make'] == "MAKE") or ($_GET ['model'] == "MODEL")) { $errMessage = "<br><br><b><font class=small>Your search hasn't returned any results, please re-enter your search again.</font></b><br><br><a href=van_accessories.php>Search again</a><br><br>"; } else { // get search values for make and model, removing " and ' chars $strMake = mysql_escape_string($_GET ['make']); $strModel = mysql_escape_string($_GET ['model']); } // set the Prod_Type that is to be shown regardless of car make and model $strDefaultProd = "XD"; //write query string that will pased in paging links $strQuery = "make=".$strMake."&model=".$strModel."&"; //echo $strQuery; //grab the page number that we're on $page = mysql_escape_string($_GET ['page']); //$strMake = "FIAT"; //$strModel = "Uno"; //create sql query $sqlSelect = "SELECT Prod_ID, Prod_Type, Prod_Model, Prod_Make, Prod_REF, Product_Desc, Price_ExVat, image_name FROM products"; $criteria = " WHERE (Car_Make = '".$strMake."' AND Car_Model = '".$strModel."' AND Default_Code = 'CV') OR (Car_Make = 'all' AND Default_Code = 'RA')"; //$criteria = " WHERE Car_Make = '".$strMake."' AND Car_Model = '".$strModel."'"; // assign the basic sqlquery $sqlquery = $sqlSelect . $criteria; //echo $sqlquery; #debug //get the result set $result = mysql_query($sqlquery); $count = mysql_num_rows($result); $full_count = $count; //set max number of records per page $records_per_page = 999; //Next figure out how many pages you'll have. $total_pages = ceil( $count / $records_per_page ); /* This just sets up a page variable which will be past in a "next" and "prev" link. The first link to this page may not actually have $page so this just sets it up. */ if ( $page == "" ) { $page = 1; } //echo "<br>page number".$page; #debug # this will set up the "First | Prev | " part of our navigation. if ( $page == 1 ) { # if we are on the first page then "First" and "Prev" # should not be links. $naviagation = "<font color=\"#666666\">First</font> | <font color=\"#666666\">Prev</font> | "; } else { # we are not on page one so "First" and "Prev" can be links $prev_page = $page - 1; $navigation = "<a href=\"results.php?".$strQuery."page=1\">First</a> | <a href=\"results.php?".$strQuery."page=".$prev_page."\">Prev</a> | "; } # this part will set up the rest of our navigation "Next | Last" if ( $page == $total_pages ) { # we are on the last page so "Next" and "Last" # should not be links // $navigation .= "<font color=\"#666666\">Next</font> | <font color=\"#666666\">Last</font>"; } else { # we are not on the last page so "Next" and "Last" # can be links $next_page = $page + 1; $navigation .= "<a href=\"results.php?".$strQuery."page=".$next_page."\">Next</a> | <a href=\"van_roof_bar_results.php?".$strQuery."page=".$total_pages."\">Last</a>"; } /* The final thing to do is your actual query, but first we have to figure out what the offset will be. */ $offset = ( $page - 1 ) * $records_per_page; //echo "<br>offset".$offset; #debug //create sql query $sqlSelect = "SELECT Car_Make, Car_Model, Prod_ID, Prod_Type, Prod_Model, Prod_Make, Priority, Prod_Code, Prod_REF, Product_Desc, Price_ExVat, Link, image_name FROM products"; $criteria = " WHERE (Car_Make = '".$strMake."' AND Car_Model = '".$strModel."' AND Default_Code = 'CV') OR (Car_Make = 'all' AND Car_Model = 'all' AND Default_Code = 'RA')"; $limit = " ORDER BY Priority ASC, Prod_Code ASC, Prod_ID ASC LIMIT $offset, $records_per_page"; // assign the basic sqlquery $sqlquery = $sqlSelect . $criteria . $limit ; //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 } ?> <html> <head> <title>Results Page:</title> <!-- start header graphic html --> <div align="center"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td background=""><table width="960" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td></td> </tr> </table> <strong></strong><!--END header--> <table width="960" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="2"> <tr> <td><div align="center"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td class="arial11"><table width="100%" border="0" cellpadding="10" cellspacing="0"> <tr> <td width="33%"><?php //$query = "SELECT Car_Make, Car_Model, Image_Van, FROM products WHERE Car_Make= '.$strMake.' AND Car_Model = '.$strModel.'"; $query = "SELECT Image_Van FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'"; // the result of the query $result = mysql_query($query) or die("Invalid query: " .mysql_error()); $pic = mysql_fetch_array($result); // show the image echo "<img src='images/product_images/".$pic['Image_Van']."'/>"; ?></td> <td width="11%"> </td> <td width="11%"> </td> <td width="45%"><?php echo '<a href=van_roof_rack_results.php?'.$_SERVER['QUERY_STRING'].'"><img src=2012/r-bars.jpg width=328px height=53px border=0></a>.'; ?> </tr> </table></td> </tr> </table> </div></td> </tr> </table> <table width="950" border="0" align="center" cellpadding="2" cellspacing="2" bordercolor="#FFFFFF"> <tr align="center" valign="top" class="std"> <td colspan="7"><hr width="500" size="1" /> <?php if (isset($errMessage)){ echo $errMessage; } // if no matches found from query if (!$result || (mysql_num_rows($result) == 0)) echo ("<b><font class=small>No matches were found.</font></b><br><br>"); // display the results else{ // use rowcount to display total results found echo("<br><p><em><font class=vehicle>Search Results for " . $strMake . " " . $strModel .".</em> Total Matches Found = " . $full_count . "</p>"); //*debug echo $navigation; } /* With your navigation built you can just echo that out any where you want it to appear. */ //echo $navigation; ?></td> </tr> <?php $title = "<tr align=\"center\" class=\"headertable\" height=\"32\">"; $title .= "<td width=\"95\"><img src=\"images/transparent.gif\" width=\"95\" height=\"1\" /></td>"; $title .= "<td width=\"60\">Make</td>"; $title .= "<td width=\"60\">Ref No.</td>"; $title .= "<td width=\"550\">Title/Description</td>"; $title .= "<td width=\"73\">Price Ex VAT</td>"; $title .= "<td width=\"73\">Price Inc VAT</td>"; $title .= "<td width=\"60\"><img src=\"2003/basket.gif\" width=\"21\" height=\"14\"></td>"; $title .= "</tr>"; //set the counters for the records results display screen $cnt = 0; $headercount1 = 0; $headercount2 = 0; $headercount3 = 0; $headercount4 = 0; $headercount5 = 0; $headercount6 = 0; $headercount7 = 0; while ($row = mysql_fetch_assoc($result)) { //cut the prod_type variable //get the length of the variable $strLength = strlen ($row["Prod_Type"]); //assign first 2 characters of variable $strPrefix = substr($row["Prod_Type"], 0, 2); //debug //echo "\n prefix :" . $strPrefix; //assign remaining characters of variable $strSuffix = substr($row["Prod_Type"], 2, $strLength); //echo "\n suffix :" . $strSuffix; //set product header image depending on the Prod_Type Code //start the table row echo "<tr align=center><td colspan=7 class=small>"; //set bg cell color diff for BH make switch ($strPrefix) { case "BH": $color=$color2; break; default: $color=$color1; break; }//end switch switch ($strPrefix) { case "BH": if ($headercount1 == 0) { echo "<img border=0 src=2003/searchheaders/bulkheads.jpg alt=Bulkheads></td></tr>"; echo $title; } $headercount1 ++; //$headercount1 break; case "RB": if ($headercount2 == 0) { echo "<img border=0 src=2003/searchheaders/roofbars.jpg alt=Roof-Bars></td></tr>"; echo $title; } $headercount2 ++; //$headercount1 break; case "BX": if ($headercount3 == 0) { echo "<img border=0 src=2003/searchheaders/bulkheads.jpg alt=Roof-Boxes></td></tr>"; echo $title; } $headercount3 ++; //$headercount1 break; case "BR": if ($headercount4 == 0) { echo "<img border=0 src=2003/searchheaders/bulkheads.jpg alt=Bike-Racks></td></tr>"; echo $title; } $headercount4 ++; //$headercount1 break; case "SR": if ($headercount5 == 0) { echo "<img border=0 src=2003/searchheaders/bulkheads.jpg alt=Ski-Rack></td></tr>"; echo $title; } $headercount5 ++; //$headercount1 break; case "CR": if ($headercount6 == 0) { echo "<img border=0 src=2003/searchheaders/bulkheads.jpg alt=Canoe-Rack></td></tr>"; echo $title; } $headercount6 ++; //$headercount1 break; case "XD": if ($headercount7 == 0) { echo "<img border=0 src=2003/searchheaders/general.jpg alt=General-Accessories></td></tr>"; echo $title; } $headercount7 ++; } //print a product row if the prodtype isn't set to INFO $pop_link = ""; if ($strSuffix != "INFO"){ //test to see if an image exists for the product if ($row["Link"] <> NULL){ $pop_link = " <a href=\"products/".$row["Link"]."?iframe=true&width=650&height=500\" rel=\"prettyPhoto\" >More information >></a>"; } echo "<tr class=stdtable align=center height=25>"; //DISPLAY THUMB IMAGE IF ONE WAS LISTED IN THE image_name FIELD $p_image_name = htmlspecialchars($row["image_name"]); //echo "p_image_name:$p_image_name"; if ($p_image_name != "") echo "<td bgcolor=$color width=\"95\"><img src=\"images/product_images/$p_image_name\" border=\"0\" /></td>"; else echo "<td bgcolor=$color width=\"95\"><img src=\"images/transparent.gif\" width=\"95\" height=\"50\" border=\"0\" /></td>"; echo "<td bgcolor=$color width=\"60\">". htmlspecialchars($row["Prod_Make"]) . "</td><td bgcolor=$color width=\"60\">" . htmlspecialchars($row["Prod_REF"]) . "</td><td bgcolor=$color align=left class=\"infolink\" width=\"550\">" . htmlspecialchars($row["Product_Desc"]) . $pop_link . "</td><td bgcolor=$color width=\"73\">£" . number_format(htmlspecialchars($row['Price_ExVat']), 2) . "</td><td bgcolor=$color width=\"73\">£" . number_format((calcVAT (htmlspecialchars($row['Price_ExVat']))), 2) . "</td><td bgcolor=$color width=\"60\"><a href=basket.php?src=".urlencode($_SERVER['REQUEST_URI'])."&productID=" . $row["Prod_ID"] . "><img src=2003/buy.gif border=0></a></td></tr>"; } // if the prod type is set to INFO then print out a new row for that information else { echo "<tr class=stdtable align=center height=25 ><td colspan=7 bgcolor=$color_info><em><strong>" . htmlspecialchars($row["Product_Desc"]) . "</strong></em></td></tr>"; } } ?> <tr align="center"> <td colspan="7" class="small1"></td> </tr> <tr> <td colspan="7" height="1" bgcolor="#003366"><img src="colour_spacer.gif" width="756" height="1" /></td> </tr> </table></td> </tr> </table> <strong></strong> <!--END header--> </p> <?php mysql_free_result($result); mysql_close(); //include footer code ?> Quote Link to comment https://forums.phpfreaks.com/topic/257019-new-code-stops-original-from-working-correctly/ Share on other sites More sharing options...
spiderwell Posted February 13, 2012 Share Posted February 13, 2012 it looks like you are using the same variable $result for both recordsets, and when you pass the second dataset to that variable it over writes the original. I havent studied it that closely but that was my first impression Quote Link to comment https://forums.phpfreaks.com/topic/257019-new-code-stops-original-from-working-correctly/#findComment-1317554 Share on other sites More sharing options...
roldahayes Posted February 13, 2012 Author Share Posted February 13, 2012 Perfect! I was using $query twice in the page.... Thank you for all your help in this... Quote Link to comment https://forums.phpfreaks.com/topic/257019-new-code-stops-original-from-working-correctly/#findComment-1317557 Share on other sites More sharing options...
spiderwell Posted February 13, 2012 Share Posted February 13, 2012 there is nothing to stop you using the same variable for stuff, but it can lead to the problem you had if you dont keep tabs on it. I would suggest its not the prefered/recommended way, but it isn't 'illegal' for want of a better phrase Quote Link to comment https://forums.phpfreaks.com/topic/257019-new-code-stops-original-from-working-correctly/#findComment-1317562 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.