Yii: Retrieving data using Cdbcriteria - php

I am yiibie. What I am trying to do is to retrieve data from my database, and for that i am using cDbCriteria. I want to get the last 4 images from my database table which is named Event. and then want to display those 4 images individually. I am using this method but not getting any result, please help me with this.
<?php
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "image DESC";
$Criteria->select = "id, image";
$Events = Event::model()->findAll($Criteria);
foreach ( (array)$Events as $Event)
{
$Event[1]=$image1;
$Event[2]=$image2;
$Event[3]=$image3;
$Event[4]=$image4;
}
?>
<div class="row">
<h3>Events</h3>
<div class="col-md-3">
<div class="thumbnail">
<img src="<?php echo $image1 ?>">
<div class="caption">
<button class="btn btn-primary center-block">Join</button>
</div>
</div>
</div>

findAll() method retrieves an array of ActiveRecords, so you need a foreach loop. Try:
foreach ( (array)$Events as $Event)
{
echo "
<div class='col-md-3'>
<div class='thumbnail'>
<img src='$Event->image' >
<div class='caption'>
<a href='join.php'><button class='btn btn-primary center-block'>Join</button></a>
</div>
</div>
</div>
";
}

$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, image";
$Events= Event::model()->findAll($Criteria);
This will gives last 4 records
1st way
if(isset($Events[0]))
echo $Events[0]->image;
if(isset($Events[1]))
echo $Events[1]->image;
if(isset($Events[2]))
echo $Events[2]->image;
if(isset($Events[3]))
echo $Events[3]->image;
i am here checking isset() because there may b 3 entry. This is static
2nd way
foreach($Events as $event)
{
echo $event->image;
}
this is dynamic binding.

Related

Images not loading on page when I call the view using a different model function

I have created a method in a model to call all events from a database with images - this worked fine I then wanted to find ones that match a postcode so created another function which is called when a search bar is filled in and submitted. This other function works and loads all info from database except the images even the one that is hardcoded in the index page - I can't work out what I have done wrong or why it won't work.
I have tested the code in phpMyAdmin and the function returns the image-path, the index page must be okay because it returns everything on the other function which leaves the controller but i can't see anything wrong there either.
The model function code:
function FindEventByPostcode($id){
$this->db->query("SELECT
concert.concert_id as concertId,
event_type.id as typeId,
concert.type_id as typeCId,
concert.concert_name as concertName,
concert.date as concertDate,
users.name as userName,
concert.date as date,
concert.post_code as post_code,
concert.info as info,
images.image_path as imagePath,
event_type.type as type
FROM concert
JOIN event_type
ON concert.type_id = event_type.id
JOIN users ON
users.id = concert.user_id
JOIN images ON concert.image_id = images.id
WHERE concert.post_code = :post_code
ORDER BY concert.date DESC"
);
$this->db->bind(':post_code', $id);
$results = $this->db->resultSet();
return $results;
}
The controller code:
$events = $this->eventModel->getEventTypes();
class Pages extends Controller {
public function __construct(){
$this->eventModel = $this->model('Event');
$this->userModel = $this->model('User');
$this->pageModel = $this->model('Page');
}
public function index(){
if(isLoggedIn()){
redirect('profiles');
}
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$postcode = ($_REQUEST['post_code']);
$event = $this->pageModel->FindEventByPostcode($postcode);
$data =[
'title' => 'Forthcoming Events',
'events' => $event
];
$this->view('pages/index', $data);
}else{
$data = [
'title' => 'Forthcoming Events',
'events' => $events
];
$this->view('pages/index', $data);
}
}
}
The Index Page:
<?php require_once APPROOT . '/views/inc/header.php' ?>
<div>
<p>Event Finder</p>
<form name="postcode" method="post"
action="<?php echo URLROOT; ? >/pages/index">
<input type="text" name='post_code'/>
<input type="submit" value="find event">
</form>
</div>
<div class="col-md-6">
<h1><?php echo $data['title'] ?></h1>
</div>
<img src="img/concertBanner.jpg" style="width:100%"/>
<?php foreach($data['events'] as $post) : ?>
<div class="card card-body mb-3">
<img class="card-img-top" src="img/<?php echo $post->userName; ?>/
<?php echo $post->imagePath; ?>"
style="width:50%; height:50%;" alt="Card image cap">
<div class="card-body">
<h4 class="card-title"><?php echo $post->concertName; ?></h4>
<p class="card-text"> Event Info: <?php echo $post->info ?></p>
<p class="card-text"><?php echo $post->type ?></p>
<p></p>
<div class="bg-light p-2 mb-3">
Organised by <?php echo $post->userName; ?>
on <?php echo $post->date; ?>
</div>
<p class="card-text"><?php echo $post->concertName; ?> in
<?php echo $post->post_code; ?> </p>
<a href="<?php echo URLROOT; ?>/pages/show/
<?php echo $post->concertId;?>" class="btn btn-dark">More</a>
</div>
</div>
No error messages just a broken image link instead of the image.

