Jump to content

Help Please!! Show region if database - table - field not empty


emediastudios

Recommended Posts

I have a form that allows my client to upload up to (9) images to a directory and records the image names in a sql database.

The details go into a table called employees and my database name is gcpropery.

 

This works fine.

 

Problem is, that if my client only has 5 images, the other fields (photo6, photo7...etc..) get no input and are empty.

 

My show details page shows all the images and details that are drawn from form varibles that are in my browse records page.

It has 9 dynamic image fields that show the images, if there is no record in any of these fields i get a blank box with no image, this makes sense.

 

how do i not show that region if the field is empty in that record?

 

This is the code for the image fields

 

 <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo4']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo4']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

 

How do i alter this to only show this image if (photo4) exsists and the field is not empty?

 

I was told that if i altered it to

 

<?php

if (!empty($db_var)){
?>
<a href="images/<?php echo $_POST['photo4']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo4']; ?>" alt="image" width="150" border="0" />

<?php } ?>

 

But it doesnt show the image at all, even if there is a record.

 

 

Link to comment
Share on other sites

well i would assume you are using some sort of loop i would use something like this

<?php

if (isset($_POST['photo4']){
?>
<a href="images/<?php echo $_POST['photo4']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo4']; ?>" alt="image" width="150" border="0" />

<?php } ?>

Link to comment
Share on other sites

Well i for one am confused about your code. If you are retrieving images that are stored in the database, why do you get the name from the $_POST array?

 

And secondly, i think you should take a look at the idea of database normalization:

http://en.wikipedia.org/wiki/Database_normalization

 

You would be better having a seprate table that stores the image names, which is linked to your employees table by an employee id.

Link to comment
Share on other sites

I am a newbe and experimenting.

Its hard for me to explain.

 

I have a file that repeats region, (browse properties) the data is drawn fron the employees table of my gcproperty database and shows all records.

 

I have hidden fields in the form that i have named to match the fields in my table

code below

 

<?php require_once('Connections/gcproperty.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM employees";
$Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>results</title>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
color: #666666;
}
.style3 {color: #666666}
.style5 {font-size: 12px}
.style6 {color: #FF0000}
-->
</style>
</head>

<body><br />
<?php if ($totalRows_Recordset1 == 0) { // Show if recordset empty ?>
  <table width="600" border="0" align="center">
    <tr>
      <td><div align="center" class="style6">There are no current properties to display</div></td>
    </tr>
  </table>
  <?php } // Show if recordset empty ?>
There are currently <span class="style6"><?php echo $totalRows_Recordset1 ?></span> properties listed in the GCProperty database<br />

<?php do { ?><?php if ($totalRows_Recordset1 > 0) { // Show if recordset not empty ?>
    <form action="full_details.php" method="post" enctype="multipart/form-data" name="properties" target="_self" id="properties">
      <table width="600" align="center" bgcolor="#F1F1F1">
      <tr>
        <td width="437" align="left" valign="top" bordercolor="#000000" bgcolor="#FFFFFF"><span class="style1"><u><?php echo $row_Recordset1['name']; ?></u></span><span class="style3">
        <label>
        <input name="name" type="hidden" id="name" value="<?php echo $row_Recordset1['name']; ?>" />
        </label>
        <label></label>
        </span> <span class="style3"><br />
          <span class="style5"><?php echo $row_Recordset1['price']; ?>
          <label></label>
          <label>
          <input name="price" type="hidden" id="price" value="<?php echo $row_Recordset1['price']; ?>" />
          </label>
          <br />
          <?php echo $row_Recordset1['suburb']; ?>
          <label></label>
          <label>
          <input name="suburb" type="hidden" id="suburb" value="<?php echo $row_Recordset1['suburb']; ?>" />
          </label>
          <br />
          </span></span><span class="style3"><?php echo $row_Recordset1['content']; ?>
          <label>
          <input name="content" type="hidden" id="content" value="<?php echo $row_Recordset1['content']; ?>" />
          <input name="content2" type="hidden" id="content" value="<?php echo $row_Recordset1['content2']; ?>" /><br />
<input name="photo" type="hidden" id="photo" value="<?php echo $row_Recordset1['photo']; ?>" />
          <br />
<input name="photo2" type="hidden" id="photo2" value="<?php echo $row_Recordset1['photo2']; ?>" />
          <br />
</label>
          

          <label>
          <input name="photo3" type="hidden" id="photo3" value="<?php echo $row_Recordset1['photo3']; ?>" />
          </label>
          <label>
          <input name="photo4" type="hidden" id="photo4" value="<?php echo $row_Recordset1['photo4']; ?>" />
          </label>
          <label>
          <input name="photo5" type="hidden" id="photo5" value="<?php echo $row_Recordset1['photo5']; ?>" />
          </label>
          <label>
          <input name="photo6" type="hidden" id="photo6" value="<?php echo $row_Recordset1['photo6']; ?>" />
          </label>
          </span>
          <label>
          <input name="photo7" type="hidden" id="photo7" value="<?php echo $row_Recordset1['photo7']; ?>" />
          <input name="photo8" type="hidden" id="photo8" value="<?php echo $row_Recordset1['photo8']; ?>" />
          <input name="photo9" type="hidden" id="photo9" value="<?php echo $row_Recordset1['photo9']; ?>" />
          </label></td>
        <td width="151"><div align="center"><a href="images/<?php echo $row_Recordset1['photo']; ?>" target="_blank"><img src="images/<?php echo $row_Recordset1['photo']; ?>" alt="image" width="150" border="0" /></a></div></td>
      </tr>
      
      
      <tr>
        <td colspan="2"></td>
      </tr>
      
      <tr>
        <td height="23" colspan="2" bgcolor="#666666"><a href="full_details.php" target="_self">
          <label>
          <input type="submit" name="submit" id="submit" value="more info" />
          </label>
        </a></td>
      </tr>
    </table>
    </form>
    <?php } // Show if recordset not empty ?>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?></body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

When you click the more info button it posts the form varibles into the full_details.php file which looks like this

 

<?php require_once('Connections/gcproperty.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_gcproperty, $gcproperty);
$query_Recordset1 = "SELECT * FROM employees";
$Recordset1 = mysql_query($query_Recordset1, $gcproperty) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GCProperty - <?php echo $row_Recordset1['name']; ?></title>
<style type="text/css">
<!--
.style1 {
font-size: 18px;
font-weight: bold;
color: #666666;
}
.style3 {color: #666666}
.style5 {font-size: 12px}
.style6 {color: #FF0000}
-->
</style>
</head>

<body>
<form action="show2.php" method="get" enctype="application/x-www-form-urlencoded" name="fav" target="_self" id="fav">
  <br />
  <table width="640" border="0" align="center">
    <tr>
      <td width="101"> </td>
      <td width="41"> </td>
      <td width="350"> </td>
      <td width="40"><div align="left"><script type="text/javascript" src="favlink.js">
</script>
</div></td>
      <td width="40"><div align="left"><a href="javascript:window.print()"><img src="images/print.png" alt="print this page" border="0" /></a> </div></td>
      <td width="42"><div align="left"><a href="sendpage.php" target="_self"><img src="images/send.png" alt="send to friend" width="16" height="16" border="0" /></a></div></td>
    </tr>
  </table>
  <table width="645" align="center">
    <tr>
      <td colspan="2" align="left" valign="top" bordercolor="#000000" bgcolor="#F1F1F1"><div align="left"><span class="style1"><u> <?php echo $_POST['name']; ?></u></span> </div>
      <div align="right"></div></td>
    </tr>
    <tr>
      <td width="173" align="left" valign="top" bordercolor="#000000" bgcolor="#FFFFFF"><table width="156" border="0">
          <tr>
            <td width="150" height="403" valign="top">
           <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo2']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo2']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

              <br />
          <table width="150" border="0">
  <tr>
   <td><a href="images/<?php echo $_POST['photo3']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo3']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

            <br />
           <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo4']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo4']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>
<br />

            <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo5']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo5']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

            <br />
            <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo6']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo6']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

            <br />
            <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo7']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo7']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

            <br />
            <table width="150" border="0">
  <tr>
    <td><a href="images/<?php echo $_POST['photo8']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo8']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>

            <br />
           <table width="150" border="0">
  <tr>
    <td> <a href="images/<?php echo $_POST['photo9']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo9']; ?>" alt="image" width="150" border="0" /></a><br /></td>
  </tr>
</table>
</td>
          </tr>
        </table>
       </td>
      <td width="460" valign="top"><table width="200" border="0">
          <tr>
            <td><a href="images/<?php echo $_POST['photo']; ?>" target="_blank"><img src="images/<?php echo $_POST['photo']; ?>" alt="image" width="450" border="0" /></a></td>
          </tr>
      </table>
      <br />
      <table width="459" border="0">
        <tr>
          <td bgcolor="#F1F1F1"><span class="style5"><?php echo $_POST['price']; ?></span></td>
          </tr>
        <tr>
          <td><span class="style5"><?php echo $_POST['suburb']; ?></span></td>
          </tr>
        <tr>
          <td height="270" valign="top"><span class="style3"><?php echo $_POST['content']; ?><br />
            <hr width="300" class="style5" />
          </span><span class="style3"><?php echo $_POST['content2']; ?></span></td>
          </tr>
      </table>
      <div align="right"></div></td>
    </tr>
    <tr>
      <td colspan="2" align="left" valign="top" bordercolor="#000000" bgcolor="#F1F1F1"> </td>
    </tr>
    
    
    <tr>
      <td colspan="2"></td>
    </tr>
  </table>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

 

So i guess it doesnt retrive the info from the database but rather the form varibles that where populated from the database in the previous file.

I did it this way as i had no idea on how to get the current record to display in my full_details file and it worked..

 

??? I,m confusing myself now.........  :D

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.