How to make this content loop inside divs - php

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));
}

Related

Write a PHP code to print following number pattern

Write a PHP code to print following number pattern:
147
258
369
I am trying like this but its shows me shows below. how to convert column to row pattern.
<?php
$num = "";
for($i=1;$i<=9;$i++) {
$num .= $i;
if($i%3==0){
$num .="<br />";
}
}
echo $num;
?>
please help me
You need to use for loop inside for loop to achieve this. Below is the code
$num = "";
for( $i = 1; $i <= 3; $i++ ) {
for( $j = 0; $j <= 2; $j++) {
$k = $i + ( $j * 3 );
$num .= $k;
}
$num .= "<br />";
}
echo $num;
Here is another way to get this output.
for($i = 1; $i <= 3; $i++) {
$print = $i;
for($j = 1; $j <= 3; $j++) {
echo $print;
$print = $print + 3;
}
echo "<br />";
}

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 generate dynamic number array like below

$big_box = array(1,2,3,16,17,18,31,32,33,46,47,48,61,62,63,76,77,78,91,92,93,106,107,108,121,122,123,136,137,138............);
$small_box = array(4,19,34,49,64.............);
I want to generate dynamic numbers array like above example upto 10000.
For Array $big_box Try to something like this.
$j = 0;
for ($i=0; $i <= 1000; $i++) {
if ($i!=0) {
$big_box[] = $i;
$j++;
}
if ($j == 3) {
$j = 1;
$big_box[] = $i+13;
$i =$i+13;
}
}
echo "<pre>";
print_r($big_box);
And array $small_box
$small_box = array();
for($i=0; $i <= 1000; $i++) {
if ($i == 0) {
$small_box[$i] = 4;
}else{
$small_box[$i] = $small_box[$i-1]+15;
}
}
echo "<pre>";
print_r($small_box);

PHP random DIV with css question

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>';
?>

create var's with others

I want to create somthing like 100 var's which their names will be:
$numbr_1 = 1;
$numbe_2 = 2;
$number_3 = 3;
...
I won't write 100 vars of course, but there is a way to do it with foor loop or somthing? I thought about this:
for ($i = 1; $i <= 100; $i++)
$number_{$i} = $i;
You're talking about variable variables, and they are incredibly stupid to use. For one, they make debugging next to impossible.
What you want is an array:
for ($i = 1; $i <= 100; $i++) {
$numbers[$i] = $i;
}
Something like this should work:
for($i = 1 ; $i <= 100 ; $i++){
$var_name = "number_$i";
$$var_name = $i;
}
for($i=1;$i<=100;$i++) {
$j="number$i";
$$j = $i;
}
Why don't you use an array?
$number = array();
for ($i = 0; $i < 100; $i++)
{
$number[] = $i;
}
for($i = 1 ; $i <= 100 ; $i++){
${'number_'.$i} = $i;
}
Possible solution is usage of array.
$number = array();
for ($i = 1; $i <= 100; $i++) {
$number[$i] = $i;
}

Categories