2015-06-14 67 views
0

我有一段代碼從輸入字符串中讀取空格分隔的URL,並用iframe視頻替換它們。我想在它們上面插入iframe的編號。對iframe進行編號並顯示它

下面是我的代碼:

$result = $conn - > query($sql); 
if ($result - > num_rows > 0) { 
    // output data of each row 
    while ($row = $result - > fetch_assoc()) { 
     $nr = $row['nr']; 
     $playery = $row['player']; 
     $nrplayer = $nrplayer++; //old int (not used) 


     //////remove from string all iframes and replace tons spaces to only one before every url (That makes string more clear)////// 
     while (strpos($playery, ' ') !== false) { 
      $playery = str_replace(" ", "", $playery); 
     } 
     $playery = str_replace("http", " http", $playery); 
     $playery = str_replace('<iframewidth="420"height="315"src="', ' ', $playery); 
     $playery = str_replace('<iframesrc="', ' ', $playery); 


     ////////remove space of front and in the back //////// 
     while (mb_substr($playery, 0, 1) == " ") { 
      $playery = substr($playery, 1); 
     } 
     while (mb_substr($playery, 0, -1) == " ") { 
      echo "ok"; 
      $playery = substr($playery, 1); 
     } 


     //////add some ads on page//////// 
     require("ads.php"); 

     //////create iframes/////// 
     $nrr = 1; 
     echo $nrr."<br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='".str_replace(' ', "'></iframe> ".$nrr++." <br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='", $playery)."'>"; 

    } 
} 

我也只拿到了1行字符串的值$playery

$playery = "http://www.dailymotion.com/swf/x2t2mgd http://www.dailymotion.com/swf/x2t2ury https://drive.google.com/file/d/0B4DzuaeCNhcsdlhnLURaU0dBQTg/preview https://drive.google.com/file/d/0B6ORWPhIsXTCUVpMd0RNTFU3Wk0/preview https://drive.google.com/file/d/0B9x5eo1ro_r9UVZZSVJucGtJbGs/preview http://mp4upload.com/embed-t9ve1iot81so.html" 

我的代碼,我得到的是這樣的輸出:

1 
<video> 
    1 
<video> 
    1 
<video> 
    1 
<video> 

不過,我要得到這樣的:

1 
<video> 
    2 
<video> 
    3 
<video> 
    4 
<video> 
+0

後,如果用'echo'行應該是結果(這並不真正符合你的例子在上面給出) - 那麼你只在輸出'$ nrr =中使用這個變量1;'(當然它總是「1」)。你可能想使用'$ nrplayer'。但是你的代碼不太可讀,也許你需要提供更多的信息。 – Risadinha

+0

編輯。我添加了評論 – Doknes

+0

我改進了代碼的格式並重新措辭了句子以更好地解釋問題。格式不正確的代碼難以閱讀,並將可能回答您問題的人趕走。請查看[編輯幫助](http://stackoverflow.com/editing-help)獲取更多格式化信息。我也修改了標題。 – Harry

回答

0

做一些這樣的事情,我想你你把$ NRR = 1的循環中所以每次循環運行它變爲1

<?php 

$result = $conn->query($sql); 
if ($result->num_rows > 0) { 
    $nrr = 1; 
    // output data of each row 
    while ($row = $result->fetch_assoc()) { 
     $nr = $row['nr']; 
     $playery = $row['player']; 
     $nrplayer = $nrplayer++; 
     while (strpos($playery, ' ') !== false) { 
      $playery = str_replace(" ", "", $playery); 
     } 
     $playery = str_replace("http", " http", $playery); 
     $playery = str_replace('<iframewidth="420"height="315"src="', ' ', $playery); 
     $playery = str_replace('<iframesrc="', ' ', $playery); 
     while (mb_substr($playery, 0, 1) == " ") { 
      $playery = substr($playery, 1); 
     } 
     while (mb_substr($playery, 0, -1) == " ") { 
      echo "ok"; 
      $playery = substr($playery, 1); 
     } 
     require("ads.php");   
     echo $nrr . "<br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='" . str_replace(' ', "'></iframe> " . $nrr++ . " <br><br><br> <iframe allowfullscreen='1' width='800' height='500' src='", $playery) . "'>"; 
    } 
} 
?> 
+0

我沒有迴路,其唯一的回聲和替換; f – Doknes

+0

@Doknes可以請你告訴我們完整的代碼 –

+0

後編輯。 – Doknes

0

在PHP是$ var ++/$ var--表達式它被封裝在聲明後執行,因此$ NRR不會成爲遞增,直到你的迴音

$var = 1; 
echo $var++; //1 
echo $var; //2 
+0

那麼我該怎麼辦? :C – Doknes