Php Echo Multiple Rows In A Random Order - php

I have 2 MySql Tables. Videos and Photos. When a user uploads a file it gets stored in one of the tables depending on the file.
heres the code:
index.php:
getVideos($conn);
getPhotos($conn);
get.inc.php:
function getVideos($conn) {
$sql = "SELECT * FROM videos ORDER BY date desc";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<div class='video-box'><p>";
echo $row['title']."<br>";
echo "by:";
echo $row['uid']."<br>";
echo $row ['videofile'];
echo "</p>";
}
}
function getPhotos($conn) {
$sql = "SELECT * FROM photos ORDER BY date desc";
$result = $conn->query($sql);
while ($row = $result->fetch_assoc()) {
echo "<div class='photo-box'><p>";
echo $row['title']."<br>";
echo "by:";
echo $row['uid']."<br>";
echo $row ['photofile'];
echo "</p>";
}
}
there are 3 videos and 2 photos in my database curently *
When I have the code as I do Now all 3 of the videos from my database appear first on the the home page then when you slide down the 2 photos appear at the bottom.
How do I make it So the photos and videos are mix and are dispalyed by the date they were uploaded (newest at the top older at the bottom.) Without them being separated like they are now. Please Help.
- Thanks in advance

You can use UNION
SELECT * FROM
(SELECT id, date, videofile as file FROM videos
UNION
SELECT id, date, photofile as file FROM photos) t
ORDER BY t.date;
Here you can play with example: http://sqlfiddle.com/#!9/92c8fc/1/0
Altough it is possible it will probably be better to store this information just in one table. If you need to preserve information about what is video file and what is photo you can just add another column where you store this kind of meta data.

Related

How can i display the 5 latests results from the database at the bottom of the page in PHP instead of displaying it at the top?

This is my PHP file:
<?php
$sql = "SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5";
$result = mysqli_query($con, $sql);
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
echo "<p>";
echo "<span class = 'usr'>".$row['user_name']."</span>";
echo "<br>";
echo $row['message'];
echo "<br>";
if($row['Date'] === date("Y-m-d")){
echo "Today ";
}
else{
echo $row['Date'];
}
echo $row['Time'];
echo "</p>";
}
}
else{
echo "No messages were found!";
}
?>
This is how my database table looks:
An image of the database table
And this is how it displays the data in the PHP file:
An image of the PHP file output
It displays the five latest rows at the top, is it possible to flip and make it display it at the first bottom and then display the others on top of it?
I tried using DESC instead of ASC but then it displays the five oldest rows instead of the latest
I am not sure this is a perfect method
but this could solve your problem
select * from (SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5) G ORDER BY G.id ASC;
This IS wrapping your SQL with a similar one
so in subquery it will fetch all latest 5 items and in main query it will sort by ASC
Try :
$sql = "SELECT * FROM (SELECT * FROM `general_chat` ORDER BY `general_chat`.`id` DESC limit 5) as G ORDER BY `G`.`id` asc";

Delete file from folder and mysql joined table

