Showing dynamic items list using two tables in codeigniter php - php

I have two tables one having advert info which includes id for images table associated with the advert. I am showing the advert information in one single page, how do I send both arrays to the view page. If I write the select for both tables, I get multiple lines since I have multiple images for an advert.
My model functions
public function get_adverts()
{
//$this->db->select();
//$this->db->from('adverts');
$db_query='select adverts.id,adverts.address,adverts.title,adverts.item_condition, adverts.add_type, adverts.price, adverts.negotiable, adverts.product_description, countries.country_name,states.state_name, cities.city_name, product_pictures.picture_file_name
FROM adverts,countries,cities,states, product_pictures
WHERE adverts.country_id=countries.id and adverts.city_id=states.id and adverts.area=cities.id and adverts.id= product_pictures.add_id';
$query = $this->db->query($db_query);
return $query->result();
}
public function get_pictures()
{
//$this->db->select();
//$this->db->from('adverts');
$db_query='select product_pictures.picture_file_name
FROM adverts,product_pictures
WHERE adverts.id=product_pictures.add_id';
$query = $this->db->query($db_query);
return $query->result();
}
and my control
public function get_products()
{
// gets all dvertes in the system
$this->load->model('Product_model');
$data['products']=$this->Product_model->get_adverts(); // get database query output to the array
$data['images']=$this->Product_model->get_pictures();
$array_info2 = array(
"title" => "Not Found"
);
$this->load->view('include/header', $array_info2);
$this->load->view('include/navbar_logged_in');
$this->load->view('Site/view_adverts',$data);
$this->load->view('include/footer');
}
This lists all images under one advert, my view portion given below:
<div class="container">
<?php foreach($products as $product):?>
<div class="ad-listing-list mt-20">
<div class="row p-lg-3 p-sm-5 p-4">
<div class="col-lg-4 align-self-center">
<?php foreach($images as $image):?>
<img class="image img-thumbnail" src="<?php echo base_url('/uploads/advert_images/thumbs/'.$image->picture_file_name);?>" >
<?php endforeach;?>
</div>
<div class="col-lg-8">
<div class="row">
<div class="col-lg-6 col-md-10">
<div class="ad-listing-content">
<div>
<?php echo $product->id." ".$product->title;?>
</div>
<ul class="list-inline mt-2 mb-3">
<li class="list-inline-item"> <i class="fa fa-folder-open-o"></i> Electronics</li>
<li class="list-inline-item"><i class="fa fa-calendar"></i>26th December</li>
</ul>
<p class="pr-5"><?php echo $product->product_description;?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endforeach;?>
</div>

Related

laravel show method doesn't have the style

I'm new on laravel and i'm trying to make on-many relationship between category table and devices table on database
in Category view i have all the category and i want when i clik to one category to go to another view and show all the devices of this category
It work's with my using rsource controller for the category and in the show method : CatgoryController.php
public function show($id)
{
$categories = Category::find($id);
$all_devices = $categories->devices;
return view('devices',compact('categories',$categories,'all_devices',$all_devices));
}
and in the devices view i do:
devices.blade.php
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-12 text-center animateup">
<div class="inline-icons-text section-heading">
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
<span class="inline-icon heading-width">
<span class="heading-text"><h1> {{$categories->category_name}} </h1> </span>
</span>
<div class="inline-icon">
<hr/><hr/><hr/><hr/><hr/>
</div>
</div>
</div>
</div>
<br/>
<div class="row text-center">
#foreach ($all_devices as $device)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$device->device_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
</div>
#stop
this is how i show all the category and when click on one it goes to anthor view to show the devics of this category
categories.blade.php
<div class="row text-center">
#foreach ($categories as $categories)
<div class="col-md-3 col-sm-6 animateleft">
<h5>
{{$categories->category_name}}
<span class="glyphicon glyphicon-chevron-right"></span>
</h5>
</div>
#endforeach
</div>
and finally this my rout Rout.php
Route::resource('categories','CategoryController');
Route::resource('/devices','DevicesController');
My Problem is that evry thing work but the page that have the devices of one category dosen't have the style of the site
move ur stylesto public folder into any subfolder..here i'm using devices as sub-folder and devices.css is my file..
place link like this in ur master blade.with ur folder and file names
<link rel="stylesheet" href="{{URL::to('/')}}/devices/devices.css">

image slider using explode function

I'm working on a product display slider which displays product images from database
Now I am trying to use explode function to display one by one
problem is that all images a showing in one go rather then one by one
here is the code
<div class="col-md-9">
<div class="col-md-5 grid">
<div class="flexslider">
<?php $photos = explode(',',$products['image']);
foreach($photos as $photo):?>
<ul class="slides">
<li data-thumb="<?= $photo; ?>">
<div class="thumb-image"> <img src="<?= $photo; ?>" data-imagezoom="true" class="img-responsive"> </div>
</li>
</ul>
<?php endforeach; ?>
</div>
</div>

I need to display 3 unique products in each div

