# Solved: PHP/SQL Image Gallery



## Big-K (Nov 22, 2003)

Argh, last resort. I've been working on a new way of updating the image galleries of my website, moving from manual code updating to using a mysql database as i managed to learn sql and the php that goes with it. I've got that working fine. Now i'm working on the smaller issues so i figured i'd come here for answers. The system i made scans a folder for images when i run a script and inputs their filename into a table in the database. I'm using this code to display the images it inputs.


```
while($row = mysql_fetch_array($result))
  {
  echo "<td width=\"100px\" height=\"100px\" align=\"center\">";
  echo "[URL][IMG alt=".$row['alt']."].$path.$row['filename'].[/IMG][/URL]";
  echo "[/TD]";
  }
```
One of the main things i want to do is make it so that every five images it will put in [/TR][TR] to make a new row, this would be so that it fits in the table (the page i'm deving is http://kedart.com/dev/main/index.php?s=photo&g=abstract , the other pages around it are still normal). Another option would be to take out the table seperation and just make an overflow control so that when too much is in it moves to the next line.

More problems may arise as i start the other parts i planned to use mysql for.

-K


----------



## brendandonhu (Jul 8, 2002)

I haven't tested this at all but it might work

```
<?php
$i = 1;
while($row = mysql_fetch_array($result))
{
  $data .= sprintf('[TD][URL=javascript:PopupPic(\'%s\')][IMG alt="%s"]%s[/IMG][/URL][/TD]', $pop . $row['filename'], $path . $row['filename'], $row['alt']);
  if($i % 5 === 0 || $i < 5)
  {
    echo '[TR]' . $data . '[/TR]';
    unset($data);
  }
  $i++;
}
```


----------



## Big-K (Nov 22, 2003)

That puts the end row tags around every image, making them go vertical instead. Would I need to have a field in the mysql table called id?


----------



## Big-K (Nov 22, 2003)

Brendans solution didn't work, but it did help. I cobbled together this code and it did work. It requires an id field and would have to be added to for id 10 to do it or id 15. If someone could edit the code to do that as well that would rock.


```
while($row = mysql_fetch_array($result))
  {
    if($row['id'] == 5)
  {
  echo "<td width=\"100px\" height=\"100px\" align=\"center\">";
  echo "[URL][IMG alt=".$row['alt']."].$path.$row['filename'].[/IMG][/URL]";
  echo "[/TD]";
  echo "[/TR][TR]";
  }
  else{
  echo "<td width=\"100px\" height=\"100px\" align=\"center\">";
  echo "[URL][IMG alt=".$row['alt']."].$path.$row['filename'].[/IMG][/URL]";
  echo "[/TD]";
  }
  }
```


----------



## brendandonhu (Jul 8, 2002)

This way might be closer...if not I can set up a database and mess with it.

```
<?php
$i = 1;
$rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
  $data .= sprintf('[TD][URL=javascript:PopupPic(\'%s\')][IMG alt="%s"]%s[/IMG][/URL][/TD]', $pop . $row['filename'], $path . $row['filename'], $row['alt']);
  if($i % 5 === 0 || $i === $rows)
  {
    echo '[TR]' . $data . '[/TR]';
    unset($data);
  }
  $i++;
}
?>
```


----------



## Big-K (Nov 22, 2003)

Yeah that does work. I used it to modify the one i wrote(since you look to have an entirely different style of code). I modified my if to


```
if($row['id'] % 5 === 0 || $row['id'] === $rows)
```
either works.


----------



## brendandonhu (Jul 8, 2002)

Good


----------

