Jump to content

attachment list


doddsey_65

Recommended Posts

i have it so people can attach files to their posts. So on the end of the post table i have a colum called p_attachments. it contains data like so:

 

username1-_-image1.jpg|username2-_-image2.png|

 

I also have an attachments table with data like so:

 

a_id      a_name      a_size

1          image1      456123

2          image2      123456

 

so when i display the posts i want to display the name of the attachment, and its size aswell as the image if it is an image.

 

<?php
$attachment_list = explode('|', $row[$key]['p_attachments']);
    $a_list = '';

    $amount = count($attachment_list);
    $a_name = array();
    $size = array();
    $ext = array();
    $_a_name = array();
    
    for($i=0; $i<$amount-1; $i++)
    {
        $ext[] = preg_split('|\.|', $attachment_list[$i]);
        $names[$i] = $ext[$i][0];
        
        $q = "SELECT * FROM ".TBL_PREFIX."post_attachments
                        WHERE a_name = '".$names[$i]."'<br />";
                        
                        
                
        $a_query = $link->query("SELECT * FROM ".TBL_PREFIX."post_attachments
                        WHERE a_name = '".$names[$i]."'")
                        or die(print_link_error());
        $result = $a_query->fetchAll();

        $size[] = round(($result[$i]['a_size'] / 1024), 2);
        
        $_a_name[] = preg_split('||', $result[$i]['a_name']);
        
    
        if($ext[$i][1] == 'png' || $ext[$i][1] == 'jpg' || $ext[$i][1] == 'jpeg' || $ext[$i][1] == 'gif' || $ext[$i][1] == 'bmp')
        {
            if(preg_match('#\[attachment=(.*?)\]#si', $p_content))
            {
                $bb[$i] = "#\[attachment=(.*?)\]#si";
                $html[$i] = '<blockquote class="attachment"><a class="attachment" href="'.$config['asf_root'].'attachments/\\1"><img class="attachment" src="'.$config['asf_root'].'attachments/\\1" alt="\\1" title="Click For Full Image" /></a><span><strong>'.$_a_name[$i].'</strong></span><span><strong>Size: </strong>'.$size[$i].'kb</span></blockquote>';
            }
        }
?>

 

but the code isnt looping through all of the attachments on a post, it only displays data from the last attachment.

 

Any ideas?

Link to comment
Share on other sites

Your design for this feature is just hopelessly wrong.  Did it not occur to you that 2 people might attach a file with the same name?

 

As for why your current code doesn't work, this doesn't seem to be complete, because you start a loop based on the explode of the list of attachment names (bad design... see "repeating group")  that never gets ended in the code you provided.

 

I really don't know why it is that since you even made an attachment table, you didn't just go the extra yard and have the post id, be a foreign key in that table. 

Link to comment
Share on other sites

that would be the simplest way, but the attachments are added to the database before the post is(not my script and not the way i prefer) so i dont have a post id to go off until the post is submitted. But by then the attachment is already in the database.

Link to comment
Share on other sites

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.