I need to display 3 unique products in each div, like this: Sample output
How can I get the different products in one loop? At present in the below code, I'm getting the same product 3 times.
<?php
foreach ($rows as $display) {
?>
<div class="col-md-4 col-sm-6">
<div class="section-title">
<div class="pull-right pr-icon">
<i class="fa fa-diamond"></i> Top Suppliers
</div>
<div class="main-title">
<b> Selected Products</b>
</div>
<div class="sub-title">
Source the latest items
</div>
</div>
<div class="row">
<div class="col-md-4">
<img src="<?php echo 'data:image;base64,'.$display->image; ?>" class="img-responsive image product-item" />
<div class="product-description">
<p class="text-center"><?php echo $display->name; ?></p>
<p class="desc-btn">VIEW</p>
</div>
</div>
</div>
</div>
<?php
}
?>
From controller:
public function index()
{
$this->load->database();
$data['rows']=$this->homemodel->fetchData();
$this->load->view('home',$data);
$this->load->library('image_lib');
}
Model code:
<?php
class homemodel extends CI model {
function __construct()
{
parent::__construct();
}
public function fetchData()
{
$query=$this->db->select('*');
$this->db->from('display');
$query = $this->db->get();
return $query->result();
}
}
?>
<?php
$chunkedArray = array_chunk($rows, 3); //productList Array, number of chunks/parts
foreach($chunkedArray as $newRow) {
echo '<div class="row">';
foreach ($newRow as $display) {
?>
<div class="col-md-4 col-sm-6">
<div class="section-title">
<div class="pull-right pr-icon">
<i class="fa fa-diamond"></i> Top Suppliers
</div>
<div class="main-title">
<b> Selected Products</b>
</div>
<div class="sub-title">
Source the latest items
</div>
</div>
<div class="row">
<div class="col-md-4">
<img src="<?php echo 'data:image;base64,'.$display->image; ?>" class="img-responsive image product-item" />
<div class="product-description">
<p class="text-center"><?php echo $display->name; ?></p>
<p class="desc-btn">VIEW</p>
</div>
</div>
</div>
</div>
<?php
}
echo '</div>';
}
?>
array_chunk would do the work..
know more on array_chunk here

How can I echo in else that "The searched business does not exist"?

My last question was how to access model's data in one of its view after got successful answer I implemented it. This was my question
Now its working good, but how can I echo that when a user search a business, which does not exist in the database, the system echo
Business not exist
I tried to insert else in the foreach loop, but it echoes "Business not exist" many times.
I am posting user searched business name in my landing.php which is my layout to searching view.
I am pasting my Landing.php, Business controller and the view file which I called Searching
My landing.php:
<form action="business/searching" method="POST">
<div class="form-group form-group-lg">
<h2 class="title">
Find the best places to eat, drink, shop, or visit in Islamabad.
</h2>
<div class="col-sm-5 col-md-5 col-lg-5 col-md-offset-1">
<input type="text" class="form-control" name="business" id="lg" placeholder="I'm looking for...."/>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="text" class="form-control" id="sm" placeholder="Islamabad" disabled=""/>
</div>
<div class="col-sm-3 col-md-3 col-lg-3">
<input type="submit" class="btn btn-primary btn-lg"/>
</div>
</div>
</form>
In my Business controller
public function actionSearching()
{
$models = Business::model()->findAll();
$modeladd = Address::model()->findAll();
$this->layout='//layouts/main';
$this->render(
'searching',
array(
'models' => $models,
'modeladd' => $modeladd,
)
);
}
Searching view Its bit complicated but on ask i can explain.
<div class="gap"> </div>
<div class="container">
<div class="row">
<div class="col-md-9">
<ul class="booking-list">
<li>
<?php foreach($models as $model) //getting business model from searching action in business controller
{
$name=$model->business_name; //storing name
$id=$model->id; //storing id in order to use this id in address if as addres has no business_name
if($name== $_POST["business"]) //matching name
{ ?>
<a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
<div class="row">
<div class="col-md-5">
<img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
</div>
<div class="col-md-7">
<div class="booking-item-rating">
</div>
<h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
<?php foreach($modeladd as $mo) //getting address model from searching action in business controller
{
$addbizid=$mo->business_id; //businessid from address table
if($id== $addbizid) //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
// from above after the searched business name is matched and now comparing that id with addres->businessid
{
$street=$mo->street_number; //if match store address in these variable and then simple print them below
$sector=$mo->sector;
$city=$mo->city; ?>
<p class="booking-item-address"><i class="fa fa-map-marker"></i> <?php echo $street; echo $sector; ;echo $city;?> <?php }} ?></p>
<p class="booking-item-description"> <?php echo $model->business_description;?>
</p>
</div>
</div>
<?php }} ?>
</a>
</li>
</ul>
</div>
<div class="col-md-3">
<div class="booking-item-dates-change mb30">
<h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
<i class="fa fa-plus"></i> Add Business Page
</div>
</div>
</div>
<div class="gap"></div>
</div>
<div class="gap"></div>
</div>
You can use endif, end foreach loop if you want to use if else in foreach loop.
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//echo $_POST["business"]; echo "</br>";
//foreach($modeladd as $mo)
// {
//
// $street=$mo->street_number;
//
// $address=$mo->sector;
//
// $city=$mo->city;
// $business_id=$mo->business_id;
// echo $street; echo ("<br />");echo $address; echo ("<br />");echo $city; echo ("<br />");echo $business_id;
//}
?>
<?php $matchFound = false; ?>
<div class="gap"> </div>
<div class="container">
<div class="row">
<div class="col-md-9">
<ul class="booking-list">
<li>
<?php foreach($models as $model): ?>
<?php //removing foreach outerloop
$name=$model->business_name; //storing name
$id=$model->id; //storing id in order to use this id in address if as addres has no business_name
?>
<?php if($name== $_POST["business"]): //matching name
$matchFound = true; //removing if match
?>
<a class="booking-item" href="<?php Yii::app()->request->baseUrl;?>/business/userbusiness/<?php echo $id?>">
<div class="row">
<div class="col-md-5">
<img src="<?php Yii::app()->request->baseUrl;?>/img/<?php echo $model->image;?>" alt="Not availble" title="Image business" />
</div>
<div class="col-md-7">
<div class="booking-item-rating">
</div>
<h5 class="booking-item-title"><?php echo $model->business_name; ?></h5>
<?php foreach($modeladd as $mo) //getting address model from searching action in business controller
{
$addbizid=$mo->business_id; //businessid from address table
if($id== $addbizid) //wanted to match business name, but couldnot as addres has no business name only business_id, so i get business_id
// from above after the searched business name is matched and now comparing that id with addres->businessid
{
$street=$mo->street_number; //if match store address in these variable and then simple print them below
$sector=$mo->sector;
$city=$mo->city; ?>
<p class="booking-item-address"><i class="fa fa-map-marker"></i>
<?php echo $street; echo $sector; ;echo $city;?>
<?php
}
}
?>
</p>
<p class="booking-item-description"> <?php echo $model->business_description;?>
</p>
</div>
</div>
<?php
break;
endif; //removing if matc outer loop
endforeach; //removing for each outer loop
?>
<?php if (!$matchFound): ?>
<?php echo "No data found for the room number you entered."; ?>
<?php endif; ?>
</a>
</li>
</ul>
</div>
<div class="col-md-3">
<div class="booking-item-dates-change mb30">
<h3>Not here? Tell us what we're missing.</h3><p>If the business you're looking for isn't here, add it!</p>
<i class="fa fa-plus"></i> Add Business Page
</div>
</div>
</div>
<div class="gap"></div>
</div>
<div class="gap"></div>
</div>

