Number the iframes and display it

52 views Asked by At

I have a piece of code that reads space separated URLs from an input string and replaces them with iframe video. I want to insert the number of the iframe above each of them.

Below is my code:

$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)."'>";

    }
}

I have got only 1 row and the value of string $playery is

$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"

With my code, the output that I get is something like this:

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

However, I want to get this:

   1
<video>
   2
<video>
   3
<video>
   4
<video>
2

There are 2 answers

1
user1718074 On BEST ANSWER

in php $var++/$var-- expressions are executed after the statement it is encapsulated in, so $nrr will not become incremented until after your echo

$var = 1;
echo $var++; //1
echo $var; //2
2
Faiz Rasool On

Do some thing like this, i think you you are putting $nrr = 1 inside your loop so every time loop run it become 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) . "'>";
    }
}
?>