I am coding a page that through a form allows you to write phrases or whatever you want and then display them on the page using a foreach loop.
I am doing mostly everything in an object oriented way, creating objects phrase
and phrase_photo
. With each phrase
created it is displayed alongside it a default photo. There is button besides each photo that will trigger an upload form to change the default photo for another one you would like.
I have two tables in the database for this, phrases
and phrase_photos
. The relationship between the both is through the attribute
$phrase_photo->phrase_id
so if you change the default photo, a phrase_photo
object will be created pulling dynamically the $phrase->id
value and assign it to $phrase_photo->phrase_id
attribute.
The iteration in the foreach loop is giving me no problems but when I try to change let’s say phrase#3 photo, $phrase_photo->phrase_id
attribute is always assigned with phrase#1 phrase->id
attribute.
It seems the upload form inside the foreach loop to change the default photo starts from the very first element with no regard whether you are clicking in the button of another element phrase down the iteration. This is resulting in that I am only able to change the photo of the very first phrase in the iteration.
Here it is the code:
<?php
$id = $session->user_id;
$user = User::find_by_id($id);
$phrases = Phrase::find_all_userid($id);
?>
<?php
//CODE TO CREATE THE PHRASE OBJECT AND SAVE IT 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 TO WRITE THE PHRASE -->
<section class="write_phrase">
<form action="profile.php" method="post">
<p>Write a 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 TO DISPLAY ALL THE PHRASES CREATED-->
<section class="display_phrases" >
<?php foreach($phrases as $phrase){ ?>
<article>
<p><?php echo $phrase->content; ?></a></p> <!--DISPLAY THE ACTUAL PHRASE -->
<?php $phrase_photo = PhraseFoto::find_by_phraseid($phrase->id);?> <!--SEE IF THERE IS CUSTOMIZED PHOTO FOR THE 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 TO CHANGE THE DEFAULT PHOTO TO THE 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 another photo</label>
<input type="text" name="upload" hidden />
<?php
if(isset($_POST['upload'])){
$phrase_photo= new PhraseFoto();
$phrase_photo->phrase_id=$phrase->id; //HERE IS PROBLEM, it always assigns to this attribute the $phrase_id of the first phrase regardless if you are changing the photo of another phrase.
$phrase_photo->attach_file($_FILES['photo']);
if($phrase_photo->save()){
redirect_to("profile.php");
} else{
}
}
?>
</form>
</article>
<?php } ?>
</section>
I hope I am being clear enough. If so, can you see the problem and spot what I need to change in order for the assignment of the values of the form upload inside the loop can work properly ?