Jump to content

If field contains data, show image


adman4054

Recommended Posts

Hello--

Very new to PHP and looking for some help. I have found several examples, but none that I could get to work. I'm trying to write some code that checks to see if there data contained in a field. If there is I want to show an image from a folder. If there isn’t data, nothing would display.

 

This is a site with three listing levels. The top level, called premium ("p" as the listing type) and can contain a video, so I would like to "If [listing type] in the "listings" table = "P" and there is a video in the video table for this listing then display /images/sample.jpg (image is not dynamically called). The image will link to the listing. I.e., href=”company/php?listID=6

 

The db is "test", the video table is "video" and there are three fields; "video path", "video image" and "listing ID". The other table involved is “listings” where the listing type is stored. 

 

Link to comment
https://forums.phpfreaks.com/topic/221709-if-field-contains-data-show-image/
Share on other sites

Hello Solon--

Thanks for looking at my post. I'm posting two codes that I found while searching, but me being so new to PHP I'm not sure how to make them work

This code looks like its checking to make sure the field is not empty, but is it just checking for image 2? Also, "row_rs radiators" am I getting that from a query?

<?php if (!empty($row_rs_radiators['image2'])) { echo $row_rs_radiators['image2']; } ?>

This example also looks like it would work, but I'm not sure how to customize it for my use

<?php if (!empty($mySobi->customFieldsData['field_myvideofield']))
      echo <img src=\"/images/stories/has_video.jpg\">; ?>

Thanks again.

Thank you for the assistance, not sure I'm giving you the right stuff, but here goes:

 

$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()");			
		while($detail = mysql_fetch_array($query)) {
		 //echo "<br>".$detail['listingid'];
		 $listingID = $detail['listingid'];
		  			$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()");			
					while($get = mysql_fetch_array($query2)) {
					$imageName = $get['logoImage'];
					if($imageName == ''){
						$imageName = 'no_image.jpeg';	
					}
					$phone = $get['listPhone'];
					$toll= $get['listToll'];



$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ");
			$numRowsVideo_sr = mysql_num_rows($query2);
			//$numRowsVideo_sr = 0;

Thank you for the assistance, not sure I'm giving you the right stuff, but here goes:

 

$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()");			
		while($detail = mysql_fetch_array($query)) {
		 //echo "<br>".$detail['listingid'];
		 $listingID = $detail['listingid'];
		  			$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()");			
					while($get = mysql_fetch_array($query2)) {
					$imageName = $get['logoImage'];
					if($imageName == ''){
						$imageName = 'no_image.jpeg';	
					}
					$phone = $get['listPhone'];
					$toll= $get['listToll'];



$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ");
			$numRowsVideo_sr = mysql_num_rows($query2);
			//$numRowsVideo_sr = 0;

to actually get an error from the mysql_query

$result=mysql_query($query) or die(mysql_error());

 

Do you at all connect to mysql, and then choose database?

 

if(empty($get['logoImage'])){
$imageName = 'no_image.jpeg';	
}else{
$imageName = $get['logoImage'];
}

 

Maybe you should look into LEFT JOIN? =o

Try it like this and let us know if you get any errors.

$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error());			
		while($detail = mysql_fetch_array($query)) {
		 //echo "<br>".$detail['listingid'];
		 $listingID = $detail['listingid'];
		  			$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()")  or die("Query 2: ".mysql_error());			
					while($get = mysql_fetch_array($query2)) {
					$imageName = $get['logoImage'];
					if($imageName == ''){
						$imageName = 'no_image.jpeg';	
					}
					$phone = $get['listPhone'];
					$toll= $get['listToll'];



$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ")  or die("Query 3:".mysql_error());
			$numRowsVideo_sr = mysql_num_rows($query2);
			//$numRowsVideo_sr = 0;

I'm so sorry, I'm confusing you. I attached a set of queries that where not really related to what I'm trying to accomplish. I thought that by showing you the queries it would give you an idea of how the code is structured and the table information. The "image and "no image" isnt part of the task I need to accomplish.

 

I'm trying to look into the video table, if there is a video for a particular listing it will pull an image from mu images folder.-- if video exists (by whatever query will work) then display a video icon from /images/video.gif.

 

Sorry, appreciate the help.

or you can place the last query into the second while loop and create an if statement saying that if there is a video for this listing echo the image else echo nothing like so:

<?php
$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error());			
		while($detail = mysql_fetch_array($query)) {
		 //echo "<br>".$detail['listingid'];
		 $listingID = $detail['listingid'];
		  			$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()")  or die("Query 2: ".mysql_error());			
					while($get = mysql_fetch_array($query2)) 
					{
						$imageName = $get['logoImage'];
						if($imageName == '')
						{
							$imageName = 'no_image.jpeg';	
						}
						$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ")  or die("Query 3:".mysql_error());
						$numRowsVideo_sr = mysql_num_rows($query2);
						if($numRowsVideo_sr >= 1)
						{
							echo "<img src=''/>";
						}
						else
						{
							echo "NO IMAGE";
						}
					}
					$phone = $get['listPhone'];
					$toll= $get['listToll'];




			//$numRowsVideo_sr = 0;
                ?>

 

i did not test this but it should work.

:) Try it now:

<?php
$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error());			
		while($detail = mysql_fetch_array($query)) {
		 //echo "<br>".$detail['listingid'];
		 $listingID = $detail['listingid'];
		  			$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()")  or die("Query 2: ".mysql_error());			
					while($get = mysql_fetch_array($query2)) 
					{
						$imageName = $get['logoImage'];
						if($imageName == '')
						{
							$imageName = 'no_image.jpeg';	
						}
						$listID = $get['listingID'];
						$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ")  or die("Query 3:".mysql_error());
						$numRowsVideo_sr = mysql_num_rows($query2);
						if($numRowsVideo_sr >= 1)
						{
							echo "<img src=''/>";
						}
						else
						{
							echo "NO IMAGE";
						}
					}
					$phone = $get['listPhone'];
					$toll= $get['listToll'];




			//$numRowsVideo_sr = 0;
                ?>

This is the start of the new code and begins on line 248 and ends at line 406, the 405 line is "}" thanks again.

 

 

     
<?php
	include("inc/conn.php");



$query = mysql_query("SELECT * FROM categorylisting WHERE categoryid = $catID ORDER BY RAND()") or die("Query 1:".mysql_error());
while($detail = mysql_fetch_array($query)) {
//echo "<br>".$detail['listingid'];
$listingID = $detail['listingid'];
$query2 = mysql_query("SELECT * FROM listing WHERE listingID = $listingID AND listType='F' ORDER BY RAND()")  or die("Query 2: ".mysql_error());
while($get = mysql_fetch_array($query2)) 
{
$imageName = $get['logoImage'];
if($imageName == '')
{
$imageName = 'no_image.jpeg';
}
$listID = $get['listingID'];
$query2 = mysql_query("SELECT * FROM video WHERE listingID = '$listID' ")  or die("Query 3:".mysql_error());
$numRowsVideo_sr = mysql_num_rows($query2);
if($numRowsVideo_sr >= 1)
{
echo "<img src=''/>";
}
else
{
echo "NO IMAGE";
}

}
$phone = $get['listPhone'];

$toll= $get['listToll'];
//$numRowsVideo_sr = 0;
                ?>









	<!-- re-skining -->


	<form action="showFeatured.php?listID=<?php echo $listingID;?>&catID=<?php echo $catID;?>" method="post">
	<div style="border:1px solid gray;background-color: offwhite; width: 620px; height:120px;padding:9px;margin-bottom:5px;">

		<div style='float:left;width:140;height:120;padding-right:10px;border-right:1px solid gray;'>
		<a href="companyDetails.php?listID=<?php echo $listingID;?>"><img src='logoImages/<?php echo $imageName; ?>' border='0' height="100px" width="210px"></a>
		</div>





		<div style='float:left;width:325px;height:90;padding:5px;'>
			<div class="data" style="color:#2c5c97;font-family:Geneva, Arial, Helvetica, sans-serif;font-weight:bold;font-size:16px">
				<?php echo ucwords($get['listTitle']);?>
			</div>
			<div style="color:black;font-size:12px;">
				<?php $desc1 = ucfirst(stripslashes($get['listDesc1'])) . " ";
					  $desc1 .= ucfirst(stripslashes($get['listDesc2']));
					  if(strlen($desc1) > 100){
						  $strpos = strpos($desc1," ",100);
						  $desc1 = substr($desc1,0,$strpos);
						  echo $desc1."...";
					  } else {
						  echo $desc1."...";
					  }
					  ?>
			</div>	




		  <span><a href="companyDetails.php?listID=<?php echo $listingID;?>" style="color:#1F5291;font-Geneva, Arial, Helvetica, sans-serif;font-size:12px;text-decoration:none;">Read More >></a></span>
		</div>				

		<div style="float:left;width:200;height:27;border:0px solid gray;padding:8px 8px;">

			<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
			<span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/social.gif" width="20" height="26" border="0" title="Social Media"></a>
			</span>
			<?php } ?>

			<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
			<span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/whitepaper.gif" width="16" height="26" border="0" title="Case Study"></a>
			</span>
			<?php } ?>

			<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
			<span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/podcast.gif" width="17" height="26" border="0" title="Podcast"></a>
			</span>
			<?php } ?>

			<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
			<span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/contact.gif" width="19" height="26" border="0" title="Contact"></a>
			</span>
			<?php } ?>

			<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
			<span style="margin-right:5px;"><a href="companyDetails.php?listID=<?php echo $listingID;?>" target="_blank"><img src="images/video.gif" width="20" height="26" border="0" title="Video"></a>
			</span>
			<?php } ?>



		</div>
	</div>
	</form>
	<!-- re-skining eof-->
        <!-- <form action="showFeatured.php?listID=<?php echo $listingID;?>&catID=<?php echo $catID;?>" method="post">
          <tr> 
            <td width='138' height='50' rowspan='3' align='left' valign='middle' bgcolor='#ffffff' style='border-top: 1px solid black; border-left: 1px solid black; border-bottom: 1px solid black;'> 
              <div align='left'><img src='logoImages/<?php echo $imageName; ?>' border='0' style='max-width:120;max-height:60;'></div>
            <td width="255"  rowspan="3" valign="middle" style="border-top: 1px solid black; border-bottom: 1px solid black;"> 
              <div class="bodyTxtLGbold"><?php echo $get['listTitle'];?></div>
              <div class="bodyTxtColor"><?php echo stripslashes($get['listDesc1']) . " ". stripslashes($get['listDesc2']);?></div>
              <span id="userButton"> 
              <input name="sendToUrl" type="submit" value="<?php echo $get['url'];?>" class="buttonAsListingLink" onMouseOver="this.className='buttonAsListingLink_hover';"  onMouseOut="this.className='buttonAsListingLink';">
              </span> <input type="hidden" name='url' value="<?php echo $get['url'];?>"> 
            </td>
            <td width="31" height="28" valign="middle" style="border-top: 1px solid black"><div align="center"><img src="images/arrow.jpg" width="23" height="20"></div></td>
            <td width="200" valign="middle" style="border-top: 1px solid black; border-right: 1px solid black;"> 
              <span class="bodyTxt" ><a href="companyDetails.php?listID=<?php echo $listingID;?>" class="contactLink">Company 
              Details</a></span> </td>
          </tr>
        </form>
        <tr> 
          <td height="25" valign="top" ><div align="center"><img src="images/phone.jpg" width="23" height="20"></div></td>
          <td valign="middle" style="border-right: 1px solid black;"><span class="bodyTxt"><?php echo $phone;?></span></td>
        </tr> -->
        <!-- <tr> 
          <td height="44" valign="top" style="border-bottom: 1px solid black;"><div align="center"><img src="images/envelope.jpg" width="29" height="20"></div></td>
          <td valign="top" style="border-bottom: 1px solid black; border-right: 1px solid black;"><a href="mailto:<?php echo $get['listContactEmail'];?>" class="contactLink">Contact 
            Us</a><br/><br/> -->
	<!-- new code -->
	<!-- <span class="bodyTxt" style="margin-left:-40px">
	<?php if($get['listType'] == 'F' && $get['facebookURL'] != '') { ?>
	<a href="<?php echo $get['facebookURL']; ?>" target="_blank"><img src="images/facebook_icon.jpeg" title="www.facebook.com" alt="www.facebook.com" width="40" height="40" style="border-style: none"/></a>
	<?php } ?>
	<?php if($get['listType'] == 'F' && $get['twitterURL'] != '') { ?>
	<a href="<?php echo $get['twitterURL']; ?>" target="_blank"><img src="images/twitter_icon.png" title="By: TwitterButtons.com" alt="By: TwitterButtons.com" width="125" height="40" style="border-style: none"/></a>
	<?php } ?>		
	</span><br />	 -->
	<!-- new code end -->		
	<!-- </td>
        </tr>
        <tr> 
          <td height="6"></td>
          <td></td>
        </tr> -->
        <?php 
					}				
		}
?>

VERY sorry break this politeness but if you don't know how to deal with basic syntax errors I would seriously consider why you are programming at all.

 

The syntax is the first thing you should learn. Have you ever read a book or tutorials on php? It gives you the exact line where the problem may be.

 

I really am not saying this just to be hostile. Buy a decent php book or read up on the syntax and basic things like opening and closing brackets.

Archived

This topic is now archived and is closed to further replies.

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