Ben Thomson
Ben Thomson

Reputation: 558

Don't show string if variable empty

I'm using a php variable as a json link for youtube. When the variable is not defined, it returns the featured videos. When you press a button, enter text, the variable is defined and all works. How is it done when the variable is empty, there is no string returned. But when the variable even equals a space! The string is displayed. Here's the code:

<style>
.titlec {
  font-size: small;
}
ul.videos li {
display: inline;
  float: left;
  width: 1em;
  margin-bottom: 1em;
}
ul.videos
{
  margin-bottom: 1em;
  padding-left : 0em;
  margin-left: 0em;
  list-style: none;
}
</style>
<script type="text/javascript">
function loadVideo(playerUrl, autoplay) {
  swfobject.embedSWF(
      playerUrl + '&rel=1&border=0&fs=1&autoplay=' + 
      (autoplay?1:0), 'player', '290', '250', '9.0.0', false, 
      false, {allowfullscreen: 'true'});
}




function showMyVideos2(data) {
  var feed = data.feed;
  var entries = feed.entry || [];
  var html = ['<ul class="videos">'];
  for (var i = 0; i < entries.length; i++) {
    var entry = entries[i];
    var title = entry.title.$t.substr(0, 1);
    var thumbnailUrl = entries[i].media$group.media$thumbnail[0].url;
    var playerUrl = entries[i].media$group.media$content[0].url;
    html.push('<li onclick="loadVideo(\'', playerUrl, '\', true)">',
              '<span class="titlec">', title, '...</span><img src="', 
              thumbnailUrl, '" width="97.5" height="72.75"/>', '</span></li>');
  }
  html.push('</ul>');
  document.getElementById('videos2').innerHTML = html.join('');
  if (entries.length > 0) {
    loadVideo(entries[0].media$group.media$content[0].url, false);
  }
}
</script>
<div id="youtube" style="width: 20em; height: 180px; float: left;">
    <object id="player"></object>
</div>
<div id="videos2"></div>
<script 
    type="text/javascript" 
    src="http://gdata.youtube.com/feeds/api/videos?q=<?php echo $query ?>&alt=json-in-script&callback=showMyVideos2&max-results=2&format=5">
</script> 
 </div>

php

<div id="dialog2" class="window">
<div id = "song">
<h1 style="color:white"> What Song Are You<br> Listening To? </h1>
</div>
<?php


echo " <div id = 'search'>";
echo "<form name='search' method='post' action='#dialog'>\n";

    echo "<input type='text' name='something'>\n";
    echo htmlspecialchars($_POST['something'])."\n";
    echo "</input>";
    echo "<a name='search' href='javaScript:document.search.submit()'>Share</a> \n";
    echo " </div>";
    echo " </form>";
?>
</div>
<!-- End of Sticky Note -->
<div id = "dialog" >
<?php
if(isset($_POST['submit'])) {
}
?>

<?php


require_once 'tinysong.php';

$api_key = '67563c4f676562e15f78dfa0121a5cce';

$query = $_POST['something'];

$tinysong = new Tinysong($api_key);


$result = $tinysong
            ->search($query)
            ->execute();


?>

The link contains the variable $query
Thanks again all!

Upvotes: 0

Views: 908

Answers (1)

deceze
deceze

Reputation: 522155

if (trim($var)) {
    // the variable is *not* empty, even with spaces removed
}

Upvotes: 1

Related Questions