如果变量为空,则不显示字符串

时间:2012-03-06 01:19:18

标签: php javascript string variables

我正在使用php变量作为youtube的json链接。未定义变量时,将返回精选视频。当您按下按钮,输入文本,变量已定义并且全部有效。如果变量为空,没有返回任何字符串,它是如何完成的。但是当变量甚至等于空格时!显示字符串。这是代码:

<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();


?>

该链接包含变量$query
再次感谢所有人!

1 个答案:

答案 0 :(得分:1)

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