$_SESSION: Undefined Index

I'm creating a booking site for meeting rooms.
I have done the sql query to search for free rooms according to the time and date chosen.
This is working. I display the free rooms (in a while loop) in cards with datas from the query.
<?php
if (isset($_POST['submit'])) {
echo '<main role="main">
<div class="card-columns">';
require_once 'connexion_BD.php';
$res_date = $_POST['res_date'];
$res_date = date("Y-m-d", strtotime($res_date));
$res_horaire_id = $_POST['res_horaire'];
$_SESSION['res_date'] = $_POST['res_date'];
$_SESSION['res_horaire'] = $_POST['res_horaire'];
$sql = "SELECT sal_nom, sal_nb_place, sal_notes
FROM salle
WHERE sal_id NOT IN (SELECT res_sal_id FROM reservation WHERE res_date = :res_date AND res_horaire_id = :res_horaire_id)";
$requete = $db->prepare($sql);
$requete->bindValue(':res_date', $res_date);
$requete->bindValue(':res_horaire_id', $res_horaire_id);
$requete->execute();
$i=0;
while ($row = $requete->fetch()) {
$tab_sal_nom[0+$i] = $row['sal_nom'];
$tab_sal_nb_place[0+$i] = $row['sal_nb_place'];
$tab_sal_notes[0+$i] = $row['sal_notes'];
?>
<div class="card bg-light">
<img class="card-img-top" <?php echo 'src="img/salle_reunion_'. rand(1, 8) .'.jpg"' ?> alt="Salle de réunion">
<div class="card-body">
<h5 class="card-title">Salle <?php echo $tab_sal_nom[0+$i]; ?></h5>
<p class="card-text">Salle disposant de <?php echo $tab_sal_nb_place[0+$i]; ?> places.</p>
<p class="card-text"><small class="text-muted">Informations de salle : <?php echo $tab_sal_notes[0+$i]; ?></small></p>
<form action="confirm_resa.php" method="POST">
<button class="btn btn-outline-primary float-right" style="margin-bottom: 15px;" name=<?php echo '"reserver_'.$tab_sal_nom[0+$i].'"'?> type="submit">Réserver</button>
</form>
</div>
</div>
<?php
$i = $i + 1;
}
}
?>
Now here's my problem, I want to know which room name the user choose to store the info in a $_SESSION['sal_choix'], so, I look at the submit button named <?php echo '"reserver_'.$tab_sal_nom[0+$i].'"'?> (equals to reserver_Daum, reserver_Majorelle, reserver_Galle, reserver_Corbin etc...)
My idea was to look with some if:
if (isset($_POST['reserver_Daum'])) $_SESSION['sal_choix'] = "Daum";
elseif (isset($_POST['reserver_Galle'])) $_SESSION['sal_choix'] = "Galle";
etc...
But that's not working, cause in the confirm_resa.php, $_SESSION['sal_choix'] is: undefined index
It's not a session problem because two other : $_SESSION['res_date'] and $_SESSION['res_horaire'] are sets.
I'm pretty sure the problem is in the if(isset($_POST[])) but I can't see it...
help pls!

How to limit the number of 'Recent Blogs' created in CakePHP?

