roldahayes Posted February 8, 2012 Share Posted February 8, 2012 Hi, I have a search results page that return the results based on "Make" & "Model" of vehicle. The question is, can an image be echoed onto that results page based on which make and model has been searched for? The image is stored in a field called "Image_Van" so would need to display that depending on the row in that database that has been selected from Make & Model... Hope this makes sense! <?php //info bars colour $color_info="#D6D685"; // Almost Silver //FFFFCC $color1="#E9E9E9";// Light Blue $color2="#EAD5FF";// Light Blue //include function library code include_once("func_lib.php"); // use the user_connection include file's connectDB function 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, Image_Van FROM products"; //change this back to 'FILL THIS IS IN' to switch accessories off! $criteria = " WHERE (Car_Make = '".$strMake."' AND Car_Model = '".$strModel."' AND Default_Code = 'RS') OR (Car_Make = 'all' AND Default_Code = 'SA')"; //$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=\"van_steps_results.php?".$strQuery."page=1\">First</a> | <a href=\"van_steps_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=\"van_steps_results.php?".$strQuery."page=".$next_page."\">Next</a> | <a href=\"van_steps_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, Image_Van FROM products"; $criteria = " WHERE (Car_Make = '".$strMake."' AND Car_Model = '".$strModel."' AND Default_Code = 'RS') OR (Car_Make = 'all' AND Car_Model = 'all' AND Default_Code = 'SA')"; $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 } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Results Page:</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script> <!--script src="js/jquery.lint.js" type="text/javascript" charset="utf-8"></script--> <link rel="stylesheet" href="css/prettyPhoto.css" type="text/css" media="screen" title="prettyPhoto main stylesheet" charset="utf-8" /> <script src="js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script> <!-- start header graphic html --> <style type="text/css"> <!-- .table { border: 0px none; border-collapse: separate; } BODY { } A { COLOR: #003366; TEXT-DECORATION: none; } A:hover { TEXT-DECORATION: underline; color: #900; } .small { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; } .std { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; COLOR: #000000; } .header { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; FONT-WEIGHT: BOLD; COLOR:#000000; } .bigheader { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; FONT-WEIGHT: BOLD; COLOR:#FFFFFF; } .error { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; COLOR:#FF0000 } .headertable { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; FONT-WEIGHT: regular; COLOR:#FFFFFF; BACKGROUND-COLOR:#000066; } .stdtable { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; FONT-WEIGHT: regular; COLOR:#000066; } .style16 {font-size: 11px} .style7 { color: #CC0000; font-style: italic; } .small1 { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; } .header1 { FONT-SIZE: 8pt; FONT-FAMILY: Verdana; FONT-WEIGHT: BOLD; COLOR:#000000; } a:link { color: #900; } --> </style> <div align="center"> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td background=""><table width="100%" border="0" align="center" cellpadding="2" cellspacing="0"> <tr> <td bgcolor="#003366"> </td> </tr> </table> <!--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%"><p><img src="2003/results.gif" alt="" width="238" height="43" /></p> <p> </p> <p> </p></td> </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); //debug //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/step.jpg></td></tr>"; echo $title; } $headercount1 ++; //$headercount1 break; // REMOVE ALL THIS COMMENTING IF NEEDED TO SHOW THE ROOF BARS! case "RB": // if ($headercount2 == 0) // { // echo "<img border=0 src=2003/searchheaders/NOTUSED.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/towbar.jpg alt=General-Accessories></td></tr>"; echo $title; } $headercount7 ++; } //end header image selection and close the table row. //echo "</td></tr>"; // if (($cnt++)%2) // { // $color="#E9E9E9"; // Almost Silver // } // else // { // $color="#E9E9E9";// Light Blue // } //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_Van = htmlspecialchars($row["Image_Van"]); //echo "p_Image_Van:$p_image_name"; if ($p_Image_Van != "") echo "<td bgcolor=$color width=\"95\"><img src=\"images/product_images/$p_Image_Van\" 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>"; } } //****************************************************************************************************** /* //get all porducts from the database that are marked with prod_type XD regardless of car make and model $sqlSelectDefault = "SELECT Prod_ID, Prod_Type, Prod_Model, Prod_Make, Priority, Prod_Code, Prod_REF, Product_Desc, Price_ExVat FROM products"; $criteriaDefault = " WHERE Prod_Type LIKE '".$strDefaultProd."%'"; $limitDefault = "ORDER BY Priority ASC, Prod_Code ASC"; // assign the basic sqlquery $sqlqueryDefault = $sqlSelectDefault . $criteriaDefault . $limitDefault ; //echo $sqlquery; //get the result set $resultDefault = mysql_query($sqlqueryDefault); // if no matches found from query if (!$resultDefault || (mysql_num_rows($resultDefault) == 0)){ //echo ("<b><font class=small>No matches were found.</font></b>"); } // display the results else{ // use rowcount to display total results found $countDefault = mysql_num_rows($resultDefault); //echo $count; //echo("<p>Total Matches Found = " . $rowCount . "</p>"); //*debug } //set product header image depending on the Prod_Type Code //start the table row echo "<tr align=left><td colspan=6 class=small>"; echo "<img border=0 src=2003/searchheaders/general.jpg alt=General-Accessories>"; echo "</td></tr>"; while ($rowDefault = mysql_fetch_assoc($resultDefault)) { //cut the prod_type variable //get the length of the variable $strLengthDefault = strlen ($rowDefault["Prod_Type"]); //assign first 2 characters of variable $strPrefixDefault = substr($rowDefault["Prod_Type"], 0, 2); //debug //echo htmlspecialchars($rowDefault["Prod_ID"]); //echo "\n prefix :" . $strPrefixDefault; //assign remaining characters of variable $strSuffixDefault = substr($rowDefault["Prod_Type"], 2, $strLengthDefault); //debug //echo "\n suffix :" . $strSuffixDefault; if ($strSuffixDefault != "INFO"){ $color="#E9E9E9"; // Almost Silver //print a product row if the prodtype isn't set to INFO echo "<tr class=stdtable align=center height=25 ><td bgcolor=$color>". htmlspecialchars($rowDefault["Prod_Make"]) . "</td><td bgcolor=$color>" . htmlspecialchars($rowDefault["Prod_REF"]) . "</td><td bgcolor=$color align=left>" . htmlspecialchars($rowDefault["Product_Desc"]) . "</td><td bgcolor=$color>?" . number_format(htmlspecialchars($rowDefault['Price_ExVat']), 2) . "</td><td bgcolor=$color>?" . number_format((calcVAT (htmlspecialchars($rowDefault['Price_ExVat']))), 2) . "</td><td bgcolor=$color><a href=basket.php?src=".urlencode($_SERVER['REQUEST_URI'])."&productID=" . $rowDefault["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=6 bgcolor=$color><em>" . htmlspecialchars($rowDefault["Product_Desc"]) . "</em></td></tr>"; } } */ ?> <tr align="center"> <td colspan="7" class="small1"></td> </tr> <tr> <!-- <tr align="center" class="headertable" height="32"> <td width="145">Product Name</td> <td width="178">Ref No.</td> <td width="280">Description</td> <td width="73">Price Ex VAT</td> <td width="73">Price Inc VAT</td> <td width="60"><img src="2003/basket.gif" width="21" height="14"> </td> </tr>--> <td colspan="7" height="1" bgcolor="#003366"><img src="colour_spacer.gif" width="756" height="1" /></td> </tr> </table></td> </tr> </table> <p><br /> </p> </p> <div align="center"><br /> </div> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td> </td> </tr> </table> <?php //free up some memory mysql_free_result($result); // close the aatabase connection mysql_close(); //include footer code ?> <br /> </table> </html> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 8, 2012 Share Posted February 8, 2012 Seriously, you're going to dump that entire script (with all those line breaks) and expect what from us? You can either store the image as a flat file (i.e. jpg, gif, png) and store the path in the DB or you can actually store the image data in the DB. The first method is the easiest IMO. So, just store the image path for each record in the field "Image_Van". Then, when you query the records use that value to create an image tag using that value as the src parameter value of the image. $query = "SELECT make, model, Image_Van FROM table_name WHERE make='$make' AND mode='$maode'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { echo "{$row['make']}, {$row['model']} <img src='{$row['Image_Van']}'><br>\n"; } Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 8, 2012 Author Share Posted February 8, 2012 Thanks for the reply (and going through the endless line breaks....!) I have used: <?php $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE make='$make' AND model='$model'"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) {echo "{$row['make']}, {$row['model']} <img src='{$row['Image_Van']}'><br>\n";} ?> But this produces the error: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/site.co.uk/subdomains/httpdocs/image_results.php on line 1192 Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /var/www/vhosts/site.co.uk/subdomains/httpdocs/image_results.php on line 1656 Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 8, 2012 Share Posted February 8, 2012 Perhaps this... $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE make='$make' AND model='$model'"; needs to be... $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND Car_Model='$model'"; Also helps if you echo out your queries initially to make sue they are producing what you expect Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 8, 2012 Author Share Posted February 8, 2012 That gets rid of the warnings but now no search results are shown at all... Quote Link to comment Share on other sites More sharing options...
litebearer Posted February 8, 2012 Share Posted February 8, 2012 I should have looked further. change this... while($row = mysql_fetch_assoc($result)) {echo "{$row['make']}, {$row['model']} <img src='{$row['Image_Van']}'><br>\n";} to this... while($row = mysql_fetch_assoc($result)){ echo "{$row['Car_Make']}, {$row['Car_Model']} <img src='{$row['Image_Van']}'><br>\n"; } Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 8, 2012 Share Posted February 8, 2012 I should have looked further. Or, perhaps the OP should have looked further and adjusted the provided code to meet his needs. The code I provided was just an example and should never have been simply copy/pasted into the script. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 8, 2012 Share Posted February 8, 2012 I should have looked further. Or, perhaps the OP should have looked further and adjusted the provided code to meet his needs. The code I provided was just an example and should never have been simply copy/pasted into the script. unfortunately, that's all people want these days... Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 8, 2012 Author Share Posted February 8, 2012 And if you took the time to read reply number 2, you would realise that it wasn't a copy and paste.... Thanks for all the advise though, I have actually learned something here... Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 8, 2012 Share Posted February 8, 2012 And if you took the time to read reply number 2, you would realise that it wasn't a copy and paste.... Thanks for all the advise though, I have actually learned something here... I know it wasn't a complete copy/paste but still - my expectation is that the person receiving the code goes through it to understand what it is doing and can debug minor errors. The fact that you got errors stating "supplied argument is not a valid MySQL result resource" should have told you that the query failed. But, you seem to be willing to learn. So, I'll provide a couple suggestions on how you could have found/fixed these errors yourself. 1. I always create my queries as string variables instead of defining them in the mysql_query() function so I can echo them to the page when an error occurs. Of course, you should have a switch in your logic to show a generic error when in a production environment. 2. On the problem you had with no results being displayed you could have checked that there were results returned from the query. Example code: //Put this in a config file that is loaded for any page define('DEBUG_MODE', true); $query = "SELECT somethign FROM table_name WHERE foo='$bar'"; $result = mysql_query($query); if(!$result) { //Query failed if(DEBUG_MODE !== true) { echo "There seems to be a problem. Please try again later'" } else { echo "The query failed. Query: {$query}<br>Error: " . mysql_error(); } } else { //The query passed. Check if there were results if(!mysql_num_rows($result)) { echo "There were no matching records."; } else { //Output the result while($row = mysql_fetch_assoc($result)) { //Echo the formatted data } } } Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 9, 2012 Author Share Posted February 9, 2012 Ok, NOW I'm learning something....! Using a variation of the Debug code, I am realising that there are no results to be displayed... Maybe I'm looking at this in the wrong way.... I just need an image of the Car Model displayed at the top of the search results so... The end of the URL of the search results will effectively be what is displayed in the "Car_model" field so could I call the image URL from the "Image_Van" field depending on the URL? Possibly using $_SERVER['QUERY_STRING ?? Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 9, 2012 Author Share Posted February 9, 2012 Right.. I am determined to get this working!!! This is by debug code: What I need to know is should this be echoing any results if I have coded correctly or am I missing the point entirely...? include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } //Put this in a config file that is loaded for any page define('DEBUG_MODE', true); $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND CAr_Model='$model'"; //execute sql Statement $result = mysql_query($query); if(!$result) { //Query failed if(DEBUG_MODE !== true) { echo "There seems to be a problem. Please try again later'"; } else { echo "The query failed. Query: {$query}<br>Error: " . mysql_error(); } } else { //The query passed. Check if there were results if(!mysql_num_rows($result)) { echo "There were no matching records..."; } else { //Output the result while($row = mysql_fetch_assoc($result)) { //Echo the formatted data } } } Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 No, it won't echo results. Replace "//Echo the formatted data" with echo '<pre>' . print_r($row, true) . '</pre>'; Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 9, 2012 Author Share Posted February 9, 2012 Im not sure I am going in the right direction with this but I have now got: //create sql Statement $query_img = "SELECT Image_Van FROM products WHERE Car_Model='$model'"; //execute sql Statement $result_img = mysql_query($query_img); //get rows $row = mysql_fetch_array($result_img); //output Image echo "<img src=\"includes/images/".$row['Van_Image']."\">"; Am i going the right way with this.... Quote Link to comment Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 That will output the image for the last row returned. So if you return more than one row you still only get one image. If you want to show multiple images you need to loop the result. while($row = mysql_fetch_assoc($result_img)) { echo '<img src="includes/images/' . $row['Van_Image'] . '" />'; } Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 Day Three... (Still Determined!) Ok, here is what I am using now: include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } // create the query $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND Car_Model='$model'"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " .mysql_error()); $pic = mysql_fetch_array($result); // show the image echo "<img src='images/product_images".$pic['Image_Van']."' width='100' height='100'/>"; And here is an example of how the data base is set up: CREATE TABLE `products` ( `Prod_ID` int(11) NOT NULL auto_increment, `Car_Make` varchar(64) default NULL, `Car_Model` varchar(64) default NULL, `Car_ID` varchar(64) default NULL, `Prod_Type` varchar(64) default NULL, `Prod_Code` varchar(50) default NULL, `Default_Code` varchar(50) default NULL, `Priority` int(11) default NULL, `Prod_Model` varchar(50) default NULL, `Prod_Make` varchar(50) default NULL, `Product_Desc` varchar(255) default NULL, `Prod_REF` varchar(50) default NULL, `Price_ExVat` varchar(50) default NULL, `Post_ID` varchar(50) default NULL, `Link` varchar(255) default NULL, `image_name` varchar(255) default NULL, `Image_Van` varchar(255) default NULL, PRIMARY KEY (`Prod_ID`) ) ENGINE=MyISAM AUTO_INCREMENT=1887025 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1887025 ; So, the url to the image is stored in 'Image_Van' and I need to be able to show it when someone selects a certain Make & Model of vehicle. Using the page above, I am getting "Invalid query: Query was empty" So I am obviously going wrong somewhere.... Advise please?! Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 Day Three... (Still Determined!) Ok, here is what I am using now: include_once("usr_conn.php"); if (!connectDB()) { echo "<p>Unable To Connect To Database</p>"; return; } // create the query $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND Car_Model='$model'"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " .mysql_error()); $pic = mysql_fetch_array($result); // show the image echo "<img src='images/product_images".$pic['Image_Van']."' width='100' height='100'/>"; And here is an example of how the data base is set up: CREATE TABLE `products` ( `Prod_ID` int(11) NOT NULL auto_increment, `Car_Make` varchar(64) default NULL, `Car_Model` varchar(64) default NULL, `Car_ID` varchar(64) default NULL, `Prod_Type` varchar(64) default NULL, `Prod_Code` varchar(50) default NULL, `Default_Code` varchar(50) default NULL, `Priority` int(11) default NULL, `Prod_Model` varchar(50) default NULL, `Prod_Make` varchar(50) default NULL, `Product_Desc` varchar(255) default NULL, `Prod_REF` varchar(50) default NULL, `Price_ExVat` varchar(50) default NULL, `Post_ID` varchar(50) default NULL, `Link` varchar(255) default NULL, `image_name` varchar(255) default NULL, `Image_Van` varchar(255) default NULL, PRIMARY KEY (`Prod_ID`) ) ENGINE=MyISAM AUTO_INCREMENT=1887025 DEFAULT CHARSET=utf8 AUTO_INCREMENT=1887025 ; So, the url to the image is stored in 'Image_Van' and I need to be able to show it when someone selects a certain Make & Model of vehicle. Using the page above, I am getting "Invalid query: Query was empty" So I am obviously going wrong somewhere.... Advise please?! in the above code, $query holds the SQL, in your mysql_query call you are using $sql, which holds nothing. $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND Car_Model='$model'"; // the result of the query $result = mysql_query($query) or die("Invalid query: " .mysql_error()); Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 Could you explain that in "Laymans" terms please... I'm struggling to grasp it. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 I wrote one sentence.... ok, these lines that you have here: $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make='$make' AND Car_Model='$model'"; // the result of the query $result = mysql_query($sql) or die("Invalid query: " .mysql_error()); you have $query set to the SQL statement (your select statement). Now when you use mysql_query() below that, you use the variable $sql, $sql does not hold the SQL statement, $query does. So $query needs to be used as the mysql_query() parameter. The code that I provided in my last reply has the updated code. Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 Sorry, I was looking for the mistake in the code you had written, I didn't notice that you has corrected it. My bad! Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 OK, I (might) be getting somewhere here... It now appears to be recognising the make and model... I have a syntax error though when using; $query = "SELECT Car_Make, Car_Model, Image_Van, FROM products WHERE Car_Make= '.$strMake.' AND Car_Model = '.$strModel.'"; What am I doing wrong here? Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted February 10, 2012 Share Posted February 10, 2012 you're concatenating incorrectly, and it's not even needed. this will do fine: $query = "SELECT Car_Make, Car_Model, Image_Van, FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'"; Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 Still showing the same error, Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM products WHERE Car_Make= 'CITROEN ' AND Car_Model = 'NEMO 08-'' at line 1 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 10, 2012 Share Posted February 10, 2012 you're concatenating incorrectly, and it's not even needed. this will do fine: $query = "SELECT Car_Make, Car_Model, Image_Van, FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'"; Remove the comma before the "FROM". I also prefer to format my queries in multiple lines to make them easier to read/analyze. $query = "SELECT Car_Make, Car_Model, Image_Van FROM products WHERE Car_Make= '$strMake' AND Car_Model = '$strModel'"; Quote Link to comment Share on other sites More sharing options...
roldahayes Posted February 10, 2012 Author Share Posted February 10, 2012 Woo Hoo! The image is now displaying correctly!! The only problem now is that all the search result data that normally shows is now blank.... Could this code kill off the other queries? Quote Link to comment 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.