Jump to content

Recommended Posts

Hi Guys,

 

I'm trying to format the results from a query into a table to have a number of images in a row... You know like google images...

 

My issue is that there is something wrong with my logic along the way... and the first two rows are missing images (it's a bit odd as the rest seem to be ok, and then I tidy up at the end... Here is the code, and if you can spot my mistake(s) then there is a seat on the right hand of the 'Great Code Man'

 

$num_records = mysql_num_rows($result); //Total number of records in result 
$num_per_row = 3; //Change to however many columns per row you desire
$percentage = 100/$num_per_row; //for table formatting later on td width

echo "<table width=\"450\" border=\"0\">
<tr>";

mysql_close();

$i = 0; 

while ($row = mysql_fetch_array($result)) { 
$ID=mysql_result($result,$i,"ID");
$ImageURL=mysql_result($result,$i,"ImageURL");
$sold=mysql_result($result,$i,"Sold");

    if (($i == 2) || (($i > $num_per_row) && (($i % $num_per_row) == 1))) { 
        echo "</tr><tr>"; 
    } 

    echo "<td width=\"$percentage%\"><a href=\"image.php?ID=$ID\"><img src=\"/pictures/thumbnails/$ImageURL\" border=\"1\"></a>";
    if ($sold == 1) {
    echo "<img src=\"/images/sold.gif\" height=\"20\">";
    }
    echo "</td>
    ";   

    if (($i == $num_records) || (($i >= $num_per_row) && (($i % $num_per_row) == 0))) { 

       //Fill in empty columns as necessary 
       if (($i == $num_records) && ($i % $num_per_row != 0)) { 
           $n = $num_per_row - ($i % $num_per_row); 
           echo '<td colspan="' . $n . '"> </td>'; 
       } 

       echo "</tr>"; 
    } 

    $i ++; //Increment counter 
}

 

I know it's a bit of a mess, but that's due to me messing with it (as it didn't work) and now it 'kind of works' but I get:

 

With three columns

 

2

2

3

3

3

3

3

remainder

 

With two columns

 

2

1

2

2

2

2

remainder

 

It's all a bit odd if you ask me...

 

But if I know how to fix it I would have by now (and I'm loathed to start from scratch as I'd just get a little confused again)

 

Hope that you can help me (I'm sure that it's a simple mistake somewhere)

 

 

A. ;)

 

Well, I'm not sure if it has some effect on it, but why do you have mysql_close();

before you fetch some rows? I think it is atleast good practice to put this code at the end if it doesnt take effect in this case. You never know if you ever will need to something with database in while loop..

 

its only my opinion..

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.