Charlie Brown Charlie Brown - 14 days ago 6
PHP Question

How to show all received array result from input array value in codeigniter

Hi I'm working in codeigniter. I want to show all array passed from an input into my email html format. But now only show 1 item name only, how to show all the item name. Thank you

<?php

$details = $this->transaction_detail->get_all_by_header($trans->id)->result();

foreach($details as $value)
{
echo '<input type="hidden" name="itemname" value="'. $this->item->get_info($value->item_id)->name. '">';
}

?>


Received the array and put into email function

public function send_mail()
{

$config['newline'] = "\r\n";


$from_email = "info@xxx.com";
$to_email = $this->input->post('email');


// Only show one item only, how to show all the result array item name

$itemname = $this->input->post('itemname');

$html = "Item Name :" . $itemname . "<br>" .




//Load email library
$this->load->library('email');
$this->email->initialize($config);
$this->email->from($from_email, 'xxx.com');
$this->email->to($to_email);
$this->email->subject("xxx.com - Completed Order");
$this->email->message($html);

//Send mail


if ($this->email->send()) {
$this->session->set_flashdata("email_sent", "Email sent successfully.");
redirect(site_url('transactions'));
return true;
} else {
$this->session->set_flashdata("email_sent", "Error in sending Email.");
return false;
}



}

Answer

I can only guess hows your app working, but first, I would make your input an array.

echo '<input type="hidden" name="itemname[]" value="'. $this->item->get_info($value->item_id)->name. '">';

And then correct controller a little:

$html = '';
$items = $this->input->post('itemname[]');//or without square brackets
foreach ($items as $item) {
    $html .= "Item Name :" .  $item. "<br>" .
}
Comments