PHP random DIV with css question - php

I tried to use PHP to make DIV random positions. we can look each 100px as a unit, type one small div is 1*1 ,type two small div is 1*2, type three small div is 2*1. All divs only allow show in a big div box 10*6. Here is my code.
<?php
function DIV1()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:100px;background:#ff0 none;\"></div>";
}
function DIV2()
{
$choose1 = array("0","100","200","300","400","500","600","700","800","900");
$choose = array("0","100","200","300","400");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:100px;height:200px;background:#f00 none;\"></div>";
}
function DIV3()
{
$choose1 = array("0","100","200","300","400","500","600","700","800");
$choose = array("0","100","200","300","400","500");
$rand_keys = array_rand($choose, 1);
$rand_keys1 = array_rand($choose1, 1);
echo "<div style=\"position:absolute;top:".$choose[$rand_keys]."px;left:".$choose1[$rand_keys1]."px;width:200px;height:100px;background:#00f none;\"></div>";
}
echo '<div style="width:1000px;height:600px;">';
$sizes = array();
for($i = 0; $i < 15; $i ++) $sizes[] = DIV1($row);
for($i = 0; $i < 10; $i ++) $sizes[] = DIV2($row);
for($i = 0; $i < 5; $i ++) $sizes[] = DIV3($row);
shuffle($sizes);
for($i = 0; $i < 30; $i ++) echo $sizes[$i];
echo '</div>';
?>
I still have some css questions. in my code, I make postion:absolute and top left setting, but some divs will overlapping. how to solve it? Thanks.

1) You should use "return" instead of "echo" in the end line of 3 div functions so that array sizes can have any effect
2) Your problem is not standard, it is a little bit mathematical challenge, you must do "remembering" of positions and shuffling by hand
3) can this below code be your solution ? ( test here )
<?php
function divHtml($dtype, $x, $y) //$dtype: (1)=1x1yellow, (2)=1x2, (3)=2x1, (4)=1x1white
{ $ww =($dtype ==3) ?'200px':'100px'; $hh =($dtype==2)?'200px':'100px'; $bgcols =array('#ff0', '#f00', '#00f', '#fff');
$xx =($x*100) .'px'; $yy =($y*100) .'px'; $bgc =$bgcols[$dtype-1];
return "<div style='position:absolute; width:$ww;height:$hh; left:$xx;top:$yy; background:$bgc;'> $dtype </div>";
}
$divs =array(); //real divs array (html inside)
$cells =array(); //logical filled/notFilled (0/1) array
for ($i=0; $i<60; $i++) $cells[$i] =0;
function reserve($dtype, $x, $y)
{ global $cells, $divs; if ($y*10+$x >59 ||($cells[$y*10+$x])) return false;
switch ($dtype)
{ case 2: if ($y ==5 || $cells[$y*10+$x] ||$cells[($y+1)*10+$x]) return false; $cells[($y+1)*10+$x] =1; break;
case 3: if ($x ==9 || $cells[$y*10+$x] ||$cells[$y*10+$x+1]) return false; $cells[$y*10+$x+1] =1; break;
}
$cells[$y*10+$x] =1; $divs[] =divHtml($dtype, $x, $y); return true;
}
for($i = 0; $i < 10; $i ++) while (!reserve(2, rand(0,9), rand(0,5))) ; //adding 10 blocks of type2 (20cellsTotal)
for($i = 0; $i < 5; $i ++) while (!reserve(3, rand(0,9), rand(0,5))) ; //adding 5 blocks of type3 (10cellsTotal)
for($i = 0; $i < 15; $i ++) while (!reserve(1, rand(0,9), rand(0,5))) ; //adding 15 blocks of type1 (15cellsTotal)
for($i = 0; $i < 60; $i ++) if (!$cells[$i]) reserve(4, $i%10, floor($i/10)) ; //filling rest 15 cells with type4
//^go through all cells, but filling only 15
echo '<div style="position:absolute; width:1000px; height:600px;">';
foreach ($divs as $single) echo $single;
echo '</div>';
?>

Related

for loop gets executed just once

I have created an array dynamically like this way
$names = array();
for ($i = 0; $i < 100; $i++) {
$names[] = $i;
}
then created part
$parts = count($names) / 20;
and created a sub array then loop through the parts
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i = 0; $i < count($newarray); $i++) {
echo $i;
}
$j = $j + 20;
}
The problem is that this code displays from zero to 19 It doesn't display the other parts
Both the inner and outer loops use the same control variable $i, so just change the inner one...
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i1 = 0; $i1 < count($newarray); $i1++) {
echo $i1;
}
$j = $j + 20;
}

How to make this content loop inside divs

I got this code to generate a list of 6 random products.
How can I make it generate each product inside a div?
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
echo $ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart);
}
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
echo '<div>' . $ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart) . '</div>';
}
$max_items = 6;
for($i = 0; $i < $max_items; $i++) {
printf('<div> %s </div>',$ps_product->show_snapshot($prodlist[$rand_prods[$i]], $show_price, $show_addtocart));
}

print value of a variable that is composed for a string into a For Loop

In this loop, I'm trying to take the value of the variable, but to save code I want to use a For Loop to print it concatenating part of the variable with a number genereted into the loop. This is my Try.
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
echo '$x'.$i;
}
?>
the result that I'm geting is
$x0$x1$x2$x3$x4
I want it to end up like this:
0123
Its supposed to be:
for ($i=0; $i < 5; $i++) {
echo ${"x$i"};
}
Sidenote: You'll have to define $x4 or terminate it to < 4 so you won't get a undefined index.
Try this :
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
for ($i=0; $i < 5; $i++) {
$y='x'.$i;
if(isset($$y)){
echo $$y;
}
}
Try this Code:
<?php
$x0 = 0;
$x1 = 1;
$x2 = 2;
$x3 = 3;
$string = '';
for ($i=0; $i < 5; $i++) {
$string .= $i;
}
echo $string;
?>

Find the greatest product of five consecutive digits in the 1000-digit number PHP?

I was trying to solve the 8. Question of project euler and I have the following PHP code. But my answer isnt right. What did i do wrong?
<?php
$data = '7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450';
$greatest = 0;
for ($i = 0; $i < strlen($data); $i++) {
$_data = substr($data, $i, 5);
if (strlen($_data) == 5) {
$_product = 1;
for ($x = 0; $x < 5; $x++) {
$_product += $_data[$j];
}
if ($_product > $greatest) {
$greatest = $_product;
}
}
}
echo $greatest;
?>
In your code you need to change the following:
for ($x = 0; $x < 5; $x++) {
$_product += $_data[$j]; // Here change the plus sign (+) with a star (*)
}

decrese value of loop

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}
Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}
It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;
You need to decrement $p outside the first loop

Categories