Retrieve Category & Subcategory list in PHP - php

I'm very new to HTML, PHP & MySql, so apologies for the noob question but, I need to retrieve categories from a database, that are in 2 tables, but am having difficulty calling the subcategories after clicking the initial link.
//Category code as...
<?php
require 'dbcon.php';
$sql = "SELECT * FROM `category`";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<table>
< tr >
<th>Category < /th>
< /tr>";
while ($row = $result ->fetch_assoc()) {
$id = $row['category_ID'];
echo "<td>" .$row['description']. "</td>";
echo "</tr>";
}
echo "</table>";
?>
//Subcategory code as...
<?php
require 'database_conn.php';
$sql = "SELECT category.ID, subcategory.title, subcategory.ID FROM category, subcategory WHERE subcategory.ID=category.ID";
$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));
echo "<table>
< tr >
<th>Title < /th>
< /tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
Ideally, I would like it so that selecting one category will bring up the whole list of subcategories, either in the same page or a separate page. I've struggled over this for a few days now and I'm really struggling to understand what it is I am doing wrong.

simply take the id from the URL and put into your query
$category_id = $_GET['id'];
SELECT * FROM subcategory WHERE category_id = $id;

Related

How would I go about displaying a table from SQL with PHP A-Z where after 10 rows the script starts a new table next to it

I am trying to display SQL table data with PHP, right now I have made an HTML table that infinitely loops up until the end of the page. Now I want to try and display 10~ names vertically before starting a new HTML table next to it that continues looping until the entire SQL table is displayed. The script I have right now is as follows.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
while($row = mysqli_fetch_array($result)){
echo "<table>
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
</table>" ;
}
if I understood correctly you want to have a table for each 10 rows , if thats the case you can do it as follows :
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$i = 1;
while ($row = mysqli_fetch_array($result)) {
if ($i == 1) {
print "<table>";
} elseif ($i == 10) {
print " </table><table>";
$i = 1;
}
echo "
<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>
";
$i++;
}
You can simply fetch all the rows into a table and then split it into chunks of 10.
$result = mysqli_query($conn, "SELECT name, id FROM table ORDER BY name");
$data = $result->fetch_all(MYSQLI_ASSOC);
foreach (array_chunk($data, 10) as $table) {
echo "<table>";
foreach ($table as $row) {
echo "<tr><td><input type=\"submit\" name=\"select\" value=\"" . $row['name'] . "\"></input></td></tr>";
}
echo "</table>";
}

mysql get the value of a column in a database as a percentage

I have a table in a database and am currently pulling data using the SELECT statement Where the information from the column Opinion equals either Negative or Positive.
what i want to also do is output the positive data as an overall percentage but Unsure if that would be possible i had a look at multiple overflow questions but couldn't see anything. Any help would be appreciated.
$sql = "select Opinion from survey where Opinion = 'Positive'";
$result = mysqli_query($con, $sql);
if (!$result) {
die(mysqli_error($con));
}
echo "<div style='overflow: auto;'>";
echo "<table width=40% border=1 align=center >
<tr>
<th>Opinion</th>
<th>Date</th>
</tr>";
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr align=center>';
echo "<td>" . $row['Opinion'] . "</td>";
}
} else {
echo "0 results";
}
?>
The query will calculate how many percentage of 'Positive' opinions compared to total rows of the 'survey' table:
select (SUM(IF(Opinion = 'Positive',1,0))/count(*))*100 as percentage_positive
from survey
The query below can determine the percentage of each different opinions at once:
select
Opinion,
count(*) as total,
(count(*) / (select count(*) from survey))*100 as percentage
from survey
group by opinion
Something like this as SQL query?
SELECT COUNT(Opinion) / (SELECT COUNT(Opinion) FROM survey) * 100
FROM survey
WHERE Opinion = 'Negative'
After help from Kevin HR i have fixed my issue with the code below.
$sql = "select Opinion,count(*) as total,(count(*) / (select count(*) from survey))*100 as percentage from survey group by opinion";
$result = mysqli_query($con, $sql);
echo "<div style='overflow: auto;'>";
echo "<table width=40% border=1 align=center >
<tr>
<th>Opinion</th>
<th>Percentage</th>
</tr>";
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr align=center>';
echo "<td>" . $row['Opinion'] . "</td>";
echo "<td>" . $row['percentage'] . "</td>";
}
}
else {
echo "0 results";
}
Working code for getting the value of number of rows from a database
$sql = "SELECT * FROM `survey` WHERE Opinion='Positive'";
$connStatus = $con->query($sql);
$numberOfRows = mysqli_num_rows($connStatus);
echo "There are a total number of $numberOfRows Positive rows in the database";
echo "<br>";
echo "<br>";

Show all items from a database even without a picture

