Another SQL query in a loop (row) query - php

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.

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

How to Show row number for every row fetched from mysql

I want to show row number for every row fetched from database. can someone help me to do this.
My Code to fetch rows is:
$query="
select id
, title
, description
, url,votes
from posts
order
by votes desc
";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$arr[] = $row;
}
}
# JSON-encode the response
$json_response = json_encode($arr);
// # Return the response
echo $json_response;
My code displays post, title, post votes and post controls (Vote up or Down).
I want to show row number for every row fetched.
My Code to Display posts is:
<ul class="thumbnails" ng-controller="votingCtrl">
<li ng-repeat="post in posts" class="clearfix">
<div class="col-md-1 voting well">
<div class="votingButton" ng-click="upVote(post);">
<i class="glyphicon glyphicon-chevron-up"></i>
</div>
<div class="badge badge-inverse">
<div>{{post.votes}}</div>
</div>
<div class="votingButton" ng-click="downVote(post);">
<i class="glyphicon glyphicon-chevron-down"></i>
</div>
</div>
<div class="well col-md-11">
<h4>{{post.title}}</h4>
<p>{{post.description}}</p>
</div>
</li>
</ul>
Thanks
try to use ROW_NUMBER() mysql function
select id,title,description,url,votes, ROW_NUMBER() OVER (ORDER BY votes DESC) as row_num from posts order by votes desc
With the current logic, row number can be displayed using {{post.id}} as the SELECT query fetches id along with votes as fields.
In the loop itself, we can display a custom counter like:
$arr = array();
$cntr = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
++$cntr;
$row['cntr'] = $cntr;
$arr[] = $row;
}
}
And in view template, you can use {{post.cntr}}.
SELECT
(#row_number:=#row_number + 1) AS num,
id,
title
FROM
Test,
(SELECT #row_number:=0) AS t
LIMIT 5;
Please try above its tested it will work, only need to replace your query with that.

Getting linked data from multiple tables into seperate bootstrap cards

I very recently (as in, this week) started learning PHP and am doing a practice task.
(Therefore, apologies in advance, if I sound like I don't know what I'm talking about, that is true)
Here is a crude schematic of my tables:
I have a main table products, and there are 3 planned product types (book, disc, furniture), and each type has a special attribute, with their own tables. (size for discs, weight for books etc.)
So far through testing, I've managed to query out the object from my products table each into their separate bootstrap card components. What I am trying to accomplish now is to add the special attributes to their respective object cards.
This is where i've reached a dead end. The idea I have is to first create a seperate array $printtocard, then call all the products table records by type, then run a foreach through all the found records, and then run a switch inside the foreach where i check the product types and run cases based on type, which create new objects based on the linked table data, and add each of them to the $printtocard array.
Afterwards the plan is to call the array in the code that later outputs the objects.
Here is my code so far:
Main php code
class products {
var $arrayy;
var $sql;
var $typepr;
protected function get_prod($conn1){
$printtocard = array();
$call_type = 'SELECT type FROM products ORDER BY id';
$type_res = mysqli_query($conn1, $call_type);
$type_arr = mysqli_fetch_all($type_res, MYSQLI_ASSOC);
$this->typepr = $type_arr;
return $this->typepr;
foreach($typepr as $xxx){
switch($xxx){
case "disc":
/* $disc_ */$sql = 'SELECT p.sku, p.name, p.price, a.value FROM products AS p INNER JOIN attr_size AS a ON p.id = a.product_id;';
$val_res = mysqli_query($conn1, $sql);
$val_arr = mysqli_fetch_all($val_res, MYSQLI_ASSOC);
$printtocard[] = $val_arr;
return $this->printtocard;
break;
case "book":
/* $book_ */$sql = 'SELECT p.sku, p.name, p.price, a.value FROM products AS p INNER JOIN attr_weight AS a ON p.id = a.product_id;';
$val_res = mysqli_query($conn1, $sql);
$val_arr = mysqli_fetch_all($val_res, MYSQLI_ASSOC);
$printtocard[] = $val_arr;
return $this->printtocard;
break;
case "furniture":
/* $furniture_ */$sql = 'SELECT p.sku, p.name, p.price, a.height, a.width, a.length, FROM products AS p INNER JOIN attr_dims AS a ON p.id = a.product_id';
$val_res = mysqli_query($conn1, $sql);
$val_arr = mysqli_fetch_all($val_res, MYSQLI_ASSOC);
$printtocard[] = $val_arr;
return $this->printtocard;
break;
}
}
//$sql = 'SELECT p.sku, p.name, p.price, a.value FROM products AS p INNER JOIN attr_size AS a ON p.id = a.product_id;';
//$call2 = 'SELECT sku, name, price FROM products ORDER BY id';
//$val_res = mysqli_query($conn1, $sql);
//$val_arr = mysqli_fetch_all($val_res, MYSQLI_ASSOC);
$this->arrayy = $printtocard;
return $this->arrayy;
}
public function get_prod2($conn1)
{
$this->get_prod($conn1);
}
}
$fff= new products;
$fff->get_prod2($conn);
$sss= $fff->arrayy;
Snippet of the webpage code where I autogenerate the cards:
<div class="container">
<div class="row">
<?php
foreach($sss as $value){ ?>
<div class="col s6 md2">
<div class="card cardstyle z-depth-0">
<div class="card-content center">
<?php echo htmlspecialchars($value['sku']); ?>
<div>
<?php echo htmlspecialchars($value['name']); ?>
</div>
<div>
<?php echo htmlspecialchars($value['price']); ?>
</div>
<div>
<?php echo htmlspecialchars($value['value']); ?>
</div>
</div>
<div class="card-action right-align">
<a class="brand-text" href="#"> more info </a>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
The error message I currently get:
Warning
: Invalid argument supplied for foreach() in
C:\xampp\htdocs\uzdevumi\scandi_uzdevumi_kristianskonters\productlist\productlist.php
on line
108
A number of pointers
1. Its better to define your variables at the top depending on your intentions later (protected, private or public)
protected $arrayy;
protected $sql;
protected $typepr;
The return just before your "foreach" loop renders your loop pointless because it stops your function there and returns what you have.
return $this->typepr;
In connection to my previous point, you seem to think that "return" does some concatenation for you as shown by each "case" you have in your switch. No it does not it exits your function back to the calling scope.
return $this->printtocard;
The warning you have at the end means what it says, you are providing the "foreach" loop with an invalid argument. In other words it does not exist, thankfully its php (dynamically typed). But the problem is its undefined (has not value). Maybe its a typo and you wanted to loop through the array variable you got from sql calls.
foreach($typepr as $xxx)
Cross check typos and I did not get to check your html.

PHP Database - Output total revenue by multiplying two values

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 :)

PHP MySQL get data from 2 tables

I am trying to combine 2 tables from my database:
files table:
id
file_name
file_description
file_url
access_files table:
id
student_id
file_id
Here is my sql code, currently getting all files from the files table, it doesn`t show the selected files for the user.
<?php
$SQL = "SELECT * FROM files, access_files WHERE student_id ='$studentid'";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
?>
<div class="accordion-group">
<div class="accordion-heading">
<a href="#<?php print $db_field['file_id']; ?>" data-parent="#accordion" data-toggle="collapse" class="accordion-toggle collapsed">
<?php print $db_field['file_name']; ?>
</a>
</div>
<div class="accordion-body collapse in" id="<?php print $db_field['file_id']; ?>">
<div class="accordion-inner">
<?php print $db_field['file_description']; ?><br/><br/>
Download File Now!
<br/><br/>
</div>
</div>
</div>
<?php } ?>
The code is suppose to show only the files associated to the user.
What you need to do is JOIN the tables.
Most common types of JOINs:
INNER JOIN - Used to match data where it can match the ON () statement. If the ON() doesn't match, the result will be excluded.
LEFT JOIN - Used if the data you match in the ON() doesn't have to be there. It just appends the data to the original FROM, and fills columns with NULL if no data is matched.
Example
SELECT
ft.id,
ft.file_name,
ft.file_description,
ft.file_url,
af.id as access_id,
af.student_id,
af.file_id
FROM
files ft
INNER JOIN access_files af ON ( ft.id = af.file_id )
WHERE
fa.student_id = '$studentid'
You are making a classic cartesian join with your query:
SELECT * FROM files, access_files WHERE student_id ='$studentid'
You need to specify how the two tables are connected:
SELECT * FROM files a, access_files b WHERE a.student_id ='$studentid' and b.studentID=a.student_id
If you don't specify the link - or don't have one, the database will try to link every single row in the first table with every single row in the second.
Join your tables.
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table1.pk = table2.fk
WHERE table1.pk = 1;

Categories