Jump to content

construct for <div>image</div> Grid CSS PHP


nadeemshafi9

Recommended Posts

hi guys i am trying to arange the items in my db as a grid i will show you what i have currently got its not too good lol.

the first 3 items are printed nicley 3 across then the next line starts to farr from the left even thogh the mesurments are right.

[code]<?php
$accross = 0;
$leftPos = 0;
$topPos = 0;

while($row = mysql_fetch_array($result)) {
if($accross = 1) {
$leftPos = 3;
}
if($accross = 2) {
$leftPos = 6;
}

print "".
"<div style='text-align: left; position: absolute; ".
"left: " + leftPos + "cm; top: " + topPos + "cm; width: 2cm; height: 4cm;'>".

"<img src='img.asp?id=" + $row["id"] + "' height='80px' width='80px'><br>".
$row["name"] + "<br>".
$row["type"] + "<br>".
$row["description"] + "<br>".

"</div>";

if($accross = 2) {
$topPos = $topPos + 4;
$leftPos = 0;
}

if($accross != 2){
$accross = $accross + 1;
}
}
?>[/code]
thanx for any help
Link to comment
Share on other sites

Your logic is a bit out of whack.  Once $accross is equal to 2, it won't increment any more.  First iteration $accross = 0, second iteration $accross = 1, third iteration $accross = 2.  From there on, $accross will always equal 2.

More importantly, you are basically setting the value of $accross with each 'if' statement.  If you are checking to see if $accross is equal to a value, you need to use if ($accross == 2).  The statment if($accross = 2) simply returns true if it actually set $accross to the value of 2, which it always would.

Try something like this...
[code]
<?php
        $accross = 0;
        $leftPos = 0;
        $topPos = 0;

        while($row = mysql_fetch_array($result)) {

                if($accross == 1) {
                                $leftPos = 3;
                }
                if($accross == 2) {
                                $leftPos = 6;
                }
                if($accross == 3) {
                                $leftPos = 0;
                                $topPos = $topPos + 4;
                                $accross = 0;
                }

                print "".
                "<div style='text-align: left; position: absolute; ".
                "left: " + leftPos + "cm; top: " + topPos + "cm; width: 2cm; height: 4cm;'>".

                "<img src='img.asp?id=" + $row["id"] + "' height='80px' width='80px'><br>".
                $row["name"] + "<br>".
                $row["type"] + "<br>".
                $row["description"] + "<br>".

                "</div>";

                $accross++;

        }
?>[/code]
Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.