【发布时间】:2023-05-15 15:26:01
【问题描述】:
每次 SQL 数据库中的行通过并打印到网页上时,我都会尝试将变量传递到数组中。
然后,每次单击页面上的项目时,数据库中字段中的某个变量都会被捕获在 JavaScript 变量中,因此会弹出一个新窗口,其中 SQL 字段“channel_name”作为 url。
我相信我快到了,但 JavaScript 变量只保存最后一个 SQL 'channel_name' 中的变量。
我希望我是有道理的......
这是我的代码,它通过数据库并打印出每个元素:
$chResult = mysql_query($chSQL);
if ($chResult) {
$chNum = mysql_num_rows($chResult);
if ($chNum>0) {
while($row = mysql_fetch_array($chResult)) {
if ($row['is_live']=="1") {
$whatIsLive = "true";
} else {
$whatIsLive = "false";
}
//CREATE THE ARRAY
$chName = array();
//ADD ARRAY VARS FROM CHANNEL_TITLE FIELD
$chName[] = ($row['channel_title']);
//PRINT CHANNEL INFORMATION TO PAGE
echo
'<li id="'.$row['channel_id'].'" class="list-group-item col-xs-12 col-sm-6 col-md-4 col-lg-3">
<div class="item">
<div class="item-head">
<div class="badges">';
if ($row['is_live']=="1") {
echo '<span class="badge-live">Live</span>';
}
echo '
</div>
</div>
<div class="item-image">
<a href="'.SERVERPATH.'channels/'.urlencode($row['channel_title']).'"
//TARGET FOR JAVASCRIPT POPUP WINDOW
target="PromoteFirefoxWindowName"
onclick="openFFPromotionPopup();
return false;"
data-islive="'.$whatIsLive.'"
title="'.$row['channel_title'].'">';
$activeSSImage = 'userData/'.$row['user_id'].'/channelImages/ss_'.$row['channel_id'].'_t.jpg';
$defaultSSImage = 'images/ss_default.jpg';
if (file_exists($activeSSImage)) {
echo '<img src="'.$activeSSImage.'?rand='.rand(0, 99999999).'" alt="'.$row['channel_title'].'" width="250" height="200">';
} else {
echo '<img src="'.$defaultSSImage.'" alt="'.$row['channel_title'].'" width="250" height="200">';
}
echo '
<span class="image-cover"></span>
<span class="play-icon"></span>
</a>
</div>
</div>
</div>
</li>';
}
} else {
echo '';
}
} else {
echo '';
}
然后在 JavaScript 中捕获该变量,以允许带有频道/频道名称的链接在新窗口中弹出:
<script type="text/javascript">
var theChannel = <?php echo(json_encode($chName)); ?>;
var windowObjectReference = null; // global variable
function openFFPromotionPopup() {
if(windowObjectReference == null || windowObjectReference.closed)
/* if the pointer to the window object in memory does not exist
or if such pointer exists but the window was closed */
{
windowObjectReference = window.open("channels/"+theChannel,
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
else
{
windowObjectReference.focus();
/* else the window reference must exist and the window
is not closed; therefore, we can bring it back on top of any other
window with the focus() method. There would be no need to re-create
the window or to reload the referenced resource. */
};
console.log( "function complete" );
console.log( theChannel );
}
</script>
假设我在页面上打印了两个通道,但是当我单击其中任何一个时,变量仅保存最后输出的通道的名称。因此只打开最后一个输出通道窗口。
我想要达到的效果就像http://onperiscope.com/ 上发生的那样,给你一个更好的主意。
我知道我可能没有提供足够的信息,所以请询问,我会尽力提供尽可能多的信息。
谢谢
【问题讨论】:
-
现在的问题在于调用数组。当您单击流时会调用该数组,但它会输出整个数组,而不仅仅是您单击的流。
标签: javascript php jquery arrays sql-server