2015-09-29 91 views
0

我想在每次SQL數據庫中的行都經過並打印到網頁上時將變量傳遞到數組中。從數組中的SQL中捕獲PHP變量並輸出到JavaScript變量

然後每次單擊頁面上的項目時,數據庫中的字段中的某個變量就會被捕獲到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中允許的與渠道/ 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> 

說我拿到打印的頁面兩個通道,但是當我點擊它們中的變量是唯一持有輸入最後輸出的頻道的名稱。因此只能打開最後一個輸出通道窗口。

我試圖達到的效果就像在http://onperiscope.com/上發生的情況,爲您提供更好的主意。

我意識到我可能沒有提供足夠的信息,所以請問,我會盡量提供儘可能多的信息。

謝謝

+0

現在的問題在於調用數組。當你點擊一個流時,該數組會被調用,但它會輸出整個數組,而不僅僅是點擊的流。 –

回答

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']); 

我也建議尋找,而不是使用過時的MySQL的函數

EDIT1

在回答您的評論mysqli的功能,也許是因爲你已經出現包括對channel_title網址在你的href值中,最容易做的事情就是拋棄數組,並將點擊的對象傳遞給你的函數,然後從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. */ 
     } 
+0

啊,好吧。這已經修復了數組,並允許將變量添加到數組中。該數組被轉換爲JavaScript變量,其中 'var theChannel = <?php echo(json_encode($ chName)); ?>' 但是,當我單擊流的任一圖像時,變量將顯示兩個流名稱,而不僅僅是我單擊的那個。 我認爲這需要在echo的標籤中捕獲,以便它知道哪個var與哪個流圖像相關聯。我不確定這將如何工作,雖然... –

+0

看看你的編輯,它已經工作,是一個很好的解決方案,謝謝! 我沒真正嘗試是 '的onclick =「openStreamPopup(\ '' $行[ 'CHANNEL_TITLE'。 '\');' 並在javascript '函數openStreamPopup(theChannel){ .. 。 windowObjectReference = window.open( 「信道/」 + theChannel, 「窗口」, 「調整大小,滾動條,狀態」); ... };' 這也似乎工作,但您的解決方案不需要陣列來工作,所以可能是最好的方式去做。謝謝你的幫助! 編輯:對不起,我將命名從openFFPromotionPopup更改爲openStreamPopup。 –