Welcome Guest, Not a member yet? Register   Sign In
Fetching Image Paths from Separate table in a foreach loop
#1

[eluser]ridley1012[/eluser]
Hi hoping someone can help,

i'm currently working on displaying data about vehicles which is pulled from a vehicles table on my db.

I have this working fine in a foreach loop following the MVC setup.

I have a separate table for the image paths which are linked using the vehicle_index. multiple images can be uploaded and one of those images can be selected to be the main image. The problem I have is calling the main image to display in the view whilst the foreach loop is running. my code is pretty basic but its shown below. any help would be great.


MODEL
Code:
class Vehicle_mod extends CI_Model {

     function get_all_cars()
     {
$query = $this->db->from('vehicles')->where('vehicle_type_index', '1');
$query = $this->db->order_by("vehicle_year", "desc");
$query = $this->db->get();
return $query->result();
     }


}


CONTROLLER
Code:
<?php
class cars extends MY_Controller {

function index()
{
  $data['title'] = $this->company_name . " - Car Showroom";
  $data['page_title'] = "CAR SHOWROOM";
  $data['page'] = "cars";

  $this->load->model('vehicle_mod', 'vehicles');
  $data['results'] = $this->vehicles->get_all_cars();

  $this->load->view('includes/header.php', $data);
  $this->load->view('cars.php');
  $this->load->view('includes/footer.php');
}
}
?>

VIEW
Code:
<?php
foreach ($results as $item):
?>
  <div id="list_item">
    <div class="list_photo"></div>
    <div class="list_desc">
      <div class="list_title"><a href="/car_details/&lt;?=$item-&gt;vehicle_index;?&gt;">&lt;?php echo $item->vehicle_year . " " . $item->vehicle_make . " " . $item->vehicle_model . " " . $item->vehicle_derivative;?&gt;</a></div>
      <p>&lt;?php echo substr($item->vehicle_description, 0, 220) . "&nbsp;&nbsp;&nbsp;<a >read more...</a>";?&gt;</p>
      <table width="100%">
        <tr>
          <td><strong>Mileage:</strong>
            &lt;?=$item->vehicle_mileage;?&gt;</td>
          <td><strong>Transmission:</strong>
            &lt;?=$item->vehicle_transmission;?&gt;</td>
        </tr>
        <tr>
          <td><strong>Colour:</strong>
            &lt;?=$item->vehicle_colour;?&gt;</td>
          <td><strong>Body Type:</strong>
            &lt;?=$item->vehicle_body_type;?&gt;</td>
        </tr>
        <tr>
          <td><strong>Fuel Type:</strong>
            &lt;?=$item->vehicle_fuel_type;?&gt;</td>
          <td><strong>Price:</strong>
            &lt;?="£" . number_format($item->vehicle_price, 2, '.', '');?&gt;</td>
        </tr>
      </table>
    </div>
    <br clear="all" />
  </div>
  &lt;?php
endforeach;
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB