[eluser]Unknown[/eluser]
Good morning. The following code works but refreshing browser or submitting an invalid entry still places entry in database. Why, please?
Also the +.50 only displays as +.5 .
Many thanks. Steve.
View index.php
Code:
<div><?php $inc=0.50;
$total=$num_rows+$inc;?>
Bids so far: <?php echo $num_rows;?>
</div>
<div>
<?php foreach ($price as $price): ?>
Your minimum bid:
<?php echo $price['price']+.50;?>
<?php endforeach ?>
<?php echo validation_errors(); ?>
<?php echo form_open('index') ?>
<label for="price">Your bid</label>
<textarea name="price"></textarea><br />
<input type="submit" name="submit" value="Place your bid" />
</form>
</div>
Controller bids.php
Code:
<?php
class Bids extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('bid_model');
}
public function index()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['price'] = $this->bid_model->get_bids();
$data['num_rows']= $this->bid_model->get_rows();
$this->form_validation->set_rules('price', 'price', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('index', $data);
}
else
{
$this->bid_model->place_bid();
$this->load->view('welcome');
}
}
}
Model Bid_model.php
Code:
<?php
class Bid_model extends CI_Model {
public function get_bids()
{
$this->db->order_by('price','DESC');
$this->db->limit('1');
$query = $this->db->get('bids');
return $query->result_array();
}
public function get_rows()
{
$query = $this->db->get('bids');
$num_rows = $query->num_rows();
return $num_rows;
}
public function place_bid()
{
$this->load->helper('url');
$slug = url_title($this->input->post('price'), 'dash', TRUE);
$data = array(
'price' => $this->input->post('price')
);
return $this->db->insert('bids', $data);
}
}