I have a problem with this GET.
I want to pass on three values (ygo, dane, 1) with a link to index, so that these values are entered in the respective WHERE.
But unfortunately there is no value on the page and I just don't know why.
IMG:
https://screenshots.firefox.com/g6qcToRn.../127.0.0.1
Controller:
public function index($slug) {
$url = 'edition/index';
$array = explode ('/',$url);
$data['array'] = $array[0];
$data['title'] = 'Edition Liste';
$data['edition_listes'] = $this->Edition_model->edition_listess($slug);
$this->load->view('templates/header', $data);
$this->load->view($url, $data);
$this->load->view('templates/footer');
}
public function edition($tcg,$id,$short) {
$url = 'edition/edition';
$array = explode ('/',$url);
$data['array'] = $array[0];
$data['get_edition'] = $this->Edition_model->get_edition($tcg,$id,$short);
$this->load->view('templates/header', $data);
$this->load->view($url, $data);
$this->load->view('templates/footer');
}
Model:
public function edition_listess($slug) {
$this->db->select('*');
$this->db->from('db_edition');
$this->db->where('db_edition.tb_edition_tcg', $slug);
$this->db->order_by('db_edition.tb_edition_kurzel', 'ASC');
$this->db->order_by('db_edition.tb_edition_date', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
public function get_edition($tcg,$id,$short) {
$this->db->select('*');
$this->db->from('db_edition');
$this->db->where('db_edition.tb_edition_tcg', $tcg);
$this->db->where('db_edition.tb_edition_id', $id);
$this->db->or_where('db_edition.tb_edition_kurzel', $short);
$this->db->order_by('db_edition.tb_edition_kurzel', 'ASC');
$this->db->order_by('db_edition.tb_edition_date', 'ASC');
$query = $this->db->get();
return $query->result_array();
}
index.php
<?php echo validation_errors(); ?>
<title><?php echo $title; ?></title>
<div class="row">
<div class="col-xs-6 col-lg-1">
</div>
<div class="col-xs-6 col-lg-10">
<?php foreach ($edition_listes as $edition_liste): ?>
<div class="list-group">
<a href="<?php echo base_url(); ?>edition/edition/<?php echo $edition_liste['tb_edition_tcg'];?>/<?php if($edition_liste['tb_edition_kurzel'] > '') { echo $edition_liste['tb_edition_kurzel']; } else { echo $edition_liste['tb_edition_id']; } ?>" class="list-group-item list-group-item-action list-group-item-secondary">
<div class="media">
<img class="media-object img-rounded img-responsive" src="<?php echo base_url(); ?>/assets/images/display.png" alt="placehold.it/350x250">
<div class="media-body">
<h4 class="list-group-item-heading"><?php echo $edition_liste['tb_edition_name'];?></h4>
<p class="list-group-item-text">
<b>SET ID: </b><?php if($edition_liste['tb_edition_kurzel']): echo $edition_liste['tb_edition_kurzel']; endif; ?></br>
<b>Kartenmenge: </b><?php echo $edition_liste['tb_edition_size'];?></br>
<b>Release: </b>00.00.0000
</p>
</div>
<div class="col-md-3 text-right">
<h5 class="colors">Pre-Release: <button class="btn btn-primary"><i class="fa fa-check-square"></i></button></h5>
<h5 class="colors">Pre-Release: <button class="btn btn-primary"><i class="fa fa-square"></i></button></h5>
</div>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
<div class="col-xs-6 col-lg-1">
</div>
</div>
edition.php (test page)
<?php echo validation_errors(); ?>
<?php echo $slug; ?>
It looks like you are not passing the third argument to edition($tcg,$id,$short). That likely means your model is using null for the value of $short in the call to $this->db->or_where('db_edition.tb_edition_kurzel', $short); so it is probably not returning any records.
Your code could be a lot easier to read. Here is code-snippet suggestion on how to create the link to the edition controller
...
<div class="list-group">
<?php
$tcg = $edition_liste['tb_edition_tcg'];
if($edition_liste['tb_edition_kurzel'] !== '') // use a more reliable test for an empty string than > ''
{
$id = $edition_liste['tb_edition_kurzel'];
}
else
{
$id = $edition_liste['tb_edition_id'];
}
$short = 1;
?>
<a href="<?= base_url("edition/edition/$tcg/$id/$short"); ?>" class="list-group-item list-group-item-action list-group-item-secondary">
...
In case you are not familiar with the syntax <?= is exactly the same as <?php echo - but with a lot less typing.
Now, a bonus suggestion. I'm not sure exactly why you are using this code
$url = 'edition/index';
There is a way to get the current controller and method without hard-coding it.
$url = $this->router->class . '/' . $this->router->method;
That way if you rename the controller or the method you don't have to change the hard-coded string. Yeah!
CodeIgniter does not document the router class, but the source code is in /system/core/Router.php and every controller will have an instance of the router class available as the property $this->router.
Related
I'm trying to make my own MVC Architecture and i'm stuck on getting id from url.
For example in the url, I have http://localhost/EdelweissMagazine /articles/edit/?id=66
but when I call print_r($_GET[url]) I only have
Array
(
[url] => articles/edit/
[id] =>
)
I can't understand why..
Here is my code. I hope you could help me to understand.
For explanations, I would like to make an update method : When clicking on the edit button, placed inside a photo (only if it is a user), the user goes to a form with datas already written to edit.
Thank you in advance
.htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\-_\/]*)$ index.php?url=$1&id=$2 [NC,L]
I've used in the router that function index.php
call_user_func_array([$controller, $action], $url);
controller/Articles.php
class Articles extends Controller{
public function index(){
$article = $this->loadModel('Article');
$articles = $article->getAll(); //getAll() : $sql="SELECT * FROM ".$this->table;
$this->render('index', ['articles' => $articles]);
}
public function edit(){
print_r($_GET);
}
}
Views/articles/index.php
<section class="gallery">
<?php
$articleLength = count($articles);
$count = 1; ?>
<?php while($count < $articleLength): ?>
<?php
if($count%4 == 1){
?>
<div class="column">
<?php
}
?>
<div class="wrap-img">
<img class="items-gallery"
src="<?php echo htmlspecialchars(SCRIPT_ROOT.'/'.$articles[$count]['picture'], ENT_QUOTES); ?>"
alt="<?php echo htmlspecialchars($articles[$count]['title'], ENT_QUOTES);?>"
/>
<?php
/*************** Code to edit an article ***************/
if(isLogged()) {
?>
<a class="action-icons" href="<?php echo SCRIPT_ROOT.'/articles/edit/?id='.$articles[$count]['id']; ?>"><i class="fas fa-pencil-alt"></i></a>
<a class="action-icons" href="<?php echo SCRIPT_ROOT; ?>"><i class="fa fa-trash"></i></a>
<?php
}
?>
/**********************************************/
</div>
<?php
if($count%4 == 0){
?>
</div>
<?php
}
$count = $count + 1;
endwhile;
?>
</section>
<div id="modal">
<span id="closeModal"><i class="fas fa-times"></i></span>
<img class="modal-content" id="img01" />
<div id="caption"></div>
<i class="fas fa-chevron-left"></i>
<i class="fas fa-chevron-right"></i>
</div>
i am new in code igniter i am trying to fetch the data of all jobs of same location in my view
i write some code but when i try to get the data its gives me single data i want to fetch all data of same location
here is my controler
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}
and model is
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->row();
}else {
return false;
}
}
and my view is
<?php foreach ($data as $key => $data): ?>
<div class="job-box">
<div class="company-logo">
</div>
<div class="row">
<div class="col-lg-12">
<div class="alert alert-success alert-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Recently Posted Jobs in <?php echo $data->location?>
Citi</div>
<div class="row">
<div class="job-box">
<div class="description">
<div class="float-left">
<h5 class="title"><?php echo $data->job_title?></h5>
<div class="candidate-listing-footer">
<ul>
<li><i class="flaticon-pin"> </i><?php echo $data->location?></li>
<li><i class="flaticon-money"> </i><?php echo $data->salary?></li>
<li><i class="flaticon-work"> </i><?php echo $data->timing?></li>
<!--<li><i class="flaticon-honor"> </i><?php echo $jobs->qualification?></li>
<li><i class="flaticon-notepad"> </i><?php echo $jobs->experience?></li>-->
</ul>
<h6>Deadline:<?php echo $data->deadline?></h6>
</div>
</div>
<div class="div-right">
Apply Now
<!--<i class="flaticon-heart favourite"></i>-->
</div>
</div>
</div>
</div>
</div><!--end row-->
</div>
</div>
<?php endforeach ?>
</div>
i want to show multi record not single by clicking the hyper link
Many Thanks in advance
Junaid Amin
Please update your Controller method, Model method and View as follows:
Controller::Method:
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , $data);
}
Model::Method:
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
View:
<?php foreach ($jobs as $row): ?>
//Your staff goes here (i.e. $row->name_of_properties)
<?php endforeach ?>
pass name instead of id in you a tag and make route for that inside config/routes file
$route['location/(:any)'] = 'controller_name/function_name/$1';
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}
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.
I'm trying to create a search form in codeigniter. This is my controller and model,
Shop.php [ Controller ]
public function s(){
$q = $this->input->get('q');
if( $q == null ){
$this->index();
}
$data['products'] = $this->product_model->search_products( $q );
$this->load->view('shop', $data);
}
Product_model.php
public function search_products( $keyword ){
$this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
brand.brand_name, category.category_name, type.type_name, stock.selling_price,
(SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
(SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
$this->db->from('product');
$this->db->join('stock','stock.product_id = product.id');
$this->db->join('brand', 'brand.id = product.brand_id');
$this->db->join('category', 'category.id = product.category_id');
$this->db->join('type', 'type.id = product.type_id');
$this->db->join('color', 'color.id = product.color_id');
$this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');
$this->db->where('stock.qty >', 0);
$this->db->group_by('product.id');
$query = $this->db->get();
return $query->result_array();
}
But return value in null. So I tried calling search_product( $keyword ) function in my view and it worked. but when I print $data['product'] variable and its empty.
What is the problem here? Thank you.
Edit
The search result shows here.
<div class="row product-list-item">
<?php
// This is just used for check the query
$p = $this->product_model->search_products( 'mesh' );
echo '<br>';
print_r($products);
if( $products != null ){
foreach ( $products as $product ){
?>
<!-- item.2 -->
<div class="product-item-element col-sm-6 col-md-6 col-lg-4">
<!--Product Item-->
<div class="product-item">
<div class="product-item-inner">
<div class="product-img-wrap">
<img src="<?php echo base_url($product['product_image1']);?>" alt="">
</div>
<div class="product-button">
<i class="fa fa-eye"></i>
</div>
</div>
<div class="product-detail">
<p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
<?php echo $product['product_name']; ?>
</a></p>
<div class="product-rating">
<div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
<span style="width: <?php echo $product['rating'];?>%"></span>
</div>
<a href="#" class="product-rating-count">
<span class="count"><?php echo $product['rate_count'];?></span> Reviews
</a>
</div>
<p class="product-description">
<?php echo $product['product_description']; ?>
</p>
<h5 class="item-price"><?php echo $product['selling_price']; ?></h5>
</div>
</div>
<!-- End Product Item-->
</div>
<?php
}
}else{
?>
<div class="text-center no-items">
<h4>No items</h4>
</div>
<?php } ?>
</div>
Normally when I get the data from my database I there are arranged in the order of their id.However, I have created a column called 'sales' which is also an integer
and I would like my data to be arranged in ascending order according to the sales.Here is my code:
My Model
public function get_bestseller($id = FALSE) {
if ($id === FALSE)
{
$query = $this->db->get('submit');
return $query->result_array();
}
$query = $this->db->get_where('submit', array('id' => $id));
$this->db->order_by('sales', 'DESC');
return $query->result_array();
}
My View
<div class="tab-pane fade" id="faq-cat-2">
<div class="panel-group" id="accordion-cat-2">
<?php foreach ($bestseller as $bestseller_item): ?>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<img class="lazy" src="<?php echo base_url() ?>Images/last.gif"
data-original="<?php echo base_url() ?>uploads/<?php echo $bestseller_item['imgname']; ?>"
style="width: 350px; height: 150px">
<div class="caption">
<?php if ($bestseller_item['Price'] == 0): ?>
<h4 class="pull-right"><p class="text-success">Free</p></h4>
<?php else: ?>
<h4 class="pull-right">$<?php echo $bestseller_item['Price']; ?></h4>
<?php endif; ?>
<h4>
<a href="<?php echo site_url('submit/view/' . $bestseller_item['id']); ?>"
target="_blank"><?php echo $bestseller_item['Title']; ?></a>
</h4>
<p><?php echo $bestseller_item['textarea']; ?></p>
</div>
<p class="text-right"><b
class="author">By: <?php echo $bestseller_item['Author']; ?></b>
</p>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
My Controller
public function index()
{
$data['bestseller'] = $this->Submit_Database->get_bestseller();
$this->load->view('templates/header');
$this->load->view('pages/home', $data);
$this->load->view('templates/footer');
}
$query = $this->db->order_by('sales', 'DESC')->get_where('submit', array('id' => $id));
You are adding the order by clause after the query has already ran.