Loop through all posts, show attachment if there

You can’t use the_title() as you haven’t set up the post data.

Instead for the title you need to use:

$post->post_title;

To get the attachments you can then use get_children() something like this:

$args = array(
    'numberposts' => 1,
    'post_parent' => $post->ID,
    'post_type' => 'attachment'
);

$attachments = get_children( $args );

foreach($attachments as $attachment) {
  // Do your stuff
}

The thing is there might be multiple attachments on a post, so it might not be predictable. Also what kind of attachment do you want to get? You can use:

'post_mime_type' => 'image'

for example, as part of the argument – if you only want images.

What might be better, if you have the option, is to use the post thumbnail or a meta key/value (custom field) instead so the user can specifically add an attachment.