Jump to content

[SOLVED] only showing a value if it equals something?


shadiadiph

Recommended Posts

Is this possible on my display page I have this code

 

<td>
<img src="showpic2.php?adid=<?=$id?>" />
</td>

 

the default background is in css and has a no picture image which shows when a picture is not there but for example in this table td there is the no image background picture controlled by css but no picture in the database in this cell so how can i make it so it only shows a picture when there is a value to the picture? right now just shows the background with the annoying no image x.

???

Link to comment
Share on other sites

mm sorry i have no idea how to do that?

 

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT thumbnail1,thumbnailtype1 FROM zloads where intProductID=".$_GET['adid'];
$result = MYSQL_QUERY($query); 
$data = MYSQL_RESULT($result,0,"thumbnail1"); 
$type = MYSQL_RESULT($result,0,"thumbnailtype1"); 
Header( "Content-type: $type"); 



print $data; 
?> 

Link to comment
Share on other sites

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT thumbnail1,thumbnailtype1 FROM zloads where intProductID=".$_GET['adid'];
$result = mysql_query($query);
if (mysql_num_rows() > 0) {
  $data = mysql_result($result,0,"thumbnail1"); 
  $type = mysql_result($result,0,"thumbnailtype1"); 
  Header( "Content-type: $type");
  print $data; 
} else {
  //picture was not found in database
  //display 'empty' picture
  //it would be best just to load it from external file
}
?> 

 

Link to comment
Share on other sites

