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>
Related
so
i try make a pos(point of sale) program using ci3
im trying add a feature where admin can make new menu and submenu
but something went wrong
when i goto controller "sistem"
my route show "sitename/sistem/sistem/menu" this route supposed to "sitename/sistem/menu". another controller also effected when i goto "dashboard" controller the route show
"sistem/dashboard"
this is sistem controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Sistem extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('M_menu');
$this->load->model('M_user');
if (!$this->session->userdata('email')) {
redirect('auth');
}
}
public function index()
{
$this->menu();
}
public function menu()
{
$data['judulpage'] = 'Manajemen Menu';
$data['datamenu'] = $this->M_menu->getmenus()->result_array();
$data['user'] = $this->M_user->getuserdata()->row_array();
$this->form_validation->set_rules('menutambah', 'Menu', 'required');
// $this->form_validation->set_message([
// 'required' => 'Field menu add harus diisi'
// ]);
if ($this->form_validation->run() == false) {
$this->load->view('template/header', $data);
$this->load->view('template/sidebar', $data);
$this->load->view('sistem/menu', $data);
$this->load->view('template/footer');
} else {
$menu['menu'] = $this->input->post('menutambah');
$this->M_menu->insertmenu($menu);
$this->session->set_flashdata('flash','Sukses');
redirect('sistem');
}
}
this is the sidebar.php i think this is the problem but i cant find the solution
<?php
$role_id = $this->session->userdata('role_id');
$queryMenu = "SELECT `user_menu`.`id`, `menu`, `icon_menu`
FROM `user_menu`
JOIN `user_access_menu` ON `user_menu`.`id` = `user_access_menu`.`menu_id`
WHERE `user_access_menu`.`role_id` = $role_id";
$menus = $this->db->query($queryMenu)->result_array();
?>
<!-- BEGIN: Main Menu-->
<div class="main-menu menu-fixed menu-dark menu-accordion menu-shadow" data-scroll-to-active="true">
<div class="main-menu-content">
<ul class="navigation navigation-main" id="main-menu-navigation" data-menu="menu-navigation">
<li class=" navigation-header"><span>MENU</span><i class=" feather icon-minus" data-toggle="tooltip" data-placement="right" data-original-title="Apps"></i>
</li>
<?php foreach($menus as $menu) : ?>
<?php
$menuid = $menu['id'];
$querysubmenu = "SELECT * FROM `user_sub_menu` WHERE `menu_id` = $menuid AND `is_active` = 1";
$submenus = $this->db->query($querysubmenu)->result_array();
?>
<li class=" nav-item"><i class="<?php echo $menu['icon_menu']; ?>"></i><span class="menu-title"><?php echo strtoupper($menu['menu']); ?></span>
<ul class="menu-content">
<?php foreach($submenus as $submenu) : ?>
<li>
<a class="menu-item" href="<?php echo $submenu['url']; ?>"><i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>
<?php endforeach; ?>
</ul>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
i take the url from database.
u can see from inspect mode, the href redirecting to sistem/menu which is coorect url but when i click it, it goto sistem/sistem/menu
You do not need to call double <?php ?> tag.
just do this
<?php echo base_url($submenu['url']); ?>
or simplify the echo syntax with
<?= base_url($submenu['url']); ?>
fixed href="<?php echo base_url(); ?> i add / this before php tag but somehow it worked but still directing to wrong url localhost/sistem/menu it supposed to localhost/site/sistem/menu, so i add base_url to add my sitename and it work, thx
<li>
<a class="menu-item" href="<?php echo base_url(); ?><?php echo $submenu['url']; ?>">
<i class="<?php echo $submenu['icon']; ?>"></i><span><?php echo $submenu['title']; ?></span></a>
</li>
I want to create a detail page, but when I click button examp_title, I can't move to Tryout/soal.
Thank You
Controller (Tryout.php)
public function deskripsi($tryout_id)
{
$data['tryout'] = $this->M_tryout->deskripsi_to($tryout_id)->row();
$data['exam_tpa'] = $this->M_tryout->get_exam_tpa($tryout_id)->result();
$data['exam_tps'] = $this->M_tryout->get_exam_tps($tryout_id)->result();
$this->load->view('tryout/deskripsi', $data);
}
public function soal($exam_code)
{
$data['soal'] = $this->M_tryout->get_soal_tryout($exam_code)->result;
$this->load->view('tryout/soal', $data);
}
View (deskripsi.php)
<?php foreach ($exam_tps as $key => $value) { ?>
<h5 class="fw-bold" class="sub_to">
<a href="<?php base_url('tryout/soal/') ?>
<?php echo $exam->exam_code?>">
<?php echo $value->exam_title;?>
</a>
</h5>
<p>
<?php echo $value->exam_duration;?> Menit,
<?php echo $value->exam_count_question;?> soal <br> Pembahasan
</p>
<?php } ?>
It looks like your <a> tag is not outputting the link you want due to a missing echo.
I have simplified the href below...
<h5 class="fw-bold" class="sub_to">
<a href="<?php echo base_url('tryout/soal/') . $exam->exam_code ?>">
<?php echo $value->exam_title; ?>
</a>
</h5>
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.
I'm inner joining two databases together here, when using the separate views individually they work fine, so do the databases, I think the cause of this is the inner join, but putting the query directly into phpmyadmin and replacing the $categoryID with a number works.
Some rows load, such as the images, but joined variables are undefined, What is causing the variables to be undefined?
Model:
function DisplayBySpecificCategory($categoryID)
{
$query = $this->db->query("SELECT * FROM CIUploads
INNER JOIN CIUsers ON CIUploads.UserID = CIUsers.UserID
WHERE CIUploads.UploadCategoryID = $categoryID
ORDER BY CIUploads.UploadTime DESC");
return $query->result();
}
Controller:
function SpecificCategory()
{
$categoryID=$this->uri->segment(3);
$this->load->model('Previews_model');
$this->load->model('Category_model');
$this->load->view('../includes/header.php');
$data['category'] = $this->Category_model->GenerateCategoryHeader($categoryID);
$data['allwork'] = $this->Previews_model->DisplayBySpecificCategory($categoryID);
$this->load->view('headers/Specific_category_header', $data);
$this->load->view('previews/Previews_view', $data);
$this->load->view('../includes/footer.php');
}
Header view:
<?php foreach ($category as $value2){ ?>
<header style="background-color:#<?php echo $value2->CategoryColor ?>">
<div class="container">
<div class="row">
<div class="col-lg-12">
<div class="page-intro">
<h1 class="inverted text-center"><?php echo $value2->CategoryTitle ?></h1>
<p class="inverted text-center"><?php echo $value2->CategoryDescription ?></p>
</div>
</div>
</div>
</div>
</header>
<?php } ?>
Previews view:
<?php
if($value->UploadCategoryID == 2){
echo '<video muted class="video-responsive fit-centered">';
echo '<source src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" type="video/mp4">Your browser does not support the video tag.';
echo '</video>';
}
else if($value->UploadCategoryID == 3){
echo '<iframe class="fit-centered" src="http://'.$value->UploadedAt.'" scrolling="no"></iframe>';
}
else{
echo '<img class="img-responsive fit-centered" src="'.base_url("/public/uploads/works")."/".$value->UploadedAt.'" alt="'.$value->UploadTitle.'">';
} ?>
<div class="overlay">
<p>
<i class="fa fa-eye fa-3x ease-color"></i>
</p>
</div>
</div>
<div class="hovereffect-information no-bounding">
<span class="category-tab pull-right text-center inverted" style="background-color:#<?php echo $value->CategoryColor ?>"><i class="fa <?php echo $value->CategoryIcon ?>"></i></span>
<p class="pull-left no-bounding text-uppercase preview-info"><?php echo $value->UploadTitle ?></p>
<img class="img-responsive small-profile-img pull-right hover-dim fit-centered" src="<?php echo base_url("/public/uploads/profile-images/")."/".$value->ProfileImg ?>" alt="<?php echo $value->Username ?>"/>
<p class="pull-left no-bounding text-uppercase preview-info">
<span class="preview-span"> <i class="fa fa-eye"></i> <?php echo $value->UploadViews ?> </span> |
<span class="preview-span"> <i class="fa fa-heart"></i> <?php echo $value->UploadLikes ?> </span> |
<span class="preview-span"> <i class="fa fa-comments"></i> <?php echo $value->UploadComments ?> </span>
</p>
</div>
</div>
<?php } ?>
Error:
Severity: Notice
Message: Undefined property: stdClass::$CategoryIcon
Filename: previews/Previews_view.php
Line Number: 31
Backtrace:
File:
/web/stud/u1263522/cmw/application/views/previews/Previews_view.php
Line: 31 Function: _error_handler
File:
/web/stud/u1263522/cmw/application/controllers/Views_controller.php
Line: 64 Function: view
File: /web/stud/u1263522/cmw/index.php Line: 292 Function:
require_once
In the question you have mentioned : Undefined property: stdClass::$CategoryID
whereas the error shows : Undefined property: stdClass::$CategoryIcon
If $CategoryIcon is undefined it means that you do not have a column called CategoryIcon in any of your tables.
Can you mention the details of the tables?
It is always a good practice to mention the column names in the query instead of *.
add your loading model before called in Controller , Check name is correct , Make sure that some name in (model = Class)
$this->load->model('model_name'); //
Or in Confige.php
I am creating a blog in PHP (trying to keep it close to OOP) and am struggling with getting pagination to work on the page that displays all posts.
The code for the blog.php in question is
<?php
require_once("includes/init.php");
$pagetitle = "Blog";
//include header.
require_once("includes/template/header.php");
// initialise script
$blog = new Blog($db);
$parsedown = new Parsedown();
// load blog posts
$posts = $blog->get_posts();
?>
<div class="container">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">Blog Home</h1>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<?php foreach ($posts as $post) { ?>
<h1><?php echo $post['title']; ?>
</h1>
<p class="lead">by <?php echo $post['author']; ?>
</p>
<hr>
<p><i class="fa fa-clock-o"></i> Posted on <?php echo date("d/m/Y", strtotime($post['date'])); ?> At <?php echo date("H:i", strtotime($post['date'])); ?> in <?php
$categories = $blog->get_categories($post['id']);
$links = array();
foreach ($categories as $category) {
$links[] = "<a href='blog-categories.php?id=".$category['category_slug']. "'>".$category['category_name']."</a>";
}
echo implode(", ", $links); ?></p>
<hr>
<p><?php $content = $parsedown->parse($post['content']); echo substr($content,0,445) ; ?></p>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>">Read More <i class="fa fa-angle-right"></i></a>
<a class="btn btn-primary" href="<?php echo $post['slug']; ?>#disqus_thread"><i class="fa fa-angle-right"></i></a>
<hr>
<?php }
?>
</div>
What can I add either to the Blog class or put in a new class to allow blog.php to only show the first 5 results and then give a 'next' link to view the next set of 5? I'm not worried about displaying the total number of pages.
As the class is very large, it can be viewed at http://pastebin.com/qN5ii2Ta.
You are currently using a method get_posts(). You could redefine this method to accept a starting point to begin gathering posts from. With this starting point defined you can LIMIT how many posts are returned in your SQL query.