I'm making a webpage that displays my database. I'm using 2 tables for this.
Table 'artikel' and 'images'
Both tables got a key that link to each other: A_ARTCODE and I_ARTCODE
Both keys are the same.
Now I got the problem my keys were not linked and I saw this was, because I didn't used this:
WHERE artikel.A_ARTCODE = images.I_ARTCODE
So first my page looked like this: click here
and when I used that code I got this: click here
Like you see, now my both ID's are correct.
But I was talking about my tables, in the table 'images' I only got 2 images. Those you see now, but I got like 383 items in my 'artikel' table.
My question now is, how can I display them all? Even the items without a picture?
This is my total php code:
<?php
include('connect-mysql.php');
if (!empty($_GET["page"])) {
$page = $_GET["page"];
} else {
$page=1;
};
$start_from = ($page-1) * 20;
$sqlget = "SELECT *
FROM artikel, images
WHERE artikel.A_ARTCODE = images.I_ARTCODE
LIMIT $start_from, 20
";
$sqldata = mysqli_query($dbcon, $sqlget) or die('error getting');
define('IMAGE_PATH', 'images/');
$sql = "SELECT COUNT(A_ARTCODE) FROM artikel";
$rs_result = mysqli_query($dbcon, $sql) or die ("mysqli query dies");
$row = mysqli_fetch_row($rs_result) or die ("mysqli fetch row dies");
$total_records = $row[0];
$total_pages = ceil($total_records / 20);
echo "<div class='boven'>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "</div>";
echo "<table>";
echo "<tr><th>A_ARTCODE</th><th>A_NUMMER</th><th>A_OMSCHRN</th><th>A_REFLEV</th><th>A_WINKEL</th><th>I_ARTCODE</th><th>I_FILE</th></tr>";
while($row = mysqli_fetch_array($sqldata)){
echo "<tr><td align='right'>";
echo $row['A_ARTCODE'];
echo "</td><td align='left'>";
echo $row['A_NUMMER'];
echo "</td><td align='left'>";
echo $row['A_OMSCHRN'];
echo "</td><td align='left'>";
echo $row['A_REFLEV'];
echo "</td><td align='right'>";
echo $row['A_WINKEL'];
echo "</td><td align='right'>";
echo $row['I_ARTCODE'];
echo "</td><td align='right'>";
echo "<img src='". IMAGE_PATH . $row['I_PATH']. '/' . $row['I_ID']. '.png' ."' />";
echo "</td></tr>";
}
echo "</table>";
echo "<div class='onder'>";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='index.php?page=".$i."'>".$i."</a> ";
};
echo "</div>";
?>
To achieve this U could use a LEFT JOIN sql.
This will give u all records for the articles, even when no images are defined for it
SELECT *
FROM artikel a
LEFT JOIN images i
ON a.A_ARTCODE = i.I_ARTCODE
LIMIT $start_from, 20
One way is to use a left join.
select *
from artikel
left join images
on artikel.A_ARTCODE = images.I_ARTCODE
LIMIT $start_from, 20;
Break yourself of the habit of writing things like from artikel, images; this isn't 1989. Use ANSI joins.
Another way is to guarantee that every row has an image. Use a default image (and a default value with a "not null" constraint for it in the table). It might be just a gradient with the text "Picture coming soon" on it.

Using JOIN to display data in a table

Thanks for reading my question
i am trying to make *clients_id* from the table repair_jobs appear as the name from the table contacts
but i am having no luck
i have got 2 sql query's is this wrong?
the 1st
$query = "select * from repair_jobs";
this helps me display the information i need regarding the fields from repair_jobs and works
this is the 2nd
$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";
under that i have this to try to display the name of the client
echo "<td>{$client_id}</td>";
but it is only displaying the number and not the data (clients name) that i need
am i missing something?
Additional information
The client_id (repair_jobs) is a number and is the same as id (contacts) but wanting to display the name (contacts)
CLIENTS
Id – name – surname – phone – address
REPAIRS
Id – clients_id (same as id in clients) – unit – date – price
current code
<?php
//include database connection
include 'db_connect.php';
//query all records from the database
$query = "select * from repair_jobs";
//execute the query
$result = $mysqli->query( $query );
//get number of rows returned
$num_results = $result->num_rows;
//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record
//start table
//creating our table heading
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</thead></tr><tbody><tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
echo "<td>{$rmake} {$rmodel}</td>";
$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";
echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
}
echo "</tr></table>";//end table
}else{
//if database table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
Change your 1st query to you join query, as there is no reason to do a 2nd query in the middle of your code. (also you never executed that query anyway).
//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";
Then in your table keep the $client_name
echo "<td>{$client_name}</td>";
<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
echo " <table class='table_basic'>";
echo "<thead><tr>";
echo "<th>Job #</th>";
echo "<th>Name Of Unit</th>";
echo "<th>Client</th>";
echo "<th>Estimated Value</th>";
echo "</tr></thead><tbody>";
while( $row = $result->fetch_assoc() ){
extract($row);
echo "<tr>";
echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
echo "<td>{$make} {$model}</td>";
echo "<td>{$client_name}</td>";
echo "<td align='center'><span class='badge badge-success'>£";
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);
echo $sum_total;
echo '</span></td>';
echo "</td>";
echo "</tr>";
}
echo "</tbody></table>";
} else {
echo "No records found.";
}
$result->free();
$mysqli->close();
?>

how to get the sum of field 'size' from my database and print it in my php page

I have a database with a table named "photos". In that table i have a column "size"(which is the size of the photos i had upload). I want to get the sum of these sizes and print the sum in my php page. Can anyone give me an example code(sql code in php code)..??
i have try this:
$query = "SELECT SUM(ph_size) from photos";
$result= mysql_query($query,$con);
echo "<table border='1'>
<tr>
<th>SUM(ph_size)</th>
</tr>";
while($row = mysql_fetch_array($result))
{
//echo $row;
echo "<tr>";
echo "<td>" . $row['SUM(ph_size)'] . "</td>";
echo "</tr>";
}
echo "</table>";
but it doesnt work.
You can use the sum function
$result=mysql_query("SELECT SUM(p.size) AS sum_of_photos_sizes FROM `photos` p WHERE 1");
if (mysql_num_rows($result)){
$data=mysql_fetch_array($result);
echo 'Total size of photos: '.$data['sum_of_photos_sizes'];
}

Categories