Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2012
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

    isset to display variable

    I'd like to use an isset to display a variable if its not empty in my DB.... thought it would be easy yet I can't get it to work


    Code block with highlighted variable I'd like not shown if variable
    PHP Code:
    $image_path 
    is empty...

    PHP Code:
    <?php         
        
    echo '
    <div class="search-container">
    <ul class="searchresults">
        <li>
               <h3 class="fs"> '
    .(empty($idnumber) ? '&nbsp;' :' <a  href="familysupport-profile.php?id='.$idnumber.'">'.$org_name.'</a>').' </h3>         
            <p class="fs">
            p: '
    .(empty($tel) ? '&nbsp;' $tel).'<br/>
            e: '
    .(empty($email) ? '&nbsp;' $email).'<br/>';
            if (isset(
    $website)) 
            {
            echo 
    '<a target="_blank" href="'.$website.'">'.$website.'</a>';
            }echo
    '
            </p>

    // THE VARIABLE i'
    D LIKE ISSET//
           
    <img src="'.(empty($image_path) ? '&nbsp;' : $image_path).'" alt="Logo of '.(empty($org_name) ? '&nbsp;' : $org_name).'" height="75px" width="75px" style="float:right; margin:5px" />

    //END ISSET//

         
    </li>   
    </
    ul>
    </
    div>';  
       
    $i++;
      }    
    echo '
    </div>';
    }
    else
    {
    echo '
    <h2><class="bluelink" href="familysupport-form.php">Sorry but no results were found, try searching again.</a></h2>;
    }
    mysql_close($dbh);
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    isset and empty serve different purposes. Isset is designed to check the existence of a variable, and that the variable is not null. If it comes from a database, its highly probable that the variable will exist, and my be empty but not necessarily null.
    For your actual code, I wouldn't use a ternary like this inside of the image source at all. That will create a broken image if it doesn't have anything, so you are better off only using it if it does exist:
    PHP Code:
    if (!empty($image_path))
    {
        
    printf('<img src="%s" alt="Logo of %s" style="height: 75px; width:75px; float: right; margin: 5px" />'$image_path, !empty($org_name) ? $org_name '');

    That block cannot be used inside of a print though.

  • Users who have thanked Fou-Lu for this post:

    stephen_ (02-02-2012)

  • #3
    New Coder
    Join Date
    Jan 2012
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks for this yet i'm getting a t error

    PHP Code:
    <?php         
        
    echo '  
    <div class="search-container">
    <ul class="searchresults">
        <li>
               <h3 class="fs"> '
    .(empty($idnumber) ? '&nbsp;' :' <a  href="familysupport-profile.php?id='.$idnumber.'">'.$org_name.'</a>').' </h3>         
            <p class="fs">
            p: '
    .(empty($tel) ? '&nbsp;' $tel).'<br/>
            e: '
    .(empty($email) ? '&nbsp;' $email).'<br/>';
            if (isset(
    $website)) 
            {
            echo 
    '<a target="_blank" href="'.$website.'">'.$website.'</a>';
            }echo
    '
            </p>
            
            if (!empty($image_path))
    {
        printf('
    <img src="%s" alt="Logo of %s" style="height: 75px; width:75px; float: right; margin: 5px" />', $image_path, !empty($org_name) ? $org_name : '');
    }
            
            
            <img src="'
    .(empty($image_path) ? '&nbsp;' $image_path).'" alt="Logo of '.(empty($org_name) ? '&nbsp;' $org_name).'" height="75px" width="75px" style="float:right; margin:5px" /> 
         </li>   
    </ul>
    </div>'
    ;  
       
    $i++;
      }    
    echo 
    '
    </div>'
    ;
    }
    else
    {
    echo 
    '<h2><a class="bluelink" href="familysupport-form.php">Sorry but no results were found, try searching again.</a></h2>';
    }
    mysql_close($dbh);
    ?>

  • #4
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,285
    Thanks
    4
    Thanked 201 Times in 198 Posts
    You're getting the error because you're trying to mix HTML and PHP in the middle of an echo. Make things simpler and easier to understand by separating them.

    The closing brace ( } ) after the $i++ appears to be extraneous unless you are only showing us a portion of your code. There also seems to be a missing IF statement earlier in the code.

    What's posted below will not be valid for the reasons stated above but it should produce what you are expecting when used in the same context as what you posted.

    PHP Code:
    <div class="search-container">
    <ul class="searchresults">
        <li>
               <h3 class="fs">
    <?php echo (empty($idnumber) ? '&nbsp;' ' <a href="familysupport-profile.php?id=' $idnumber '">' $org_name '</a>'); ?>
    </h3>
            <p class="fs">
            p: <?php echo (empty($tel) ? '&nbsp;' $tel); ?>
            <br/>
            e: <?php echo (empty($email) ? '&nbsp;' $email); ?>
            <br/>
            <?php
            
    if (isset($website)) {
                echo 
    '<a target="_blank" href="' $website '">' $website.'</a>';
            }
            
    ?>
            </p>
            <?php
            
    if (!empty($image_path)) {
                echo 
    '<img src="' $image_path '" alt="Logo of ' . empty($org_name) ? ''$org_name '" style="height: 75px; width:75px; float: right; margin: 5px" />';
            }
            
    ?>
            <img src="<?php echo (empty($image_path) ? '&nbsp;' $image_path); ?>"
                 alt="Logo of <?php echo (empty($org_name) ? '&nbsp;' $org_name); ?>"
                 height="75px" width="75px" style="float:right; margin:5px" />
         </li>
    </ul>
    </div>
    <?php
    $i
    ++;
    }
    ?>
    </div>
    <?php
    } else {
    ?>
    <h2><a class="bluelink" href="familysupport-form.php">Sorry but no results were found, try searching again.</a></h2>
    <?php
    }
    mysql_close($dbh);
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    stephen_ (02-02-2012)

  • #5
    New Coder
    Join Date
    Jan 2012
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Thanks for this Dave, I think this is my main problem. I'm trying to add some jquery tabs to spilt up the content and these are also not working as expected.

    I've attached the code of the whole page, Id appriciate any advice that could smarten up the way the PHP is structured....

    PHP Code:
    <?php
    $dbh 
    mysql_connect ('xxx''xxx''xxx');
    @
    mysql_select_db ('xxx') or die( "Unable to select database");
    ?>
    <!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>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" ></script>
    <script type="text/javascript" src="scripts/jquery.easing.1.3.js"></script>
    <script type="text/javascript" src="scripts/jquery.jBreadCrumb.1.1.js"></script>
    <script type="text/javascript" src="scripts/textsize.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
        <script>
      $(document).ready(function() {
        $("#tabs").tabs();
      });
      </script>
         
    <script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
    <script type="text/javascript">
     var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",
     new google.maps.Size(32, 32), new google.maps.Point(0, 0),
     new google.maps.Point(16, 32));
     var center = null;
     var map = null;
     var currentPopup;
     var bounds = new google.maps.LatLngBounds();
     function addMarker(lat, lng, info) {
     var pt = new google.maps.LatLng(lat, lng);
     bounds.extend(pt);
     var marker = new google.maps.Marker({
     position: pt,
     icon: icon,
     map: map
     });
     var popup = new google.maps.InfoWindow({
     content: info,
     maxWidth: 300
     });
     google.maps.event.addListener(marker, "click", function() {
     if (currentPopup != null) {
     currentPopup.close();
     currentPopup = null;
     }
     popup.open(map, marker);
     currentPopup = popup;
     });
     google.maps.event.addListener(popup, "closeclick", function() {
     currentPopup.close();
     currentPopup = null;
     });
     }
     function initMap() {
     map = new google.maps.Map(document.getElementById("map"), {
     center: new google.maps.LatLng(53.994709, -7.360878),
     zoom: 15,
     mapTypeId: google.maps.MapTypeId.ROADMAP,
     mapTypeControl: false,
     mapTypeControlOptions: {
     style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
     },
     navigationControl: true,
     navigationControlOptions: {
     style: google.maps.NavigationControlStyle.SMALL
     }
     });
     <?
     $query 
    mysql_query("xxxx");

     while (
    $row mysql_fetch_array($query)){    
     
    $org_name=$row['org_name'];
     
    $lat=$row['lat'];
     
    $long=$row['long'];
     
    $org_address=$row['org_address'];
     echo (
    "addMarker($lat, $long,'<b>$org_name</b><br/>$org_address');\n");
     }
     
    ?>
     center = bounds.getCenter();
     map.fitBounds(bounds);

     }
     </script>
         <style type="text/css">
            #map { width: 700px; height: 500px; border: 0px; padding: 10px; }
     </style>
    </head>
      
    <body onload="initMap()" style="margin:0px; border:0px; padding:0px;">

    <!--Main Content-->
    <div style="width:100%">
       
    <?php

    $org_name
    =$_POST['org_name'];
    $area=$_POST['area'];
    $services=$_POST['services'];
    $hardiker=$_POST['hardiker'];

        
    //organisation name feedback
    if (isset ($org_name))
    {
    echo 
    '<h3>Organisation Name: <span style="color: #000; font-weight:bold">'.$org_name.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched for all organisation names.</h3>';
    }
        
    //service type feedback
    if (isset ($services))
    {
    echo 
    '<h3>Services: <span style="color: #000; font-weight:bold">'.$services.'</span></h3>';
    }
    else
    {
    echo 
    '<h3 >You have searched for all services.</h3>';
    }
        
    //area selected feedback
    if (isset ($area))
    {
    echo 
    '<h3>Area: <span style="color: #000; font-weight:bold">'.$area.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched all areas.</h3>';
    }
        
    //hardiker feedback
    if (isset ($hardiker))
    {
    echo 
    '<h3>Hardiker: <span style="color: #000; font-weight:bold">'.$hardiker.'</span></h3>';
    }
    else
    {
    echo 
    '<h3>You have searched all hardiker levels.</h3>';
    }

    $sql_result"SELECT * FROM family_support WHERE org_name LIKE '%$org_name%'";

    if (
    $area != 'All') {
    $sql_result .=" AND area LIKE '%$area%'";
    }
    if (
    $services != 'All') {
    $sql_result .=" AND services LIKE '%$services%'";
    }
    if (
    $hardiker != 'All') {
    $sql_result .=" AND hardiker = '$hardiker'";
    }

    $sql_result .=" ORDER BY org_name";
    //echo $sql_result;
    $result =  mysql_query($sql_result$dbh) or die ('Database currently being updated, please try again later!'); 
    $num=mysql_numrows($result);

    //get the count 
    echo '<h3><span style="color: #000; font-weight:bold">Total search results: </span>'.$num.'</h3><br/>

     
        <div class="fsmaplink-container">
                <p class="fsmaplink-head"><a class="whitelink" href="#map">Show locations on Map</a></p>
        </div>

        <div id="tabs" style="width:800px">
        <ul>
            <li><a href="#fragment-1"><span>One</span></a></li>
            <li><a href="#fragment-2"><span>Two</span></a></li>
            <li><a href="#fragment-3"><span>Three</span></a></li>
        </ul>
    '
    ;
    if (
    $num 0)
    {

    //quries used on results table;
         
    $i=0;  
         while (
    $i $num) {
        
    $idnumber mysql_result($result$i,'id');
        
    $org_name     mysql_result($result$i'org_name');
        
    $org_address  mysql_result($result$i'org_address');
        
    $tel mysql_result($result$i'tel');
        
    $email mysql_result($result$i'email');
        
    $website mysql_result($result$i'website');
        
    $org_type mysql_result($result$i'org_type');
        
    $image_path mysql_result($result$i'image_path'); 
        
    ?> 

    <!------search feedback finshed----------------->
    <!--------------Pagination---------------------->

    <div id="fragment-1"> 

    <?php            
    echo '  
    <div class="search-container">
        <ul class="searchresults">
            <li>
                   <h3 class="fs"> '
    .(empty($idnumber) ? '&nbsp;' :' <a  href="familysupport-profile.php?id='.$idnumber.'">'.$org_name.'</a>').' </h3>         
                <p class="fs">
                p: '
    .(empty($tel) ? '&nbsp;' $tel).'<br/>
                e: '
    .(empty($email) ? '&nbsp;' $email).'<br/>';
                    if (isset(
    $website)) 
                    {
                    echo 
    '<a target="_blank" href="'.$website.'">'.$website.'</a>';
                    }echo
    '
               
                    <img src="'
    .(empty($image_path) ? '&nbsp;' $image_path).'" alt="Logo of '.(empty($org_name) ? '&nbsp;' $org_name).'" height="75px" width="75px" />
                     
               </p>
            </li>   
        </ul>
    </div>'
    ;    
    $i++;
      }    
    echo 
    '
    </div>'
    ;
    }
    else
    {
    echo 
    '<h2><a class="bluelink" href="form.php">Sorry but no results were found, try searching again.</a></h2>';
    }
    mysql_close($dbh);
    ?>    
           
    </div>

    <div id="fragment-2">
    <!--Map-->
    <h3 class="fs" id="map">Map</h3>
    <div id="map" style="width:100%; height:65%"></div>
    </div>
        
    <div id="fragment-3">
    <h3 class="fs" id="feedback">Feedback</h3>    
        <div id="contact-area">
                    <br />
                    <form action="sendmail.php" method="POST">
                    <label for="Name">Name:</label>
                    <input type="text" name="Name" id="Name" class="required"/>
        
                    <label for="Email">Email:</label>
                    <input type="text" name="Email" id="Email" class="required" />
                    
                    <label for="Message">Message:</label><br />
                    <textarea name="Message" rows="20" cols="20" id="Message"></textarea>
                    
                    <input type="reset" name="clear" value="Clear" class="reset-button" />
                    <input type="submit" name="submit" value="Send" class="submit-button" />
                </form>
            
            <div style="clear: both"></div>
        </div>
    </div>
      
    <!-- content END -->
    </div>

    <!--Main Content End-->
    </td>
    </tr>
    </table>            

    </div>

    <script type="text/javascript">
                jQuery(document).ready(function()
                {
                    jQuery("#breadCrumb2").jBreadCrumb();
                })
            </script>
    </body>
    </html>
    fragment-2 & fragment-3 divs are also not showing the content (map in 2 and contact form in 3)
    Last edited by stephen_; 02-02-2012 at 01:39 PM. Reason: Added explantion


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •