Welcome Guest, Not a member yet? Register   Sign In
Need Help with MySQL Result set binding on my view (I'm a newbie to CI)
#1

[eluser]Unknown[/eluser]
Hello,

Please I need help with my code I just recently started using Codeigniter.
MySQL result set for binding isn't displaying what I expect. Please have a look at my view code, controller and model codes.

I only need the last 3 records from the database but
The result displays a horizontal table row of 13 records and the table column data only displays 1 text character and not the full content of the selected columns

here is my code


The model - Its the home page of the site I'm working on.
Code:
[<?php
class Pages_model extends CI_Model {


public function __construct()
{
  $this->load->database();
}


/**
  * Function to fetch all sales products
  *
  * @param $slug
  * @return unknown_type
  */
        // sales status
  
$sales_status=3;
public function get_sales_products($sales_status)
{
//$sql = $this->db->get_where('products', array('sales_status' => 'yes'),0,3);

$sql = "SELECT  product_slug,product_code, product_img, sales_price, price FROM products  WHERE sales_status ='yes'  LIMIT 0,3";

  $query = $this->db->query($sql);
   return $query->row();
}
  
}

?>
The Controller -
?php

class Pages extends CI_Controller {

// Start new shopping session for guest
    public function __construct()
       {
            parent::__construct();
            // initializing session
          $this->load->library('session');
          // loading model for products on Sale
          $this->load->model('pages_model');
          //$this->load->model('pages_model', '', TRUE);
          
       }
/**
     * Loading home page view
     *
     *
     * @param $page
     * @return unknown_type
     */
public function view($page = 'home')
{

  
  $sales_status ='yes';
    // Retrieve sales products from database
$data['res'] = $this->pages_model->get_sales_products($sales_status);

if (empty($data['res']))
{
  //show_404();
  echo"<b>No sales record retrieved from database!</b>";
}
  

  
if (!file_exists('app/views/pages/'.$page.'.wtp'))
{
  // Whoops, we don't have a page for that!
  show_404();
}

$data['title'] = ucfirst($page); // Capitalize the first letter
// extend array data to parse more info to the view here

$this->load->view('templates/header.wtp',$data);
$this->load->view('pages/menu.wtp',$data);
$this->load->view('pages/'.$page.'.wtp',$data);
$this->load->view('templates/footer.wtp',$data);
}
}

?&gt;
The View (the view - home.wtp)

&lt;?php  foreach ($res as $sales_item): ?&gt;


  <td width=190 id="catalog">
<table width=190 cellpadding=4 cellspacing=2>
<tr>
<td colspan=2>&lt;?php echo $sales_item['product_slug']; ?&gt;/&lt;?php echo $sales_item['product_code']; ?&gt;"><img src="img/&lt;?php echo  $sales_item['product_img']; ?&gt;" ></td></tr>
<tr>
<td colspan=2 class="productname">&lt;?php echo $sales_item['product_name'] ?&gt; <br /></td></tr> <tr><td width=100 ><span class=mainbody>Was:</span>&nbsp;<span class="price"><s>&pound;&lt;?php echo $sales_item['price']; ?&gt;</s></span><br /><span class=mainbody>
Now:&nbsp;</span><span class="saleprice">&pound;&lt;?php echo $sales_item['sales_price'] ?&gt;</span>
</td> <td width=90>&lt;?php echo $sales_item['product_slug']; ?&gt;/&lt;?php echo $sales_item['product_code']; ?&gt;" class="formbtm">More details</td>
</tr>
</table></td>
&lt;?php  endforeach ?&gt;
Here is the database table schema
Code:
--
-- Table structure for table `products`
--

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `group_id` varchar(20) NOT NULL,
  `brand_id` int(6) NOT NULL,
  `product_name` varchar(255) NOT NULL,
  `product_slug` varchar(255) NOT NULL,
  `price` decimal(6,2) NOT NULL,
  `details` text NOT NULL,
  `product_info` text NOT NULL,
  `color` varchar(255) NOT NULL,
  `product_img` mediumtext NOT NULL,
  `product_code` int(6) NOT NULL,
  `catid` varchar(20) NOT NULL,
  `sales_price` decimal(6,2) NOT NULL,
  `saved_amount` decimal(6,2) NOT NULL,
  `availability` varchar(20) NOT NULL,
  `delivery_info` text NOT NULL,
  `sales_status` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `group_id`, `brand_id`, `product_name`, `product_slug`, `price`, `details`, `product_info`, `color`, `product_img`, `product_code`, `catid`, `sales_price`, `saved_amount`, `availability`, `delivery_info`, `sales_status`) VALUES
(1, 'G73587', 1, 'Ellis Light Oak Extending Dining Table ', '-Ellis-Light-Oak-Extending-Dining-Table- ', '930.00', 'latest oak brand', 'This is our latest supply', 'oak', '231445010.jpg', 604325, 'C173649', '893.00', '37.00', 'yes', '7-14 working days with 2 man van', 'yes'),
(2, 'G06323', 1, 'Dark Deborah Oak Dining Table ', '-Dark-Deborah-Oak-Dining-Table- ', '780.00', 'latest oak brand', 'This is our latest supply', 'oak', '231445010.jpg', 604325, 'C173649', '700.00', '80.00', 'yes', '7-14 working days with 2 man van', 'yes'),
(3, 'G73587', 1, 'Ellis Light Oak Extending Dining Table ', '-Ellis-Light-Oak-Extending-Dining-Table- ', '930.00', 'latest oak brand', 'This is our latest supply', 'oak', '231445010.jpg', 604325, 'C173649', '893.00', '37.00', 'yes', '7-14 working days with 2 man van', 'yes'),
(4, 'G06323', 1, 'Dark Deborah Oak Dining Table ', '-Dark-Deborah-Oak-Dining-Table- ', '780.00', 'latest oak brand', 'This is our latest supply', 'oak', '231445010.jpg', 604325, 'C173649', '700.00', '80.00', 'yes', '7-14 working days with 2 man van', 'no');


Looking forward to some help

Many thanks

#2

[eluser]bibos[/eluser]
HI,
Quote:...
The model - Its the home page of the site I’m working on.
...

you should probably read some tutorial before starting coding, a model can't be your home page!

you can start here : http://codeigniter.com/tutorials/
and
http://www.youtube.com/watch?v=gKg_QtlmZfc
or http://www.youtube.com/watch?v=n9mGR0Gbb...EFD13DA49E

you should alsow try to learn some docs about MVC pattern to be able to undrestand what's happend.

GOOD LUCK

#3

[eluser]CroNiX[/eluser]
Your files should be named with .php extension, not .wtp. They have to be php files so that the php will get parsed.

They load like this (leaving extension off):
Code:
$page = 'something';  //filename is actually something.php
$this->load->view('pages/' . $page ,$data);

I didn't read all of your code (please use code tags), but that is what stood out to me.
#4

[eluser]CroNiX[/eluser]
Also, you should load database in the controller or in autoload. Not sure how loading it in the model will work. If you will be using the database in most or all of your controllers, you might as well just load it once in the autoload so it will be available everywhere. Same with session or anything else you will be using a lot.




Theme © iAndrew 2016 - Forum software by © MyBB