i coding page through form allows write phrases or whatever want , display them on page using foreach loop.
i doing in object oriented way, creating objects phrase
, phrase_photo
. each phrase
created displayed alongside default photo. there button besides each photo trigger upload form change default photo 1 like.
i have 2 tables in database this, phrases
, phrase_photos
. relationship between both through attribute
$phrase_photo->phrase_id
so if change default photo, phrase_photo
object created pulling dynamically $phrase->id
value , assign $phrase_photo->phrase_id
attribute.
the iteration in foreach loop giving me no problems when try change let’s phrase#3 photo, $phrase_photo->phrase_id
attribute assigned phrase#1 phrase->id
attribute.
it seems upload form inside foreach loop change default photo starts first element no regard whether clicking in button of element phrase down iteration. resulting in able change photo of first phrase in iteration.
here code:
<?php $id = $session->user_id; $user = user::find_by_id($id); $phrases = phrase::find_all_userid($id); ?> <?php //code create phrase object , save on db. if(isset($_post['submit'])){ global $database; $phrase = new phrase(); $phrase->user_id=$id; $phrase->content=trim($_post['phrase']); $phrase->created=strftime("%y-%m-%d %h:%m:%s", time()); $phrase->create(); } ?> <!--form write phrase --> <section class="write_phrase"> <form action="profile.php" method="post"> <p>write phrase:<input type="text" name="phrase" value="" /></p> <input type="submit" id="submit" name="submit" style="display: none" onchange="javascript:this.form.submit();"/> <label for="submit" class="button">create it!</label> </form> </section> <!--code display phrases created--> <section class="display_phrases" > <?php foreach($phrases $phrase){ ?> <article> <p><?php echo $phrase->content; ?></a></p> <!--display actual phrase --> <?php $phrase_photo = phrasefoto::find_by_phraseid($phrase->id);?> <!--see if there customized photo phrase --> <article class="image"> <img src= "<?php if(isset($phrase_photo)){echo "photos_gls/". $phrase_photo->filename;} else {echo "images/default.jpg";}?>"/> <!--display default photo or customized photo --> </article> <!--form change default photo phrase--> <form class="photo_up" id="form" action="profile.php" enctype="multipart/form-data" method="post"> <input type="hidden" name="max_file_size" value="1048576" /> <input type="file" id="photo" name="photo" style="display:none" value="upload" onchange="javascript:this.form.submit();"/> <label for="photo" class="button">select photo</label> <input type="text" name="upload" hidden /> <?php if(isset($_post['upload'])){ $phrase_photo= new phrasefoto(); $phrase_photo->phrase_id=$phrase->id; //here problem, assigns attribute $phrase_id of first phrase regardless if changing photo of phrase. $phrase_photo->attach_file($_files['photo']); if($phrase_photo->save()){ redirect_to("profile.php"); } else{ } } ?> </form> </article> <?php } ?> </section>
i hope being clear enough. if so, can see problem , spot need change in order assignment of values of form upload inside loop can work ?
Comments
Post a Comment