I have a file upload system where I can delete from MySQL DB and from the folder.
The only problem is that all the files in the folder are deleted instead of just the single one. Each folder is a custom folder with a person's name.
I have a joined table where the id's of two tables are equal. I need the joined tabled to receive the name of the person to know the file location.
This is my working code so far where multiple files are deleted instead of the one I selected:
$analyse = mysqli_query($conn, "SELECT *
FROM analyse
INNER JOIN persons ON
analyse.id_person = persons.id");
while ($row = mysqli_fetch_array($analyse)) {
$img = $row['img_name'];
$name= $row['name'];
$frontname= $row['frontname'];
$image_url = "../persons/{$name} {$frontname}/analyse/{$img}";
$result = mysqli_query($conn, "DELETE FROM analyse WHERE id=$id");
unlink($image_url);
if($result){
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "Failed to delete";
}
}
For example: Person X with id 15 has three images with different ids but each time with the ID(15) of the person. I want to be able to delete the single selected file instead of all the files with id 15.
UPDATE
I found the solution, the ID that came through for deleting was always the first in my DB row. The fetching of my array was wrong and my ID consequently too.
Thanks for the answers.
Try using Limit 1 with your delete Query.
$analyse = mysqli_query($conn, "SELECT *
FROM analyse
INNER JOIN persons ON analyse.id_person = persons.id");
while ($row = mysqli_fetch_array($analyse)) {
$img = $row['img_name'];
$name= $row['name'];
$frontname= $row['frontname'];
$image_url = "../persons/{$name} {$frontname}/analyse/{$img}";
$result = mysqli_query($conn, "DELETE FROM analyse WHERE id=$id LIMIT 1");
unlink($image_url);
if($result){
header('Location: ' . $_SERVER['HTTP_REFERER']);
} else {
echo "Failed to delete";
}
}

Sql Query: 2 tables but looking for all information from one and only one row from the other

I have 2 tables
Photos: stores all photo information
Gallery: stores heading and description about the gallery
I need to echo to a new page the Gallery Description, then all the photos that should be there.
but I keep getting heading and description repeating because its in the same
$row = mysql_fetch_array($result)...
then there is more that one set of photos in that gallery I need also?
anyone help or am I being to vague....
$a="SELECT * from gallery where gallery_category=".$gallery_category;
$result = mysql_query($a,$c);
while($row = mysql_fetch_array($result)) {
echo $row['gallery_name'].'<br>'.$row['gallery_description'].'<br>';
$sql="SELECT * FROM photos WHERE gallery_id =".$gallery_id." ORDER BY photos_filename";
$result2 = mysql_query($sql,$c);
while($row = mysql_fetch_array($result2)) {
echo'<a rel="example_group" href="../galleries/images/'.$row['gallery_id'].'/'.$row['photos_filename'].'" width="130" height="100"><img src="../galleries/images/'.$row['gallery_id'].'/'.$row['photos_filename'].'" height="150px" alt=""/></a>';
}
}
Actually I'll post it up here so I can use some code formatting.
Option 1 - loop within a loop
$headerquery = $db->query("SELECT * FROM tbl1");
while ($headers= $db->fetchNextObject($headerquery )) {
echo "<table><tr><th>".$headers->GalleryName."</th></tr>"; // open table create header
$detailquery = $db->query("SELECT * FROM tbl2 WHERE GalleryID=".$headers->ID);
while ($details= $db->fetchNextObject($detailquery )) {
echo "<tr><td>".$details->Photo."</td></tr>"; //loop through 2nd table and spit out photos
}
echo "</table>"; //close table
}
Option 2 - joined query with selector
$galleryheaderid = 0;
$query = $db->query("SELECT * FROM tbl1 INNER JOIN tbl2 on tbl1.ID=tbl2.GalleryID");
while ($details = $db->fetchNextObject($query )) {
echo "<table>";
if ($galleryheaderid!=$details->ID) { //spit out header row if id's don't match
echo "<tr><th>".$details ->GalleryName."</th></tr>"; //create header
$galleryheaderid = $details->ID; //set header id to gallery id so we don't show header a 2nd time
}
echo "<tr><td>".$details->Photo."</td></tr>"; //spit out gallery information even on first row since all rows include photo information
echo "</table>"; //close table
}
Something like either of these should work obviously they'll need completing properly

MySQL/PHP Count

I have 2 MySQL tables, one for storing albums and the other for songs. I am displaying a list of albums in a table, and I want to be able to add a column called songs to display the number of songs in this album. The way I have it now just messes up my table:
Thanks for everyone's help!
Assuming that "playlist" is what you consider an album and the first while loop iterates on playlists, I'd rewrite your code like this:
while($row = mysql_fetch_array($rs)) {
echo "<tr>\n";
echo "<td>".$row['playlist_id']."</td>";
// Assuming playlist_id is an integer value in your database
$query = "
SELECT Playlist_id, COUNT(Playlist_id) AS songCount
FROM ws_music
WHERE Playlist_id = ". intval ($row['playlist_id']) ."
GROUP BY Playlist_id
";
$result = mysql_query($query) or die(mysql_error());
// No need for the second while loop
$row2 = mysql_fetch_array($result);
echo "<td>There are ". $row2['songCount'] ." ". $row2['Playlist_id'] ." song.</td>";
echo "</tr>";
}

Displaying items from multiple tables on your webpage using php while loop

I am trying to dsiplay data from 2 different tables from my mysql database using a while loop.
Currently I can display the data from 1 table and limit the results to 3. I then want to display the first 5 records from another table. If I join the tables I can only display the same number of items from both using LIMIT?
I am using a while loop to display the content from a table called item, using the following code;
$query");
$result2 = #mysql_query($query, $connection)
or die ("Unable to perform query$query");
<?php
while($row= mysql_fetch_array($result))
{
?>
<?php echo $row['item'] ?>
<?php
}
?>
If I start another loop for the data from the next table called movie, however the data is not displayed using the following code;
<?php
while($row= mysql_fetch_array($result2))
{
?>
<?php echo $row['title'] ?>
<?php
}
?>
What is the best way to display the data from the 2 tables?
Many Thanks
I don't know if you forgot to paste a bit of code, but this should work:
<?php
$query = "select * from item order by date, time asc limit 0,3";
$result = mysql_query($query);
while($row= mysql_fetch_array($result))
{
echo $row['item'];
}
$query2 = "select * from movie limit 0,5";
$result2 = mysql_query($query2);
while($row= mysql_fetch_array($result2))
{
echo $row['movie'];
}
?>
You may be able to do it with one SQL Query too:
SELECT i.item, m.movie
FROM (SELECT * FROM item ORDER BY date, time ASC LIMIT 0,3) i,
(SELECT * FROM movie limit 0,5) m
Then in php:
<?php
while($row= mysql_fetch_array($result))
{
echo $row['item'];
echo $row['movie'];
}
?>
But that depends on how you want to format the output.

Categories