php - Set session value in foreach loop -


in project, working on @ moment. users can upload pictures in different parts of site. either upload pictures in post posted or upload pictures show off pet (which has profile page already). came images table, looks this:

table `images` (  `image_id` int(11) unsigned not null auto_increment,  `user_id` int(11) unsigned not null comment 'foreign key referencing user_id of user, uploaded images',  `image_name` varchar(64) not null,  `dog_pet_id` int(11) null comment 'foreign key referencing pet's, image belongs to',  `post_id` int(11) null comment 'foreign key referencing post's, image belongs to',  primary key (`image_id`),  foreign key (`user_id`) references users(user_id) on delete cascade on update cascade ) engine=innodb default charset=utf8 collate=utf8_unicode_ci; 


if user user_id = 1, have pet dog_pet_id = 2 , upload picture pet output this:

+------------------------------------------------------------+ | | image_id | user_id | image_name | dog_pet_id | post_id | | | |        1 |       1 |     bla    |       2    |       0 | | +------------------------------------------------------------+ 

if upload image in post dog_pet_id 0 , post_id have post's id , on.
works intended. my problem displaying these images!

my attempt solve problem (in mvc structure (no cms or framework)):

display images user_id equal user's user_id , dog_pet_id equal pet's id. problem, not able solve how pet's dog_pet_id view model.
did things using form submit or kind of link, send id first time cannot that.

therefore, idea send via $_session super global, however, gets overwritten in foreach loop. if there more 1 pet per user, last pet's images diplayed.

the model

    /**      * display pet images, have been uploaded user      */     public static function displaypetimages()     {         $database = databasefactory::getfactory()->getconnection();          $query = $database->prepare("select * images user_id = :user_id , dog_pet_id = :dog_pet_id");          $query->execute(array(':user_id' => $_session['user_id'], ':dog_pet_id' => $_session['dog_pet_id']));          return $query->fetchall();     } 

the controller

    /**      * show user's private profile      */     public function showprofile()     {         'pets' => petmodel::getpet(), // get's pet's information database         'petimages' => petmodel::displaypetimages() // function shown above         ));     } 

the view

foreach ($this->pets $pet) {     foreach ($this->petimages $petimage) { ?>           $_session['dog_pet_id'] = $pet->dog_pet_id; <----- overwritten!           <img src="<?= /path/to/the/image/ . $pet->dog_pet_id . '/' . $petimage->image_name . '.jpg' ?>" />      }     echo "<form action='" . config::get('url') . 'pet/uploadpetimages_action/' . $pet->dog_pet_id . "' method='post' enctype='multipart/form-data'>";     echo "<input type='file' name='images[]' multiple required />";     echo "<input type='hidden' name='max_file_size' value='5242880' />";     echo "<input type='submit' value='upload images' />";     echo "</form>     } 


not sure, if achieve @ possible or if there better way this.
anyway, thankful kind of help!

update

the model

    /**      * display pet images, have been uploaded user      */     public static function displaypetimages($dog_pet_id)     {         $database = databasefactory::getfactory()->getconnection();          $query = $database->prepare("select image_name images user_id = :user_id , dog_pet_id = :dog_pet_id");          $query->execute(array(':user_id' => session::get('user_id'), ':dog_pet_id' => $dog_pet_id));          return $query->fetchall();     } 

the controller

        /**          * show user's private profile          */         public function showprofile()         {             'pets' => petmodel::getpet(), // get's pet's information database             'petimages' => petmodel::displaypetimages() // function shown above             ));         } 

the view

foreach ($this->pets $pet) { ?>                   <?php foreach ($this->petimages $petimage) { ?>             <img src="<?= config::get('url') . config::get('path_images_public') . session::get('user_id') . '/' . 'pets/' . $pet->dog_pet_id . '/' . displaypetimages($pet->dog_pet_id) . '.jpg' ?>" />;         }      } 

output

warning: missing argument 1 petmodel::displaypetimages()
notice: undefined variable: dog_pet_id

that session thing won't work, reason specify: view runs loop, overwrites variable.

i'd have separate html form each pet, , separate upload button;

<?php foreach ($this->pets $pet) { ?>     <form action="/wherever" method="post">         <input type="hidden" name="pet_id" value="<?= $pet->pet_id ?>" />         <?php foreach ($this->petimages $petimage) { ?>             <img src="<?= /path/to/the/image/.$pet->dog_pet_id.'/'.$petimage->image_name.'.jpg' ?>" />         <?php } ?>         <input type="submit" value="save" />     </form> <?php } ?> 

then depending on save button click, have different pet_id value compare.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -