Search code examples
phpmysqlcreate-table

Pictures in a table


I am using PHP and MySQL (html and CSS aren't the big deal) and I want to collect images I uploaded (the paths are in MySQL) and put them in a table. Imagine a table of 5 columns and 6 rows (30 pictures in total). I want the newest picture to be in the first row of the first column -- so left and at the top --, I want the oldest picture to be in the last row of the last column -- so right and at the bottom --.

index.php

<?php
    //Put all required/included files in here
    require_once('config.php');

    //Connection to database
    $link = mysql_connect(DB_HOST,DB_USER,DB_PASS);
    if(!$link) {
        die('Connection failed: ' . mysql_error());
    }

    //Select which database to use
    $db = mysql_select_database(DB_NAME);

    if(!$db) {
        die("Database selection failed: " . mysql_error());
    }
?>

config.php

<?php
    define('DB_HOST','host');
    define('DB_USER','user');
    define('DB_PASS','pass');
    define('DB_NAME','name');
?>

Now, the SQL layout:

layout.sql

CREATE TABLE 'fotos'(
    id int not null,
    locatie_thumb text not null,
    locatie_middel text not null,
    locatie_groot text not null,
    primary key(id)
);

CREATE TABLE 'tekst'(
    id int not null auto_increment,
    titel text not null,
    bijschrift text not null,
    album text not null,
    datum DATE not null,
    primary key(id)
);

The idea behind the tables is that I upload a pic via the upload form, I enter the titel, bijschrift, album, datum values there and MySQL creates an ID. Then I want to use that ID to link the tekst and fotos together into the table.

How can I collect the 30 newest entries from MySQL and arrange them in a table of 5 columns and 6 rows when I want the newest picture to be left,top and the oldest to be right,bottom?


Solution

  • Have your tried:

    SELECT *
    FROM fotos, tekst
    WHERE fotos.id=tekst.id
    ORDER BY tekst.datum DESC
    LIMIT 30
    

    If this is a 1-to-1 relationship, then why not put it all in the same table