arunpatal Posted December 8, 2012 Share Posted December 8, 2012 Hi, I want that if the name of image is in db is set the it shows image to me and it there is no value in db then no image... Now it shows image box but without image this is the code which show image <img src="product_images/<?php echo $img1; ?>.jpg" width="64" height="64" id="display_img1" onclick="MM_swapImage('main_image','','product_images/<?php echo $img1; ?>.jpg',1);MM_effectAppearFade('main_image', 1000, 0, 100, false)" /></a> I want it that if $img1 name is set in db then it show the image and if not then nothing.... Quote Link to comment https://forums.phpfreaks.com/topic/271766-if-isset/ Share on other sites More sharing options...
Love2c0de Posted December 8, 2012 Share Posted December 8, 2012 (edited) Have you got any queries? I haven't used the basic mysql functions in a while BUT I think it's something like: $qry = mysql_query("SELECT col FROM table_name WHERE col={$value}"); if(mysql_num_rows($qry) >= 1){ echo "you have an image"; //here you would do something like: $img = mysql_fetch_array($qry); $output_img = "<img src='product_images/{$img['col_name']}' alt='{$img['col_name']}' />"; } else{ echo "no image in database"; } You then just output to your page like so: <?php if (isset($output_img)){ print($output_img);} ?> To deal with multiple images you would do: while($img = mysql_fetch_array($qry)){ $output_img[] = "<img src='product_images/{$img['col_name']}' alt='{$img['col_name']}' />"; } Then: if (isset($output_img)){ print_r($output_img); } You don't just have to print_r() you could then maybe use a foreach loop and do the business in there. Edited December 8, 2012 by AoTBuNgLe Quote Link to comment https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398286 Share on other sites More sharing options...
arunpatal Posted December 8, 2012 Author Share Posted December 8, 2012 (edited) This is query $sql = mysql_query("SELECT * FROM product WHERE id='$id' LIMIT 1"); $productCount = mysql_num_rows($sql); // count the output amount if ($productCount > 0) { // get all the product details while($row = mysql_fetch_array($sql)){ $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $img1 = $row["img1"]; $img2 = $row["img2"]; $img3 = $row["img3"]; $img4 = $row["img4"]; I need this complete code with if isset statement which include javascript also in it <img src="product_images/<?php echo $img1; ?>.jpg" width="64" height="64" id="display_img1" onclick="MM_swapImage('main_image','','product_images/<?php echo $img1; ?>.jpg',1);MM_effectAppearFade('main_image', 1000, 0, 100, false)" /></a> Edited December 8, 2012 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398287 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 You can do like this: $sql = mysql_query("SELECT * FROM product WHERE id='$id' LIMIT 1"); if ($sql) { // If true, the query was successful if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_array($sql)); // Get product details $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $img1 = $row["img1"]; $img2 = $row["img2"]; $img3 = $row["img3"]; $img4 = $row["img4"]; if (!empty($img1)) { ?> <img src="product_images/<?php echo htmlentities($img1); ?>.jpg" width="64" height="64" id="display_img1" onclick="MM_swapImage('main_image','','product_images/<?php echo htmlentities($img1); ?>.jpg',1);MM_effectAppearFade('main_image', 1000, 0, 100, false)" /></a> <?php } } } I escaped the values that you are printing out from the database to protect against cross-site scripting (XSS) attacks (basically being able to manipulate the DOM with HTML or Javascript). Quote Link to comment https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398295 Share on other sites More sharing options...
arunpatal Posted December 9, 2012 Author Share Posted December 9, 2012 You can do like this: $sql = mysql_query("SELECT * FROM product WHERE id='$id' LIMIT 1"); if ($sql) { // If true, the query was successful if (mysql_num_rows($sql) > 0) { $row = mysql_fetch_array($sql)); // Get product details $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; $img1 = $row["img1"]; $img2 = $row["img2"]; $img3 = $row["img3"]; $img4 = $row["img4"]; if (!empty($img1)) { ?> <img src="product_images/<?php echo htmlentities($img1); ?>.jpg" width="64" height="64" id="display_img1" onclick="MM_swapImage('main_image','','product_images/<?php echo htmlentities($img1); ?>.jpg',1);MM_effectAppearFade('main_image', 1000, 0, 100, false)" /></a> <?php } } } I escaped the values that you are printing out from the database to protect against cross-site scripting (XSS) attacks (basically being able to manipulate the DOM with HTML or Javascript). Thanks alot to you and aotbungle It works :happy-04: :happy-04: Quote Link to comment https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398336 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.