PHP MVC Send object variable with href - php

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

Related

Echo user info on another users profile page

I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach ($userdetail_list as $row) {
?>
<tr>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach ($userdetail_list as $row) { ?>
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load this view file I do see the echoeod email but not the username? And there is also no link to the users profile page..
Here is my controller file (User.php):
<?php
class User extends CI_Controller {
public function index() {
$this->load->view('profile', $data);
}
public function __construct() {
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id) {
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user', $data);
}
public function profile() {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
?>
And here is my model (User_model.php) :
<?php
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
public function getUserInfo($user_id) {
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if ($this->db->affected_rows() > 0) {
$row = $q->row();
return $row;
} else {
error_log('no user found getUserInfo(' . $user_id . ')');
return false;
}
}
?>
Database information:
I have 2 tables:
users:
user_id
voornaam
achternaam
email
password
(products:
product_id
product_name
product_description)
I hope someone can help me,
Thanks!
You are concatenating user name in anchor tag herf separate it and write between <a> and </a>
change your code as below:
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
You can try this solution for your problem.
Please change your view file.
details.php
<tr>
<td><?= $row['username']; ?></td>
<td><?php echo $row['email']; ?></td>
</tr>
and please changes your controller function:
User.php
<?php
public function profiel_user($user_id) {
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$data['user_id'] = $user_id; // you can used user id for getting data
$this->load->view('profile');
}
?>
Thanks

Link to a user profile page in codeigniter

I'm trying to make a function in the CodeIgniter framework so users can click on product detail page, and see which user uploaded that certain product and they must be able to click on the product owner to go to his profile page.
Now, I am already able to echo owner information of the product owner on the details page. But the link to the owner profile page is not working some reason and the name isn't echoed anymore since I tried to make it a link.
This is my product details page (details.php) foreach loop function where I'm trying to echo the username of the owner of a product and link it to their profile page:
<?php include_once ('templates/header.php'); ?>
<div class="container">
<h3>Email van de eigenaar:</h4>
<?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
<div class="container">
<div class="row">
<div class="col-md-4" style="margin-top:24px;">
<img src="<?php echo base_url(); ?>upload/<?php echo $product['product_foto']; ?>" class="img-responsive">
</div>
<div class="col-md-5">
<h1> <div class="product_naam"> <?php echo $product['product_naam']; ?> </div> </h1>
<h3>Over dit cadeau</h3>
<div class="product_beschrijving"><?php echo $product['product_beschrijving']; ?> </div>
</div>
<div class="col-md-3">
<button type="button" class="btn btn-default">Cadeau aanvragen</button>
<div class="aangeboden_door"> Aangeboden door: <?php
foreach($userdetail_list as $row)
{
?>
<tr>
<td><?=$row['username']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="container">
<footer>
<p>© kadokado 2017, Inc.</p>
</footer>
<hr>
</div>
<div class="clearfix"></div>
<?php include_once ('templates/footer.php'); ?>
When I load the view page I do not see the username and no link to the users profile.
Here is my controller (User.php):
<?php
class User extends CI_Controller
{
public function index()
{
$this->load->view('profile', $data);
}
public function __construct()
{
parent::__construct();
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
}
public function userdetails($user_id)
{
//load the User_model
$this->load->model('User_model');
//call function getdata in de Product_model
$data['userdata_list'] = $this->User_model->getdata();
//get product details
$data['user'] = $this->User_model->get_user_info($user_id);
//laad view
$data['main_content'] = 'profiel_user';
$this->load->view('profiel_user',$data);
}
public function profile()
{
$this->load->model('User_model');
if ($_SESSION['user_logged'] == FALSE) {
$this->session->set_flashdata("error", "Please login first to view this page!! ");
redirect("auth/login");
}
$this->load->view('profile');
}
}
full model file (User_model.php)
<?php
class User_model extends CI_Model {
public function getUserInfoByEmail($email)
{
$q = $this->db->get_where('users', array('email' => $email), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$email.')');
return false;
}
}
public function getUserInfo($user_id)
{
$q = $this->db->get_where('users', array('user_id' => $user_id), 1);
if($this->db->affected_rows() > 0){
$row = $q->row();
return $row;
}else{
error_log('no user found getUserInfo('.$user_id.')');
return false;
}
}
public function getdata()
{
$this->db->where('user_id', $id);
$this->db->from('users');
$query = $this->db->get();
if($query->num_rows()>0)
{
return $query->result_array();
}
}
}
?>
Problably because of this line:
<?=$row['username']; ?>
Maybe your server does not support short hand tags?
try this:
<?php echo $row['username']; ?>
You also missed a semicolon here at the end:
echo base_url() . 'User/profiel_user?id='.$row['user_id']
and also here:
<a href="<?php echo base_url() ?>/Cadeauaanvragen">
You also use table row and table data tags without a surrounding table tag. Or remove all tr and td tags.
See: How do browsers analyze <tr> <td> without <table>?

Abstract Class to Display Static Varibles

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";
}

Pick from a list and save it to database

I am creating a website with Codeigniter 3 HMVC style for a client and he wants have a function on this page where the logged user has the choice of picking their own news. For example if a user chooses Juventus then he will recieve all the news added where Juventus is named. Basically like a add to basket or cart thing and I have never done something like this so here is what I have created:
Controller
class Usernews extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->model('model_usernews');
$this->load->module('template');
}
function search(){
$data['pageTitle'] = "User news";
$search_name = $this->input->post('band');
$data['bands'] = $this->model_usernews->get_clubs($search_name);
$data['module'] = "usernews";
$data['view_file'] = "usernews";
$this->template->user_news($data);
}
}
Model
class Model_usernews extends CI_Model {
function get_table(){
$table = "bands";
return $table;
}
function get_clubs($keyword) {
$table = $this->get_table();
$this->db->like('band_name', $keyword, 'both');
$this->db->order_by('band_name');
$query=$this->db->get($table);
return $query;
}
and part of the View
<div class="row">
<div class="col-lg-8 ">
<!-- USER BAND LIST -->
<!-- LIST OF ARTICLES -->
</div>
<div class="col-lg-4">
<div class="row">
<?php $attributes = array('id' => 'list_of_bands');
echo form_open('usernews/search',$attributes); ?>
<input type="text" class="form-control" name="band"
placeholder="Search">
<button type="submit" class="btn btn-default btn-lg" >OK</button>
<?php echo form_close(); ?>
<?php $num_rows = $listing->num_rows();
if($num_rows > 0){ ?>
<ul id="the_bands" class="list-unstyled">
<li> <?php foreach ($listing->result() as $row){?>
<li><?php echo $row->band_name; ?>" ><button id="add_band"
class="btn btn-default btn-sm" >Add to list</button></li>
<?php }
} else{
echo "<p>No bands with that name</p>";
} ?>
</div>
</div>
I can search for the bands in the database but now I am clueless.
In your view use $bands instead of $listing.

Dynamically populating a div with data from a MySQL table

Lets say I have a persons table :
forename surname age gender
--------------------------------------------
adam example 90 male
john example 90 male
If I wanted to display this information in separate divs, how could this be done? Say for example the following HTML.
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<!-- adams data here -->
</div>
<div class = "jumbotron">
<!-- johns data here -->
</div>
</div>
</div>
I'm aware on how to query the DB to get the information into PHP variables, I'm just not sure how to dynamically display the data in separate divs.
Below is how I am getting the data
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
echo $row->forename, '<br><br>';
echo $row->surname, '<br><br>';
}
$result->free();
}
}
?>
Just wrap the div inside a loop so you'll print a div for each result row
<?php foreach($result as $r): ?>
<div class = "jumbotron">
<?php echo $r['name'] // Print fields you need ?>
</div>
<?php endforeach; ?>
EDIT: Now I can see your query. Try this:
<?php
if($result = $db->query("SELECT forename,surname FROM users ")){
if($count = $result->num_rows){
while($row = $result->fetch_object()){
?>
<div class = "jumbotron">
<?php echo $row->forename; ?><br><br>
<?php echo $row->surname; ?><br><br>
</div>
<?php
}
$result->free();
}
}
?>
Well Joe, This is a fairly simple problem that has probably been answered before.
I'll use the mysqli class of php
first create a mysqli connection object in a file for best practice then include it in your main script.
<?php
$connection = new mysqli($host_address, $username, $password, $database);
?>
include this in your main script
<?php require "path\to\connection\script"; ?>
<?php
$sql_adam = "SELECT * FROM persons WHERE forename = 'adam'";
$sql_john = "SELECT * FROM persons WHERE forename = 'john'";
$qry1 = $connection->query($sql_adam);
$qry2 = $connection->query($sql_john);
?>
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<?php if ($qry1->num_rows >= 1){
while($adam = $qry1->fetch_assoc()){
foreach ($adam as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
<div class = "jumbotron">
<?php if ($qry2->num_rows >= 1){
while($john = $qry2->fetch_assoc()){
foreach ($john as $column => $data) {
echo "<p>$column : $data </p>";
}
}
}
</div>
</div>
</div>
Your Result should be something like
<div class = "container">
<div class="wrapper">
<div class = "jumbotron">
<p>forename : adam</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
<div class = "jumbotron">
<p>forename : john</p>
<p>surname : example</p>
<p>age : 90</p>
<p>gender : male</p>
</div>
</div>
</div>

Categories