Basically what I have to do is create an abstract class with all of my variables/shared code in it. I then have to extend the class to make the variables unique to each class that extends the abstract. I'm relatively familiar with PHP but have never dealt with abstract classes and I'm completely lost.
HTML/PHP (index.php):
<div class="card-wrapper">
<div class="card">
<div class="card-inner">
<?php
require 'Card.php';
$name = new Card();
?>
<div class="fleft">
<div class="name"><?php echo $name->name;?></div>
<div class="business"><?php echo $name->business;?></div>
</div>
<div class="fright">
<div class="email"><?php echo $name->email;?></div>
<div class="website"><?php echo $name->website;?></div>
<div class="phone"><?php echo $name->phone;?></div>
<div class="address"><?php echo $name->address;?></div>
<div class="city"><?php echo $name->city;?></div>
<div class="state"><?php echo $name->state;?></div>
<div class="zip"><?php echo $name->zip;?></div>
</div>
</div>
</div>
</div>
PHP (card.php):
<?php
abstract class Card{
public $name = "John Q Public";
public $business = "Q Public Incorporations";
public $email = "john#qpublic.com";
public $website = "jqpinc.com";
public $phone = "555-555-5555";
public $address = "123 Main Street";
public $city = "Bowling Green,";
public $state = "Kentucky";
public $zip = "42101";
}
Related
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 making a website in CodeIgniter and I'm trying to make a menu bar with all the categories, and when you click on a certain category it must show all the products that are in that category but its not really working.
The categories are showing on the category side menu bar, but when I click on a certain category link its going to the right view page but instead of the product information I'm getting this error:
A PHP Error was encountered
Severity: Error
Message: Cannot use object of type stdClass as array
Filename: views/category.php
Line Number: 41
Backtrace:
Line 41 in category.php is this line:
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
So I'm guessing it can't echo product detail information?
Here is my Allecadeaus controller to echo products:
<?php
class AlleCadeausController extends CI_Controller {
public function index()
{
$this->load->model('Product_model');
$this->load->model('allecadeaus_model');
$data['products'] = $this->allecadeaus_model->get_products();
$this->load->view('allecadeaus', $data);
}
}
My Allecadeaus_model model file which I use to echo products:
<?php
class Allecadeaus_model extends CI_Model {
public function __construct()
{
parent::__construct();
}
public function get_products()
{
$query = $this->db->query('SELECT * FROM products');
$result = $query->result_array();
return $result;
}
}
category.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My whole allecadeaus.php view file:
<?php include_once ('templates/header.php'); ?>
<!-- Alle cadeaus gele title bovenaan pagina -->
<div class="container-fluid">
<div class="row">
<div class="col-lg-12 bg-warning" style="font-size:25px">
<center>Alle cadeaus</center>
</div>
</div>
</div>
<hr />
<br>
<!-- Cadeau categorie side menu -->
<div class="container-fluid">
<div class="row">
<div class="col-md-4">
<div id="categorymenu">
<center> <h3>Categorieën</h3> </center>
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category['name']; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<!-- Laat cadeau zien op alle cadeaus pagina -->
<div class="col-md-8">
<?php foreach($products as $product) : ?>
<div class="col-md-2">
<div id="product">
<a href="<?php echo base_url() ?>/Product/details/<?php echo $product['product_id']; ?>">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto_thumb']; ?>">
</a>
<div class="product_naam"><?php echo $product['product_naam']; ?></div>
<div class="ophaal_plaats">
<?php echo $product['ophaal_plaats']; ?>
</div>
<div class="aangeboden_door">
<p>Aangeboden door: Peter</p>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
My Product.php controller file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product extends CI_Controller {
var $data = array();
public function index()
{
//Laad kado uploaden view
$this->load->view('product_form', $this->data);
}
public function details($product_id)
{
//load the Product_model
$this->load->model('Product_model');
//call function getdata in de Product_model
$data['userdetail_list'] = $this->Product_model->getdata();
//get product details
$data['product'] = $this->Product_model->get_product_details($product_id);
//laad view
$data['main_content'] = 'details';
$this->load->view('details',$data);
}
//categorie product function
public function category($id)
{
$data['title'] = 'Category';
$data['page'] = 'Product/category';
$data['category'] = $this->Category_model->findByCategory($id);
$data['products'] = $this->Product_model->findByCategory($id);
$this->Category_model->findByCategory($id);
$this->load->view('category', $data);
}
public function __construct()
{
parent::__construct();
$this->load->model('Product_model');
$this->load->helper(array('form', 'url'));
}
My Product_model file:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Product_model extends CI_model {
public function saveProduct($data) {
$this->db->insert('products', $data);
$product_id = $this->db->insert_id();
return $product_id;
}
public function getdata()
{
$this->db->select('users.user_id,users.email,users.voornaam,products.product_id,products.category_id,products.product_naam');
$this->db->from('users');
$this->db->join('products','products.user_id = users.user_id');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function get_product_details($product_id) {
$arrReturn = array();
$this->db->select('*');
$this->db->from('products');
$this->db->where('product_id', $product_id);
$query = $this->db->get();
$result = $query->result_array();
if (!empty($result)) {
$arrReturn = $result[0];
}
return $arrReturn;
}
/*
Get categories
*/
public function get_categories(){
$this->db->select('*');
$this->db->from('categories');
$query = $this->db->get();
$result = $query->result();
return $result;
}
public function findAll(){
return $this->db->get('products')->result();
}
public function findByCategory($id){
$this->db->where('id', $id);
return $this->db->get('categories')->result();
}
}
?>
My Category_model file:
<?php
class Category_model extends CI_Model {
public function findAll(){
$this->db->get('categories')->result();
}
public function findByCategory($id)
{
$this->db->where('id', $id);
$this->db->get('categories')->result();
return $this->db->get('categories')->row();
}
}
?>
My db_helper.php
<?php if (!function_exists('get_categories_h')) {
function get_categories_h(){
$CI = get_instance();
$categories = $CI->Product_model->get_categories();
return $categories;
} } ?>
Some database information:
Table 1: 5 columns: products
-product_id
-product_naam
-product_beschrijving
-user_id
-category_id
table 2: categories: 2 columns:
1.id
2.name
You can try this solution for your problem:
Whole allecadeaus.php view file:
<ul class="list-group">
<?php foreach (get_categories_h() as $category) : ?>
<li class="list-group-item">
<?php echo $category->name; ?>
</li>
<?php endforeach; ?>
</ul>
is your database already have the filed category_id, is the name correct? can you show screen from your categories and products table?
Building a school management system using only PHP and PDO - college project.
Now I'm using MVC, and I'm having trouble sending information from one view to another.
This is the View I want information from
<?php
include_once 'model/student.php';
include_once 'model/courses.php';
$selected = new Student();
$students = Student::getAllStudents();
$selected2 = new Course();
$courses = Course::getAllCourses();
?>
<div>
<div class="col-lg-6">
<span style="font-size: 24px;"> <u>Students</u> - <button style="color:black;width: 30px;height: 35px;">+</button></span>
<br>
<hr>
<?php /* this is where I have the problem, the href attribute - I'm not sure I am sending the information correctly */
foreach($students as $stu){
?> <div> Student Name: <a href='? controller=student&action=showstudent&student_id=<?php echo $stu->id ?>'> <?php echo $stu->name ?> </a></div><br>
<div> Student Phone: <?php echo $stu->phone ?> </div><br> <hr> <?php
}
?>
</div>
<div class="col-lg-6">
<span style="font-size: 24px;"> <u>Courses</u> - <button style="color:black;width: 30px;height: 35px;">+</button></span>
<br>
<hr>
<?php
foreach($courses as $cur){
echo "<span><a href='#' ><b><u><i>" .$cur->name . "</i></u></b></a></span></br><hr>";
}
?>
</div>
</div>
These are the Controller and Model
<?php
class StudentController {
public $student_model;
public function __construct() {
$this->student_model= new Student();
}
public function showstudent($id) {
$find = $this->student_model->findStudentById($id);
if($find==TRUE){
require_once 'views/pages/container/student_into_container.php';
}
else{
echo "error";
}
}
}
Model
<?php
require_once 'connection.php';
class Student{
public $name;
public $phone;
public $email;
public $img;
public $id;
public function __construct() {
}
public static function getAllStudents(){
$students = [];
$db = Db::getInstance();
$req = $db->query('SELECT id, name, phone, email, image FROM students');
$students_info = $req->fetchAll();
foreach ($students_info as $student_info){
$stu = new Student;
$stu->id = $student_info['id'];
$stu->name = $student_info['name'];
$stu->phone = $student_info['phone'];
$stu->email = $student_info['email'];
$stu->img = $student_info['image'];
array_push($students, $stu);
}
return $students;
}
public function findStudentById($id){
$db = Db::getInstance();
$req = $db->prepare('SELECT * FROM student WHERE id = :id');
$req->execute(array('id' => $id));
$student = $req->fetch();
return $student;
}
}
And this is the view I want to get the $id from a student I found
and show all his value fields - this is the student_into_container.php from the StudentController page
<?php
require_once 'connection.php'
?>
<div style="" class="">
<div class="col-lg-4 ">
<?php
require_once 'views/pages/container/student_courses_aside.php';
?>
</div>
<div style="color: black;" class="col-lg-8 blackboard ">
<div class="blackboard-container" style="margin-top:10px;font-size:36px;">
<?php foreach($students as $stu){
echo "<span> Student Name: <a href='#'>". $stu->name . "</a></span></br>";
echo "<span> Student Phone: " . $stu->phone . "</span></br> <hr>";
}
?>
</div>
</div>
</div>
Totally lost here, any help would help.
In short?
Show in once view an information sent from another view with an href attribute
I'm trying to display some items on webpage from a database. I have a function that loops through the data stored in the database. Currently it displays the last item that appears within the database rather than the first or anything else. I have tried ordering the items by the id but it gives the same result. I have also tried to display individual items of the array but it just displays the first character of the last item within the database.
display items function:
<?php
function get_product_details()
{
global $db;
$ret = array();
$sql = "SELECT * FROM Products";
$res = mysqli_query($db, $sql);
while($ar = mysqli_fetch_assoc($res))
{
$ret[] = $ar;
}
return $ret;
}
?>
Area for displaying items:
<article class = "main_area2">
<h1 class = "food_h1">Menu</h1><br>
<section class = "menu_section">
<?php
$products = get_product_details();
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
}
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
This is the current display and as you can see the first and last are exactly the same (the second to eighth are currently hard-coded).
P.S. Don't worry about closing the section & article with the close tags because it's already done.
You're looping over the array, but you're not doing anything with the values:
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
}
Then after the loop, when the values are the last of the array, you finally use them once:
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
Instead, use the values in the loop so you can use each of them:
foreach($products as $ap)
{
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php
} // end the loop after using the values
It seems, that your HTML- generation should be inside the loop, not outside:
<?php
$products = get_product_details();
foreach($products as $ap) {
$name = $ap['pro_name'];
$description = $ap['descr'];
$price = $ap['rrp'];
?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $name; ?></h2>
<br><img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br><h1 class = "food_h2"><?php echo $description; ?></h1>
<br><h1 class = "food_h2">£ <?php echo $price; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php
} ?>
<article class = "main_area2">
<h1 class = "food_h1">Menu</h1>
<br>
<section class = "menu_section">
<?php foreach(get_product_details() as $ap): ?>
<div class = "section_4">
<h1 class = "food_h2"><?php echo $ap['pro_name']; ?></h1>
<br>
<img src = "img/products/hassellback_pot.jpg" class = "img_dis">
<br>
<h1 class = "food_h2"><?php echo $ap['descr'] ?></h1>
<br>
<h1 class = "food_h2">£ <?php $ap['rrp']; ?></h1>
<button class = "basket_btn" type = "button"> Buy </button>
</div>
<?php endforeach; ?>
</section>
</article>
I am quite new in using Yii Fraework and I am trying to implement a custom form with the skeletron from the contact form demo withon the blog demo from Yii Framework. I did almost exactly the same view,, controller and model as the respective form, only that I get the following 500 error:
Error 500
CForm and its behaviors do not have a method or closure named "beginWidget".
Here are the : Controller:
<?php
class CustomController extends Controller {
public function actionSubmit()
{
$model = new CustomForm;
$form = new CForm('application.views.custom._form', $model);
$this->pageTitle = "ffffffffffff";//['title'] = "Authentication";
if($form->submitted('submit') && $form->validate())
$this->redirect(array('blog/index'));
else
$this->render('_form', array('form'=>$form));
}
public function getGenders()
{
return array(
0 => 'Male',
1 => 'Female');
}
}
?>
The Model:
<?php
class CustomForm extends CFormModel {
public $firstName;
public $LastName;
public $phone;
public $address;
public $gender;
public $email;
public function rules()
{
return array(
array('firstName, lastName, gender', 'required'),
array('email', 'email')
);
}
}
?>
The view:
<?php
$this->pageTitle=Yii::app()->name . ' - Custom Form';
$this->breadcrumbs=array(
'Custom Form',
);
?>
<h1>Custom Form</h1>
<?php if(Yii::app()->user->hasFlash('custom')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('custom'); ?>
</div>
<?php else: ?>
<p>
If you have business inquiries or other questions, please fill out the following form to contact us. Thank you.
</p>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'custom-form',
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'firstName'); ?>
<?php echo $form->textField($model,'firstName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastName'); ?>
<?php echo $form->textField($model,'lastName'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'phone'); ?>
<?php echo $form->textField($model,'phone',array('size'=>60,'maxlength'=>128)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'gender'); ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'Male')) . 'Male'; ?>
<?php echo $form->radioButton($model,'gender',array('value'=>'Female')) . 'Female'; ?>
<?php echo $form->error($model,'gender'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'address'); ?>
<?php echo $form->textArea($model,'address',array('rows'=>6, 'cols'=>50)); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php endif; ?>
Any ideas why am I getting this error? What am I doing wrong?
Thanks!
CForm represents a form object that contains form input specifications.
You are passing a view file as parameter to the CForm which wont work.
I guess there is no need for this line:
$form = new CForm('application.views.custom._form', $model);
Please check if it works :)