Trouble Stacking Different-Sized Boxes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Trouble Stacking Different-Sized Boxes

Post by NinjaBot »

Hi there.

I'm trying to stack some boxes. My code works, but I'm having trouble fathoming how to make the height of each box in the stack 'unique' instead of set to "10";

Code: Select all

$margin = "10";
$top0 = "10";
$height0 = "10";
$height1 = "12";
$height1 = "20";

for ($i = 1; $i < 8; $i++){
	$prev = ($i - 1);
	$prevHeight = "height".$prev;
	$prevTop = "top".$prev;
	$shift = ($$prevTop + $$prevHeight + $margin);
	${"top" . $i} = $shift;
	${"height" . $i} = $$prevHeight;
}
I should get something like the following:

Code: Select all

$top0 = "10";
$height0 = "10";
$top1 = "30";
$height1 = "12";
$top2 = "52";
$height2 = "20";
I'd much appreciate some help to fix this. Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I have no idea what you're asking.
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Post by NinjaBot »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


[quote="feyd"]I have no idea what you're asking.[/quote]

Maybe a fully formatted example may help:

Code: Select all

<?

$margin = "3";
$top0 = "10";
$height0 = "130";
$height1 = "300";
$height2 = "200";
for ($i = 1; $i < 8; $i++){
	$prev = ($i - 1);
	$prevHeight = "height".$prev;
	$prevTop = "top".$prev;
	$shift = ($$prevTop + $$prevHeight + $margin);
	${"top" . $i} = $shift;
	${"height" . $i} = $$prevHeight;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<title>Boxes</title>
<style type="text/css">
div {position: absolute;border: solid 1px #666;}
</style>
</head>
<body>


<div style="top:<?=$top0;?>px;height:<?=$height0;?>px">
<div style="top:<?=$top1;?>px;height:<?=$height1;?>px">
<div style="top:<?=$top2;?>px;height:<?=$height2;?>px">


</body>
</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The logic you have copies the previous height setting to the current height. I believe losing the last line of the loop will correct this problem.
NinjaBot
Forum Newbie
Posts: 7
Joined: Sat Apr 29, 2006 11:54 am

Post by NinjaBot »

feyd wrote:The logic you have copies the previous height setting to the current height. I believe losing the last line of the loop will correct this problem.
Thanks, feyd.

Indeed that was the problem. Can't believe I overlooked that.
Post Reply