mm this script isn't actually working if their is  apictur it still displays it but if there isn't it wont display anything in the else { section when i view the page directly showpic it gives me the error

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/hmtcompa/public_html/user/showpic3.php on line 6

 

if their is no image won't show anything even something simple.

 

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT thumbnail4,thumbnailtype4 FROM zloads where intProductID=".$_GET['adid'];
$result = mysql_query($query);
  if (mysql_num_rows($result) > 0) {
  $data = mysql_result($result,0,"thumbnail4"); 
  $type = mysql_result($result,0,"thumbnailtype4"); 
  Header( "Content-type: $type");
  print $data; 
} 

else 
{


print hello;


  
}
?>  

Link to comment
Share on other sites

I have this no it is outputting a load if rubbish looks like it is trying to read the .png file but displays aload of binary code?

 

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT thumbnail4,thumbnailtype4 FROM zloads where intProductID=".$_GET['adid'];
$result = mysql_query($query);
  if (mysql_num_rows($result) > 0) {
  $data = mysql_result($result,0,"thumbnail4"); 
  $type = mysql_result($result,0,"thumbnailtype4"); 
  Header( "Content-type: $type");
  print $data; 
} 

else 
{
header("Content-Type: image/png");
$file = ('../pix/noimage.png');
$source = (fread(fopen($file, "r"), filesize($file)));
print ($source);
  
}
?>  

Link to comment
Share on other sites

i got rid of the num_rows error i was gettting it now displays the noimage.png all the time even when there is a piture in the database and never displays the value of $data???

 

<?php 
require("global/admin_functions.php"); 

$query = "SELECT thumbnail2,thumbnailtype2 FROM zloads where intProductID=".$_GET['adid'];
$result = mysql_query($query);
  if (mysql_query($result) > 0) {
  $data = mysql_result($result,0,"thumbnail2"); 
  $type = mysql_result($result,0,"thumbnailtype2"); 
  Header( "Content-type: $type");
  print $data; 
} 
else 

{
header("Content-Type: image/png"); 
$file = ('../pix/noimage.png');
$source = (fread(fopen($file, "r"), filesize($file)));
print ($source);
  
}?>

 

Link to comment
Share on other sites

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT thumbnail4,thumbnailtype4 FROM zloads where intProductID=".$_GET['adid']." LIMIT 1";
$result = mysql_query($query);
if ($r = mysql_fetch_assoc($result)) {
  $data = $r["thumbnail4"]; 
  $type = $r["thumbnailtype4"]; 
  header( "Content-type: $type");
  print $data; 
} 
else {
  header("Content-Type: image/png");
  $source = file_get_contents("../pix/noimage.png");
  print $source;
}
?> 

Link to comment
Share on other sites

mm thanks but that doesn't work gives the error
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/hmtcompa/public_html/user/showpic4.php on line 6

this is the closest i have come with no errors but the problem is in the > 0 part of it.


<?php

require("../global/admin_functions.php");

 

$query = "SELECT thumbnail3,thumbnailtype3 FROM zloads where intProductID=".$_GET['adid'];

$result = mysql_query($query);

  if (mysql_query($result) > 0) {

  $data = mysql_result($result,0,"thumbnail3");

  $type = mysql_result($result,0,"thumbnailtype3");

  Header( "Content-type: $type");

  print $data;

}

else

 

{

header("Content-Type: image/png");

$file = ('../pix/noimage.png');

$source = (fread(fopen($file, "r"), filesize($file)));

print ($source);

 

}?>

[/code]

Link to comment
Share on other sites

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT `thumbnail4`,`thumbnailtype4` FROM `zloads` WHERE `intProductID` = '{$_GET['adid']}' LIMIT 1";
$result = mysql_query($query);
if ($r = mysql_fetch_assoc($result)) {
  $data = $r["thumbnail4"]; 
  $type = $r["thumbnailtype4"]; 
  header( "Content-type: $type");
  print $data; 
} 
else {
  header("Content-Type: image/png");
  $source = file_get_contents("../pix/noimage.png");
  print $source;
}
?> 

 

ok bro.. try that.. and try both EXISTING images.. and NONEXISTING images.. if anything it will show the warning for non existing images..

Link to comment
Share on other sites

thanks yours only works on the one where there are images in the database it is like in my code > 0 shows the noimage.png always and not the image in the database where there is one and if it its < 1 it shows the image in the database but where there are no images it doesn't display the noimage.png

Link to comment
Share on other sites

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT `thumbnail4`,`thumbnailtype4` FROM `zloads` WHERE `intProductID` = '{$_GET['adid']}' LIMIT 1";
$result = mysql_query($query);
if ($r = @mysql_fetch_assoc($result)) {
  $data = $r["thumbnail4"]; 
  $type = $r["thumbnailtype4"]; 
  header( "Content-type: $type");
  print $data; 
} 
else {
  header("Content-Type: image/png");
  $source = file_get_contents("../pix/noimage.png");
  print $source;
}
?>

 

ok in this case, the above will solve ur problem

Link to comment
Share on other sites

freaky got the same result the blank image wasn't showing again but when i checked the direct result on showpic3.php and showpic4.php the result was showing the noimage.png both times but on the page that is calling it was showing the image correctly that was in the database being called by showpic3.php but when i viewed showpic3.php it was displaying only the noimage.png and on showpic4.php the same showed the image but on the display page calling showpic4.php it just came up blank??

Link to comment
Share on other sites

main page

<table class="adpix">
<tr>
<td>
<img src="../showpic1.php?adid=<?=$id?>" />
</td>
<td>
<img src="../showpic2.php?adid=<?=$id?>" />
</td>
</tr>
<tr>
<td>
<img src="../showpic3.php?adid=<?=$id?>" />
</td>
<td>
<img src="../showpic4.php?adid=<?=$id?>" />
</td>
</tr>
</table>

 

 

showpic3.php

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT `thumbnail3`,`thumbnailtype3` FROM `zloads` WHERE `intProductID` = '{$_GET['adid']}' LIMIT 1";
$result = mysql_query($query);
if ($r = @mysql_fetch_assoc($result)) {
  $data = $r["thumbnail3"]; 
  $type = $r["thumbnailtype3"]; 
  header( "Content-type: $type");
  print $data; 
} 
else {
  header("Content-Type: image/png");
  $source = file_get_contents("../pix/noimage.png");
  print $source;
}
?>

 

showpic4.php

<?php 
require("../global/admin_functions.php"); 

$query = "SELECT `thumbnail4`,`thumbnailtype4` FROM `zloads` WHERE `intProductID` = '{$_GET['adid']}' LIMIT 1";
$result = mysql_query($query);
if ($r = @mysql_fetch_assoc($result)) {
  $data = $r["thumbnail4"]; 
  $type = $r["thumbnailtype4"]; 
  header( "Content-type: $type");
  print $data; 
} 
else {
  header("Content-Type: image/png");
  $source = file_get_contents("../pix/noimage.png");
  print $source;
}
?>

Link to comment
Share on other sites

this is doing my head in

doesn't seem capable of calling the else statement properly.

if the code says

 

if (mysql_query($result) < 1) {

 

it shows the image from the database everytime

 

if the code says

if (mysql_query($result) > 1) {

 

it shows the noimage,png everytime always one but not either or?

 

full code is

 

<?php 
require("global/admin_functions.php"); 

$query = "SELECT thumbnail3,thumbnailtype3 FROM zloads where intProductID=".$_GET['adid'];
$result = mysql_query($query);
  if (mysql_query($result) < 1) {
  $data = mysql_result($result,0,"thumbnail3"); 
  $type = mysql_result($result,0,"thumbnailtype3"); 
  Header( "Content-type: $type");
  print $data; 
} 
else 

{
header("Content-Type: image/png"); 
$file = ('../pix/noimage.png');
$source = (fread(fopen($file, "r"), filesize($file)));
print ($source);
  
}?>

 

if it is set at <0 or >0 it shows the noimage.png everytime.

Link to comment
Share on other sites

Hi shadiadiph,

 

I believe the confusion is being caused by checking 'mysql_query($result)' instead of checking 'mysql_num_rows($result)'. 'mysql_query' returns a boolean value of true if the query executes successfully or false if it doesn't. In this case you are trying to run a query on a result so the function returns false. As you know, PHP is a loosely typed language, so when you are comparing a boolean value against an integer, PHP will get the integer value of the boolean which in this case ends up being 0 (false == 0,, but false !== 0). so:

 

if (mysql_query($result) < 1) {

 

always returns true and runs the code in the first set of brackets

 

if (mysql_query($result) > 1) {

 

always returns false and runs the code in the else set of brackets therefore always showing the noimage.png.

 

Hope this helps!

Link to comment
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.