echo $portfolio->beginning_cap; ?> </td> <td><?php echo $portfolio->current_cap; ?> </td> <td><?php echo $portfolio->starting_date; ?> </td> <?php $gains = $portfolio->current_cap / $portfolio->beginning_cap - 1; ?> <td><?php echo print_money($gains); ?> </td> <?php $user = $this->ion_auth->user($portfolio->user_id)->row(); ?> <td><?php echo $user->first_name . " " . $user->last_name; ?> </td> <td><?php echo form_open('portfolios/delete/' . $portfolio->id); echo form_submit('delete_portfolio', 'Delete'); echo form_close();
public function view() { $portfolio_id = $this->uri->segment(3); $portfolio = Portfolio::find_by_id($portfolio_id); $data['portfolio'] = $portfolio; if (null !== $this->input->post('submit_stock_query')) { //fill this section if a certain post value is submitted, like a buy $stocks_query = array(); foreach ($_POST as $symbol) { $stocks_query[] = $symbol; } array_pop($stocks_query); $stocks_query = $this->stock->live_quotes($stocks_query); // Generate table of queried stocks for ($i = 0; $i < count($stocks_query); $i++) { //Add Buy form $stocks_query[$i][''] = form_open('portfolios/buy') . form_hidden('stock_symbol', $stocks_query[$i]['symbol']) . form_hidden('portfolio_id', $portfolio_id) . form_input(array('name' => 'shares', 'placeholder' => 'Shares', 'type' => 'number')) . form_submit('buy_stock', 'Buy') . form_close(); } $data['stocks_query'] = $stocks_query; } $data['outstanding_stocks'] = $this->get_current_stock_value(); //Generate table of current stocks $this->db->where('sale_time', NULL); $data['portfolio_stocks'] = $this->stock->find_by('portfolio_id', $portfolio_id); $data['outstanding_stock_value'] = $this->get_current_stock_value($data['portfolio_stocks']); $data['gains'] = $this->portfolio_gains($data['portfolio_stocks'], $portfolio); //preprint($data['portfolio_stocks']); //Get historical data of stock purchases $this->db->where('sale_time !=', NULL); $trades = $this->stock->find_by('portfolio_id', $portfolio_id); //Generate Table $this->table->set_template(array('table_open' => "<table class='table table-striped table-bordered table-hover' id='history_table'>")); $this->table->set_heading('Security', 'Shares', 'Gains', 'Portfolio Value', 'Time Bought', 'Opening Price', 'Time Sold', 'Closing Price'); $chart_vars = array(); foreach ($trades as $trade) { $symbol = $trade->symbol; $shares = $trade->shares; $purchase_price = $trade->purchase_price; $sale_price = $trade->sale_price; $gains = $shares * (($sale_price - $purchase_price) / $purchase_price); $trade_capital = $portfolio->beginning_cap + $gains; $chart_vars[] = array('time' => strtotime($trade->purchase_time), 'value' => $trade_capital); if ($gains > 0) { $gains_cell = array('data' => print_money($gains), 'class' => 'success'); } elseif ($gains < 0) { $gains_cell = array('data' => print_money($gains), 'class' => 'danger'); } $this->table->add_row($symbol, $shares, $gains_cell, print_money($trade_capital), $trade->purchase_time, print_money($purchase_price), $trade->sale_time, print_money($sale_price)); } $data['chart_vars'] = json_encode($chart_vars); $data['historical_trades'] = $this->table->generate(); //DISPLAY PAGE $data['charts'] = TRUE; $this->load->view('dressings/header'); $this->load->view('dressings/navbar'); $this->load->view('stocks', $data); $this->load->view('dressings/footer'); }