Transcription javascript calculations for PHP

Asked

Viewed 55 times

4

How can I transcript this code in javascript for PHP ?

var temp = "<div class='brick' style='width:{width}px;'><img src='i/photo/{index}.jpg' width='100%'></div>";
var w = 1, h = 1, html = '', limitItem = 49;

for (var i = 0; i < limitItem; ++i) {
    w = 1 + 3 * Math.random() << 0;
    html += temp.replace(/\{width\}/g, w*150).replace("{index}", i + 1);
}

Sorry if I’m asking a kind of simple question, but I’m a layman in PHP.

1 answer

6


Follows something like:

<?php
    $html = '';
    $limitItem = 49;
    for ($i = 0; $i < $limitItem; ++$i ) {
        $w = rand( 1, 4 ); // Confira se é de 1 a 4 mesmo que deseja
        $width = $w * 150;
        $index = $i + 1;
        $html += '<div class="brick" style="width:' . $width . 'px">';
        $html += '<img src="i/photo/' . $index . '.jpg" width="100%"></div>';
    }
    //se quiser conferir o resultado:
    echo $html;
?>
  • 1

    You can optimize and kill some variables, but since you did not give details, I did similar to the original.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.