doddsey_65 Posted March 2, 2011 Share Posted March 2, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229332-attachment-list/ Share on other sites More sharing options...
gizmola Posted March 2, 2011 Share Posted March 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229332-attachment-list/#findComment-1181719 Share on other sites More sharing options...
doddsey_65 Posted March 2, 2011 Author Share Posted March 2, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229332-attachment-list/#findComment-1181817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.