PHP Database - Output total revenue by multiplying two values - php

Alright so I have a website, paynite.com
which uses a PHP script. I'd like to show the total revenue by each user.
So the user's are defined by the ID: usern
and this ID exists in the table: Users
REF: http://i.stack.imgur.com/VA9Zl.png
I have products that are sold on my site in the table called: items
and I want the row's "sales" and "price_extended" to multiple to form $revenue in the code only when the author ID and the usern matches
Author is defined by the ID: author
and this ID exists in the table: items
REF: https://i.gyazo.com/34dc923c79263c1ec7a0918f7741e4ac.png
How can I do this? I've been stuck on this for like 4 hours :( I have very little coding knowledge lmao.
The code I tried to use outputs the total revenue vs sales across the whole site..
<div class="center aligned column dashboard-product-info-box">
<div class="ui statistic">
<div class="value dashboard-product-info-value" id="dashboard_product_revenue_value">
<?php $revenue = 0; $sql = mysql_query("SELECT * FROM sellify_items ORDER BY id"); if(mysql_num_rows($sql)>0) { while($row = mysql_fetch_array($sql)) { $revenue = $row['sales'] * $row['price_extended']; } } echo (number_format($revenue)); ?></div>
<div class="label dashboard-product-info-title">Revenue</div>
</div>
Help would greatly be appriciated :)

Related

How would I select and display the most recently added rows from a SQL table?

I am making an extremely basic posting system, and I cant seem to figure out how to get the most recent rows from a certain table. I have tried other solutions offered here, but my posts were randomly placed. How would I accomplish this? My code is below.
function load_posts(){
$posts_sql = "SELECT * FROM posts";
$posts_result = Phoenix\Database\Database::$database->query($posts_sql);
while($row = $posts_result->fetch_assoc()){
$posts_display = '
<div class = "card" style = "width:500px">
<div class = "card-body">
<div class = "card-title">'. $row['username'] .'</div>
<p>'. $row['content'] .'</p>
</div>
</div>
';
echo $posts_display;
}
}
Again, I want the posts to be displayed from most recent, to old.
You need to have information in each row that captures this information. The most common suspects are:
an auto-incrementing id
a creation date
Then you just ask the database to sort the results. For instance, if post_id is an auto-incremented id:
select p.*
from posts p
order by p.post_id;
SELECT * FROM TableName ORDER BY id DESC
// order by should be with primary key

Another SQL query in a loop (row) query

I am struggling with my code. I try to make a News Feed.
The news feed is working well but now I want to add a avatar picture on each news row to have a picture from the author. The avatar picture path is in a different table (users).
That means I need to get in the loop the specific user ID ($row['uid']) and with that uid I need to get the related path from the user's avatar in the user table.
If I try to use a query in my loop it shows me only one result instead of 6 as specified in my query.
Do you have any suggestion or advise how I can solve that issue?
Many thanks for your support!
This is my attempt for the moment:
<div class="timeline p-4 block mb-4">
<?php
// Getting the user id and the feedmessage from Table "activity_feed"
$statement = $pdo->prepare("SELECT feed_message, uid FROM activity_feed WHERE cid = :cid ORDER BY id DESC LIMIT 6");
$result = $statement->execute(array('cid' => $cid));
$count = 1;
while($row = $statement->fetch())
{
// Starting the News feed Loop
?>
<?php
// This will repeat now X times as defined in the query above by = LIMIT ?
// Getting the avatar path from the user table
$statement = $pdo->prepare("SELECT avatar FROM users WHERE id = :id");
$result = $statement->execute(array('id' => $row['uid']));
$user_avatar = $statement->fetch();
?>
<style>
#circle_feed {
height:50px;
width:50px;
border-radius:50%;
/* Including the avatar path from query above*/
background-image: url("<?php echo $user_avatar['avatar']; ?>");
background-position:center;
background-size:cover;
}
</style>
<div class="tl-item ">
<div class="tl-dot"><a class="tl-author" href="#" data-abc="true">
<!-- Including the Avatar circle here-->
<span class="w-40 avatar circle gd-warning border" id="circle_feed"></span></a>
</div>
<div class="tl-content">
<div class=""><?php echo $row['feed_message']; ?></div>
<div class="tl-date text-muted mt-1">DATE</div>
</div>
</div>
<?php
// News Feed Loop is ending here
}
?>
</div>
</div>
There is no need to loop. SQL is a set-based language that can give your the results that you want in a single query, using the join syntax:
SELECT
af.feed_message,
af.uid,
u.avatar
FROM activity_feed af
INNER JOIN users u ON u.id = af.uid
WHERE af.cid = :cid
ORDER BY af.id DESC
LIMIT 6
This is much more efficient than running 7 queries (one for the activity feed, then one for each row returned by the first query, in a PHP loop).
You can then fetch the rows of the resultset and use this directly in your application.

how to show separate column unique values with SQL GROUP BY clause

