I am trying to display the images of my Houses from the folder but it shows the error like this
http://localhost/jageerx/assets/images/House/assets/images/House28278954_957081961107785_391331158313648576_n.jpg
404 (Not Found)
My code is below
<?php
$PropertyType = "";
$image = "";
if($data != null)
{
foreach($data as $key=>$value)
{
$PropertyType = $value['PropertyType'];
$image=$value['HouseImage1'];
}
}
?>
the front-end code is this
<img src="<?php echo base_url('assets/images/House/'. $image);?>" alt="tab1" class="img img-responsive">
folders hierarchy is mentioned below
this is the model function
public function SIngleHouseADD($houseID)
{
$this->db->select('*');
$this->db->from('housedetail');
$this->db->where('HouseID', $houseID);
//$qry = $qry->result_array();
$query=$this->db->get();
$resultArray = $query->result_array();
return $resultArray;
//return $qry;
}
this one is the controller
public function SingleProperty()
{
//$HouseID = $_GET['id'];
$this->load->model('SingleAddModel');
$plots = $this->SingleAddModel->SIngleHouseADD($_GET['id']);
$data = array();
$data["data"] = $plots;
$this->load->view('SinglePropertyDetail_view', $data);
}
In base_url() you are passing assets/images/House/ the same path in $image, so it is duplicating path, I'll suggest you to first echo the results then use them in html. And for your Image you can use:
<img src="<?php echo base_url().$image;?>" alt="tab1" class="img img-responsive">
OR
<img src="assets/images/House/<?php echo $image;?>" alt="tab1" class="img img-responsive">
Hope this will help you :
Since your $image variable already contains assets/images/House so should not be use in base_url() again
Should be like this :
foreach($data as $key=>$value) {
$PropertyType = $value['PropertyType'];
$image=$value['HouseImage1'];
?>
<img src="<?php echo base_url().$image;?>" alt="tab1" class="img img-responsive">
<?php }?>
I don't know if this works best for you but when I add images or css in codeigniter I usually add them in the same level as my index.php so it goes like this!
See screenshot.
screenshot
Related
What I am trying to do is something like below:
The buttons at the side can be ignored. So the idea is that the big square is the main video playing and the little ones underneath will be the thumbnails of the remaining videos. The main video playing is randomly picked when the site is loaded and then the ones that are not playing, the thumbnails will be shown underneath. I am doing this like so:
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
echo $video;
array_push($items, $video);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $items[0];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
foreach($restVideos as $videoThumbnail) {
try {
$stmt = $dbconn->query("SELECT thumbnail, videoTitle FROM videos WHERE video = '$videoThumbnail'");
while($row = $stmt->fetch()){
$thumbnail = $row['thumbnail'];
array_push($thumbnails, $thumbnail);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
}
foreach($thumbnails as $nonPlaying) {
?>
<img src=<?php echo $nonPlaying ?> id="thumbnails" width="200" height="100">
<?php } ?>
what is happening at the moment is below:
Each image is just stacking up against each other how can I change it so this is not the case?
Also is there a more efficient way of doing this than what I am currently doing?
Edit:
$thumbnails = array();
$videoTitles = array();
$test = 700;
echo "<div>";
foreach($restVideos as $rVideo) {
$test = 200 + $test;
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100" style= "left: <?php echo $test?>px">
<?php
echo "</div>";
}
You not required to two query two times to db .
Use key value and store the entire video. While printing thumbnail based on the key (unique id ex. videoid). You can retrieve the content when you print the thumbnail list and can avoid querying again in a loop.
Ref to code snipp
<?php
try {
$items = array();
$stmt = $dbconn->query('SELECT videoid, video, thumbnail, videotitle, tags, editedby FROM videos ORDER BY RAND()');
while($row = $stmt->fetch()){
$video = $row['video'];
$videoMap[$row['videoid']] = $row;
$videoThumbnail = $row['thumbnail'];
$videoTitle = $row['videotitle'];
if (count($items) == 0) echo $video;
array_push($items, $row['videoid']);
$video = $row['video'];
}
shuffle($items);
$restVideos = array();
foreach($items as $key => $videoArray) {
if($key === 0) continue;
array_push($restVideos, $videoArray);
}
}catch(PDOException $e) {
echo $e->getMessage();
}
?>
<video id=v controls loop align="right">
<source src="users/videos/<?php echo $videoMap[$items[0]]['video'];?>" type="video/mp4">
</video>
<?php
$thumbnails = array();
$videoTitles = array();
echo "<div>";
foreach($restVideos as $rVideo) {
?>
<img src=<?php echo $videoMap[$rVideo]['thumbnail'] ?> id="thumbnails" width="200" height="100">
<?php
}
echo "</div>";
}
?>
Use a div tag and print the image thumbnails.
`hi guys am trying from 4 days am not getting . how to retrieve multiple images from using explode functionality.Now I need to explode that image in view . i getting single images from an array but not retrieveing multiple images in my database have one row contains three images how to return result from model.
and show in the view file
myview file:
`<div class="carousel-inner" >
<?php echo $this->session->flashdata('message')?>
<?php $item_class = 'active';
foreach($newzw as $result){?>
<div class="item <?php echo $item_class; ?> ">
<div class="overlay"></div>
<?php $img=explode(",",base_url().'uploads/images/'.$result->image);
//print_r($img);exit;
?>
<img src="<?php echo $img[0] ?> " alt="Los Angeles" style="height:662px;" >
</div>
<?php $item_class = '';
} ?>
</div>
<!-- Left and right controls -->
</div>
</div>
my model code:
function uploadslide1()
{
$this->db->select('*');
$this->db->order_by("id", "DESC");
$query = $this->db->get("sg_slides");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
controller:
function index()
{
$data=$this->general();
$data['newzw']=$this->public_model->uploadslide1();
$data['body']="static/body";
$this->load->view('welcome',$data);
}
I'm just telling you how to using explode.
The explode() function breaks a string into an array. If you want to break your image value into an array format then you have to do this
explode(separator,string); // Syntax
In your case , plays a role of separator.
$img = explode(",", $result->image); //store image value as an array format in img
Now you have the $img variable as an array now you can perform itteration using foreach
example:
foreach($img as $src){
echo '<img src="'.base_url().'uploads/images/'.trim($src)." />';
}
In database table there is no rows means getting error Undefined variable: results using codeigniter,in case there is no row or empty table is there means i want to display view page
controller
$data['breview'] = $this->Profile_model->review();
$this->load->view('supplierreview', $data);
Model
public function review() {
$this->db->select('*');
$this->db->from('reviews');
$this->db->join('supplier_otherdetails', 'supplier_otherdetails.supplierid_fk = reviews.supplier_id');
$this->db->join('customer_registration', 'reviews.customer_id=customer_registration.id');
$this->db->join('sub3_category', 'reviews.product_id=sub3_category.id');
$this->db->where('supplierid_fk', $this->session->id);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$results = $query->result();
}
return $results;
}
view page
<?php
foreach ($breview as $row) {
?>
<div class="reviewsection">
<img src="<?php echo 'data:image;base64,' .$row->product_image; ?>" class="img-circle img-user" alt="" width="50px;" height="50px;"/>
<span class="starfont"><botton class="btn btn-danger"> <?php echo $row->ratings; ?> <span class="fa fa-star starfont"></span></botton> </span>
<div class="content-left">
<p><b>Product Name:<?php echo $row->product_name; ?></b></p>
<p><?php echo $row->review_msg; ?></p>
<?php $buyer_review = strtotime($row->review_date);?>
<?php $date=date('d-F-Y',$buyer_review); ?>
<p>Buyer Name : <?php echo $row->first_name; ?> <?php echo $date ; ?></p>
</div>
</div>
<?php } ?>
$results is not defined.
Please add $results = [];
Here you go:
$results = [];
if ($query->num_rows() > 0) {
$results = $query->result();
}
return $results;
In my model, in most cases, i do the following
public function my_function() {
//$qry = YOUR_QUERY
if ($qry->num_rows() > 0) //or ==1 or whatever, depends on your structure
return $qry->result_array(); // or row_array, depends on your structure
return FALSE;
}
Then in your controller you can check if the result is FALSE or EMPTY like:
$result_from_model = $this->my_model->my_function();
if($result_from_model && !empty($result_from_model)) {
//your code here
}
You can use the following code :
return (is_array($results)?$results:array());
Hope it works.
All the answers given so far are good. Here is yet another way to do it. It's essentially the same as the accepted answer only using a ternary instead of if.
$query = $this->db->get();
return $query->num_rows() > 0 ? $query->result() : array();
}
I have another error anymore, when I try to display a result in my view, the result is NULL and I can't see the result of my query at models.
Here's my code list :
on the controller (home.php) :
$data['hasil5'] = $this->home_model->popular_list();
on the model (home_model.php) function popular_list() :
function popular_list($limit=2)
{
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
return $query->result();
} //thanks to kumar_v
and on my view (home.php) as a part of "Popular news" :
<h2>Most Popular News :</h2>
<?php
foreach ($hasil5 as $data5):
?>
<div class="welcome clear"><img class="imgl" src="<?php echo base_url(); ?>assets/news/original/<?php echo $data5->image; ?>" alt="" height="119" width="125" />
<div class="fl_right">
<h2><?php echo anchor($data5->kategori.'/detail/'.$data5->id,$data5->title) ?></h2>
<p><?php echo $data5->sinopsis; ?></p>
</div>
</div>
<?php
endforeach;
?>
The results is NULL, can you correct it again? thanks..
try this :
function popular_list($limit = 2) {
$query = $this->db->select('*')
->where('id',$this->uri->segment(3))
->where('publish',1)
->where('viewed >= ',5)
->order_by('id','DESC')
->limit($limit)
->get('news');
print_r($query->result());
return $query->result();
}
You can put the whole thing in $query variable. And also, you can improve your $this->db->where() by putting the params in an array.
You should fix your code to look like so:
<?php
foreach ($data['hasil5'] as $data5):
?>
You were using the wrong variable in your foreach loop.
Checking if the query will return any row is a good idea.
function popular_list($limit=2)
{
$result = null;
$this->db->select('news.*');
$this->db->where('id',$this->uri->segment(3));
$this->db->where('publish',1);
$this->db->where('viewed >= ',5);
$this->db->order_by('id','DESC');
$this->db->limit($limit);
$query = $this->db->get('news');
if($query->num_rows() > 0)
{
$result = $query->result();
}
return $result;
}
I am trying to make it so that each time a user session is loaded a random image is displayed from the directory. Like an advert.
At the moment the image changes each page refresh, this doesn't really help me because as a user goes from page to page the constant refresh of images becomes annoying.
Here's what I have so far?
Please can anyone point out the piece of code I need to do what I need to do.
<?php
$path_to_images = "../PTB1/data/adverts/"; // path for images
$default_img = "test.png"; // default image, when error on page
function getRandomImage($path, $img) {
if ( $list = getImagesList($path) ) {
mt_srand( (double)microtime() * 1000000 );
$num = array_rand($list);
$img = $list[$num];
}
return $path . $img;
}
function getImagesList($path) {
$ctr = 0;
if(!isset($_SESSION['id']));
if ( $img_dir = #opendir($path) ) {
while ( false !== ($img_file = readdir($img_dir)) ) {
// formati slik, ki jih prepozna
if ( preg_match("/(\.gif|\.jpg|\.png)$/", $img_file) ) {
$images[$ctr] = $img_file;
$ctr++;
}
}
closedir($img_dir);
return $images;
}
return false;
}
?>
<div class=\"advert-box\" id=\"mod-advert\">
<img src="<?php echo getRandomImage($path_to_images, $default_img) ?>" height="190" width="180"alt="">
</div>
Did you try creating a session variable?
<?php
session_start();
// check if a image is already present
// no need to create again
if(!isset($_session['image'])){
$_session['image'] = getRandomImage($path_to_images, $default_img);
}
....
....
<div class=\"advert-box\" id=\"mod-advert\">
<img src="<?php echo $_session['image'] ?>" height="190" width="180"alt="">
</div>
first step, do the image selection when user LOGIN:
session_start();
if(!isset($_SESSION['image'])){
$_SESSION['image'] = getRandomImage($path_to_images, $default_img);
}
next, on every page, you just have to look at the $_SESSION['image'] variable:
<img src="<?php echo $_SESSION['image']; ?>" height="190" width="180"alt="">