I'm trying to create a blog, and in every single blog it shows the recent blogs that has been created on the bottom of the page. Is there a way i can limit this number to 4 recent blogs? Because currently all the blogs that has been created show's up on the "Recent Blogs" area when i generate it.
<div class="container" id="newsextra">
<h4>MORE NEWS</h4>
<div class="row">
<?php
if(!empty($error)){
echo $error;
}
if (!empty($blogsinfos)) {
foreach ($blogsinfos as $blogs): ?>
<div class="col-md-3">
<a href="/news-single/<?= h($blogs->id)?>">
<img src="<?= h($blogs->mainimg)?>" class="img-responsive">
<h5><?= h($blogs->title)?></h5>
<h6><?= h($blogs->created)?></h6>
</a>
</div>
<?php
endforeach;
}
?>
</div>
</div>
From the controller you should limit the number of posts generated. Thanks #Rik.esh for the answer.
$this->loadModel('Blogs');
$opts1['conditions'] = array('Blogs.status' => 1);
$opts1['limit'] = 4;
$opts1['order'] = array('Blogs.created' => 'desc');
$blogsinfos = $this->Blogs->find('all',$opts1);
$this->set('blogsinfos', $blogsinfos);
$this->set('_serialize', ['blogsinfos']);
foreach ($blogsinfos as $blogs) {
$proid = $blogs['id'];
}
try this
$opts1['order'] = 'Blogs.created desc';

Yii: Getting name and image using cdbciteria

I want to retrieve data from my database using CDbcriteria. I want to get the name and image of the last 4 Ngos from my Ngo table.
<?php
$Criteria = new CDbCriteria();
$Criteria->limit = 4;
$Criteria->order = "id DESC";
$Criteria->select = "id, ngo_name, image";
$Ngos =Ngo::model()->findAll($Criteria);
?>
<div class="row">
<h3>NGO's</h3>
<div class="col-md-3">
<div class="thumbnail">
<img src="<?php
foreach ( (array)$Ngos as $Ngo)
{
echo '$Ngo->image';
}
?>"
<div class="caption">
<h3><?php
foreach ( (array)$Ngos as $Ngo)
{
echo '$Ngo->ngo_name';
}
?></h3>
<button class="btn btn-primary center-block">View Profile</button>
</div>
</div>
</div>
just remove single quotes around the variable $Ngo->image and $Ngo->ngo_name or use double quotes instead.

How do I join tables for a search query in Codeiginter

Hi I have the following function that search uses to find information:
function get_search(){
$property_location = $this->input->post('property_location');
$property_bedroom = $this->input->post('property_bedroom');
$property_bathroom = $this->input->post('property_bathroom');
$property_status = $this->input->post('property_status');
$property_type = $this->input->post('property_type');
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$query = $this->db->get('property');
error_log($this->db->last_query());
return $query->result();
}
How would I join another table say property_images to the search so that data is included in the result?
Here is my view:
<div class="container">
<div class="page-header">
<h4>Your search returned the following result(s):</h4>
</div>
<?php foreach ($results as $item): ?>
<div class="row">
<div class="col-md-3">
<div class="thumbnail">
<img
src="<?php echo base_url() . 'data/images/property_images/' . $item->image_name; ?>"
class="img-responsive">
</div>
</div>
<div class="col-md-9">
<h4 style="margin-top: 0;">
<?php echo $item->property_name; ?>
</h4>
<p><?php echo $item->property_description;?></p>
<p>Location: <?php echo $item->property_location ?> | Price: R<?php echo $item->property_price ?></p>
</div>
</div>
<hr>
<?php endforeach; ?>
</div>
You can use join in your query:
function get_search(){
$property_location = $this->input->post('property_location');
$property_bedroom = $this->input->post('property_bedroom');
$property_bathroom = $this->input->post('property_bathroom');
$property_status = $this->input->post('property_status');
$property_type = $this->input->post('property_type');
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$this->db->join('property_images','property.property_image_id=property_images.property_image_id'); //add this line in your code
$query = $this->db->get('property');
error_log($this->db->last_query());
return $query->result();
}
Hope it will help you....
You can do it using join() method of active records class you are using.
join($table, $cond[, $type = ''[, $escape = NULL]])
Parameters:
$table (string) – Table name to join
$cond (string) – The JOIN ON condition
$type (string) – The JOIN type
$escape (bool) – Whether to escape values and identifiers
Returns:
CI_DB_query_builder instance (method chaining)
Return type:
CI_DB_query_builder
Adds a JOIN clause to a query.
Reference
In your example:
$this->db->like('property_location',$property_location);
$this->db->like('property_beds',$property_bedroom);
$this->db->like('property_bath',$property_bathroom);
$this->db->like('property_state',$property_status);
$this->db->like('property_type',$property_type);
$this->db->like('property_images.imageField', $imageFieldValue); // Observe change here.
$query = $this->db->get('property');
$this->db->join('property_images','property.your_id_field = property_images.your_id_field'); // Observe change here.

Categories