【问题标题】:Catching PHP variable from SQL in an array and outputting to JavaScript variable从数组中的 SQL 捕获 PHP 变量并输出到 JavaScript 变量
【发布时间】: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


【解决方案1】:

我不会假装已经完全调查了您正在尝试做的事情,但从您所描述的情况来看,您的问题只是您正在打电话

//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. */
        }

【讨论】:

  • 啊,好吧,太好了。这已经修复了数组并允许将变量添加到数组中。该数组使用var theChannel = &lt;?php echo(json_encode($chName)); ?&gt; 转换为JavaScript 变量但是,当我单击流的任一图像时,该变量会显示两个流名称,而不仅仅是我单击的那个。我认为这需要在 echo 的 标记中捕获,以便它知道哪个 var 与哪个流图像相关联。我不确定这将如何工作......
  • @JoeyThomas 我添加了一个编辑,希望它能满足您的需要
  • 查看您的编辑,它有效,是一个很好的解决方案,谢谢!我实际尝试的是onclick="openStreamPopup(\'' . $row['channel_title'] . '\'); 和javascript function openStreamPopup(theChannel) { ... windowObjectReference = window.open("channels/"+theChannel, "window", "resizable,scrollbars,status"); ... }; ,这似乎也有效。但是您的解决方案不需要数组即可工作,因此可能是最好的方法。十分感谢你的帮助!编辑:抱歉,我将名称从 openFFPromotionPopup 更改为 openStreamPopup。