Jump to content

Recommended Posts

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....

Link to comment
https://forums.phpfreaks.com/topic/271766-if-isset/
Share on other sites

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 by AoTBuNgLe
Link to comment
https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398286
Share on other sites

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 by arunpatal
Link to comment
https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398287
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398295
Share on other sites

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).

 

 

:happy-04: Thanks alot to you and aotbungle :happy-04:

 

It works :happy-04: :happy-04: :happy-04: :happy-04:

Link to comment
https://forums.phpfreaks.com/topic/271766-if-isset/#findComment-1398336
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.