Display items from database in HTML/PHP - php

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>

Related

Use PHP foreach loop to display data in many html div tags

I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>

Using buttons to loop to the next element from the data fetched from database

I want to have a next button on my Productdetails.php page that would show the next image fetched from the database using while loop. Below is the productdetails.php page shows details of a particular product.
I am using simple php format, How can I go about it? I have come this far at this point.
<div class = "uniqueproduct">
<?php
if (isset($_GET['id']))
{
$id = $_GET['id'];
$query1 = mysqli_query($conn, "select * from products where PRODUCT_ID = $id");
while ($row1 = mysqli_fetch_array($query1)) {
?>
<div class = "singleproduct">
<?php echo '<img src = "data:image/jpeg;base64,'.base64_encode($row1['PRODUCT_IMAGE']).'" alt = "" width = "250px" height ="300px"/>'; ?>
</div>
<div class = "productName">
<?php echo $row1['PRODUCT_NAME']; ?>
</div>
<div class = "productDescription">
<?php echo $row1['PRODUCT_DESCRIPTION'];?>
</div>
<div class = "productPrice">
<?php echo $row1['PRODUCT_PRICE']; ?>
</div>
<?php
}
}
?>
<button class = "btnPicture"> ..............</button>
</div>

PHP MVC Send object variable with href

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

Display data from database PHP OOP

I'm pretty new to PHP OOP and i have to make a small web app for school.
One of the things i'm trying to do right is to select data from the database and display it in my front-end. I figured out how to display a single row but i wanted to display all rows in the table. How can i do that?
My function:
public function listJobs(){
$myDb = $this->_controlPanel->getMyDb();
$query = "Select * FROM jobs_offers";
$result = $myDb->performQuery($query);
$row = $result->fetch_array(MYSQLI_ASSOC);
$this->title=$row['title'];
$this->description=$row['description'];
$this->category=$row['category'];
$this->budget=$row['budget'];
}
My front-end:
<?php
header ('Content_type: text/html; charset=ISO-8859-1');
require_once ('utils/Autoloader.php');
session_start();
if (empty($myControlPanel)) {
try {
$myControlPanel = new classes_ControlPanel();
$myControlPanel->setMyDb(new classes_DbManager());
$myDbManager = $myControlPanel->getMyDb();
}
catch (Exception $e) {
echo $e->getMessage();
die();
}
}
$log = new classes_UserManager($myControlPanel);
$select = $log->listJobs();
$title = $log->title;
$description = $log->description;
$category = $log->category;
$budget = $log->budget;
?>
<body>
<div class="container-fluid">
<h2>
<h2><?php echo $title; ?> </h2>
<h2><?php echo $description; ?> </h2>
<h2><?php echo $category; ?> </h2>
<h2><?php echo $budget; ?> </h2>
</div>
First, the result will return an array of objects (PDO). You would want to return this array to your view. Or, in your case as a property of the object and not the single row like you did above.
Next, You want to use a foreach or while loop in the view. If you are using PDO (Php Data Object) you can do the following.
while($job = $result->fetch_assoc()) {
{
?>
<div class="container-fluid">
<h2>
<h2><?= $job->title; ?> </h2>
<h2><?= $job->description; ?> </h2>
<h2><?= $job->category; ?> </h2>
<h2><?= $job->budget; ?> </h2>
</div>
<?php
}
(Short Tags (<?=$var?>) are equivalent to <?php echo $var?>)
Reference: http://www.w3schools.com/php/php_mysql_select.asp

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