[eluser]Sanjay Sarvaiya[/eluser]
[quote author="brian88" date="1337196196"]Thanks. both are good answers. I did boltsabre's way. And now I wanna do PhilTem's way also.
I made a new column "images"
So now I can store all the trucks into this column with a serialized array?
I showed all my code above but heres the model and updated controller code.
Where can i serialize the data then unserialize for the view?
model
Code:
function addTruck($data){
//$d = serialize($data); // dont work
$insert = $this->db->insert('trucks', $data);
return $insert;
} // end function
controller updated code
Code:
function index() {
// get all trucks from database
$data['trucks'] = $this->main_mod->getTrucks();
$data['num_of_images'] = 5;
// if user presses submit button
if($this->input->post()){
// get the name of the truck
$db_data['name'] = $this->input->post('name');
// format the truck name to be a folder name
$folder = str_replace( " ", "", strtolower($this->input->post('name')) );
// if the name of the truck folder does not currently exist.
if(!is_dir('assets/images/'.$folder)){
// make a folder based off the name of the truck
mkdir('assets/images/'.$folder, 0777);
// config for file uploads
$config['upload_path'] = FCPATH . 'assets/images/' . $folder;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '0'; //1000 = 1MB
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
// upload users submitted images
for ($i=1; $i<=$data['num_of_images']; $i++){
// if user uploaded an image
if ( !empty($_FILES["file" . $i]['name']) ){
// upload file
if($this->upload->do_upload("file".$i)){
$upload_data = $this->upload->data();
$db_data["images"] = $folder.'/'.$upload_data['file_name'];
}else{ // error
echo $this->upload->display_errors();
}
}
}
// add to database
$this->main_mod->addTruck($db_data);
// refresh the page to see view new data added.
redirect('/');
// Debug
foreach($db_data as $d){
echo serialize($d) . '<br /><br />';
// uploaded 1 photo....it returns
// s:9:"truckname";
// s:18:"truckname/logo.jpg";
}
}else{
// The truck name folder exist. Need a new name for the folder.
echo 'Error: This truck name already exist. Choose a different name.';
}
}else{
// views
$data['content'] = 'main/post';
$this->load->view('templates/main', $data);
}
} // end function
I tried
Code:
serialize( $db_data["images"] ) = $folder.'/'.$upload_data['file_name'];
But that fails.[/quote]
Hey dude store file name in image array like this.
Code:
$db_data["images"][] = $folder.'/'.$upload_data['file_name'];