每次遍历 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 中捕获该变量,以允许在新窗口中弹出带有channels/channel_name 的链接:
<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>
假设我将两个 channel 打印到页面上,但是当我单击其中任何一个时,变量仅保存最后输出的 channel 的名称。因此只打开最后一个输出 channel 窗口。
我想要达到的效果就像 http://onperiscope.com/ 上发生的那样为您提供更好的想法。
我意识到我可能没有提供足够的信息,因此请询问,我会尽力提供尽可能多的信息。
谢谢
最佳答案
我不会假装已经完全调查了您想要做的事情,但从您所描述的情况来看,您的问题只是您正在打电话
//CREATE THE ARRAY
$chName = array();
对于每一行。也许你的意思是。
if($chNum>0){
//CREATE THE ARRAY
$chName = array();
while($row = mysql_fetch_array($chResult)) {
//some code
//ADD ARRAY VARS FROM CHANNEL_TITLE FIELD
$chName[] = ($row['channel_title']);
我还建议使用 mysqli 函数而不是已弃用的 mysql 函数
编辑1
为了回应您的评论,也许由于您似乎已经在 href 值中包含了 channel_title
的 URL,所以最简单的方法就是放弃数组并将单击的对象传递给您的函数,然后从 openFFPromotionPopup
函数中访问此属性。因此,将您的 PHP 更改为;
<a href="'.SERVERPATH.'channels/'.urlencode($row['channel_title']).'"
//TARGET FOR JAVASCRIPT POPUP WINDOW
target="PromoteFirefoxWindowName"
onclick="openFFPromotionPopup(this);return false;"
data-islive="'.$whatIsLive.'"
title="'.$row['channel_title'].'">';
然后你用 javascript 来;
function openFFPromotionPopup(elem) {
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(elem.href,
"PromoteFirefoxWindowName", "resizable,scrollbars,status");
/* then create it. The new window will be created and
will be brought on top of any other window. */
}
关于javascript - 从数组中的 SQL 捕获 PHP 变量并输出到 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32845927/