Alright, I can't bash my head at this anymore tonight. Thought I would pose the question. Maybe there's a simple answer I have not come across yet.
I have a number of related tables in PHPMyAdmin: pubtopics, pubtypes, and pubtitles.
Pubtitles is the main table (table 1), pubtopics is a simple list assigning a number to a topic (table 2), and pubtypes is another simple list assigning a number to a publication type (table 3).
I'm trying to create a page that lists every publication by type (list of all pubs in table 3), but also shows the multiple topics (table 2) that may be assigned to a unique pubtitle (table 1), and have each of those topics (table 2) link to publications listed by that topic.
The data for pubtopics looks something like this:
pubtopics
What I'm looking for is a line like this:
June 15, 2016 | Comparative Data Report | Education | Fiscal Affairs
Where the date is a link, the pub type is a link, and each of the concatenated issues is a unique link.
When I try to list all the publications for a specific publication type, all I can generate so far is either a list with multiple duplicate entries (because one may address several topics). I've tried group_concat and group by, but that doesn't allow me to have an individual link for each topic.
I've played around with explode and arrays, but it's a bit beyond my know-how. Any brilliant solutions?
Code looks like:
elseif (isset($_GET['type'])) {
$var3_getDisplay4 = $_GET['type'];
$query_getDisplay = sprintf("SELECT
DATE_FORMAT(publicationsnew.date, '%%Y') AS archive,
DATE_FORMAT(publicationsnew.date, '%%M %%e, %%Y') AS pubdate,
pub_type.number AS typelink,
pub_type.type AS pubtype,
GROUP_CONCAT(categories.category_name ORDER BY categories.category_name SEPARATOR ' | ') AS category,
categories.category_id AS catlink,
publicationsnew.title,
publicationsnew.author,
publicationsnew.imagelink,
publicationsnew.description
FROM
pub_cat
LEFT JOIN publicationsnew ON (pub_cat.pub_id = publicationsnew.pub_id)
LEFT JOIN categories ON (pub_cat.category_id = categories.category_id)
LEFT JOIN pub_type ON (publicationsnew.type = pub_type.number)
WHERE pub_type.number = %s
GROUP BY publicationsnew.pub_id
ORDER BY publicationsnew.date DESC",
GetSQLValueString($var3_getDisplay4, "text"));
and in the body:
<?php do { ?>
<h2><?php echo $row_getDisplay['pubdate']; ?> | <?php echo $row_getDisplay['pubtype']; ?> |
<a href="test.php?topic=<?php echo $row_getDisplay['catlink']; ?>">
<?php echo $row_getDisplay['category']; ?>
</a>
</h2>
<p class="pubtitle"><?php echo $row_getDisplay['title']; ?></p>
<?php echo ($row_getDisplay['author']); ?>
<?php echo ($row_getDisplay['imagelink']); ?>
<div class="pubdescription"><?php echo ($row_getDisplay['description']); ?></div>
<div class="clear"></div>
<hr>
<?php } while ($row_getDisplay = mysql_fetch_assoc($getDisplay)); ?>
Burned out for the day. To be continued...

need to count how many student applied for particular course. and show this count in (UI)Frontend part

i have project at university ,where one requirement is student have to applied to the course posted by various institution.
for eg. if institute provide mechanical, chemical courses.and if number of student applied for mechanical course.
and my question is i need to count how many people applied for mechanical course. and show this count in (UI)Frontend part.
this is query i have written to retrieve students who applied for course.
$q="select *
from students
where student_id in (
select applied_stdid
from applicants
where a_subjectid=".$_GET['id']." )";
and the code which i have written to find count and display is------>
<?php
$count=0;
while($row=mysql_fetch_array($res))
echo '<tr> <td width="10%">'.$count.'
<td width="50%">'.$row['student_fnm'].'
<td width="30%">Student Information
';
$count++;
}
echo"count is".$count;
?>
above code is fine i m able to get the count but i have to show the count infront of course like mechanical-- 20 student applied.i have to show this count in another(main page)page.the count which i m getting i have to show this in manin page
I Created a function for you it'll solve your issue. Here is my code:
<?php
//you can name this file as functions.php
function fetchcount() {
$sql = "SELECT * from applicants WHERE a_subjectid='1'";
// eg. 1 for (mechanical) but you have to create individual function for all the courses
$query = mysql_query($sql);
while ($row = mysql_fetch_object($query)) {
$totlal=mysql_num_rows($query );
}
return $totlal;
}
?>
In Your main file where you want to print the count. just do this:
require_once 'functions.php';
$res = fetchcount(); /
echo "count is".$res; // put it anywhere you want

Grouping values from my_ MySQL to be shown in separate <div> on the basis of certain criteria

I am working on a php project with MySQL as database. Here is the table structure
product_id (PK)
shop_id (FK)
product_name
product_desc
Product_id is the primary key and shop_id is the foreign key (shows the product belongs to which shop). Now I want to select these product and display them in a <div>. For each shop products, I want to display a different <div>. e.g if there are three products say p1,p2 and p3, and they have same shop_id (say) 1, i want them to show in a separate <div>. there will be separate <div> for each shop_id, and all products belong to that shop_id will be displayed in that <div>. How can I do this with My_SQL query or using PHP. I have used a simple SELECT query like this but with this i could not find a way to initiate different <div> for separate shop.
$q = mysql_query("SELECT * FROM table");
echo "<div>";
while($product = mysql_fetch_assoc($q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
It just throw all products in a single dive, I want separate div for separate shop (which contains all products of that shop). Thanks for reading..
Simplest way is to group the products by id in a array and loop through them
$all_products = array();
while($product = mysql_fetch_assoc($q)){
$all_products[$product['shop_id']][] = $product;
}
foreach($all_products as $shop_id => $product_array){
echo "<div id='shop_".$shop_id."'>";
foreach($product_array as $product){
echo $product['product_name']."<br/>";
}
echo "</div>";
}
OR call product data fro each shop id
$q = mysql_query("SELECT * FROM shops");
while($shops = mysql_fetch_assoc($q)){
$product_q = mysql_query("SELECT * FROM product_table WHERE shop_id=$shops['id']");
echo "<div>";
while($product = mysql_fetch_assoc($product_q)){
echo $product['product_name']."<br/>";
}
echo "</div>";
}

Categories