Creat individual .php?id= links for each of the products in products.php file

On my products page, there are several different product grids(created with bootstrap) in which, all of my products are displayed. What I am trying to do is to create detailed pages for each of the products the page has.
products.php:
<?php
session_start();
include('inc/config.php');
?>
<html>
<body>
some content
<div class="row">
<?php
$query=mysql_query("select*from cars");
while($data=mysql_fetch_assoc($query)){
?>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="text-center">
<?php echo "<h2>".$data['make']." ".$data['model']." </h2>"; ?></h4>
</div>
<div class="panel-body text-center">
<p class="lead">
<?php echo '<img src="cartest/'.$data['image'].'" class="img-responsive" />'; ?></p>
</div>
<ul class="list-group list-group-flush text-center">
<li class="list-group-item"><i class="icon-ok text-danger"></i><?php echo "<h4>".$data['price']." </h4>"; ?></li>
<li class="list-group-item"><i class="icon-ok text-danger"></i>Unlimited projects</li>
<li class="list-group-item"><i class="icon-ok text-danger"></i>27/7 support</li>
</ul>
<div class="panel-footer">
<?php echo '<a class="btn btn-lg btn-block btn-default" a href="product-details.php?id='.$data['carId'].'">Details</a>';?>
</div>
</div>
</div>
<? } ?>
</div>
</body>
</html>
product-details.php:
<?php
session_start();
include('inc/config.php');
?>
<html>
<body>
some content
<div class="container-fluid">
<?php
$id = $_GET['carId'];
$query = mysql_query("SELECT * FROM cars WHERE carId='$id'") or die(mysql_error());
$data = mysql_fetch_array($query);
?>
<div>
<?php echo '<img alt="" src="cartest/'.$data['image'].'"></a>'; ?>
</div>
As you can see, I managed to create individual links for each of the products but is there a way of displaying the individual pages using something like a main template which its gonna change the content section according to the product that has been clicked? or do i have to create countless pages for each of the products?
Thanks in advance
If you want to change something in template, you need to prefer it.
for.eg. for unique background you can use something like this:
<div id="product" style="background: url($mybgurl)"></div>
In this case $mybgurl it's url for this product's image, fetched from db.
P.S: Google for MVC or template engine.

Categories