Using MySQL and PHP display all the images uploaded to secured folder by customer name

I am using the following php and mysql code to display all the images uploaded by customers.

<?php
$con=mysqli_connect("localhost","root","xxxxxx","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM `cust-results`");

echo "<table border='1'>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['customer-name'] . "</td>";
echo "<td><a href=uFiles/storage/files/". $row['file1'] .">" . $row['file1'] . "</a/></td>";
echo "<td><a href=uFiles/storage/files/". $row['file2'] .">" . $row['file3'] . "</a/></td>";
echo "<td><a href=uFiles/storage/files/". $row['file3'] .">" . $row['file4'] . "</a/></td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Open in new window

The table as rowid, customer-name,file1,file2,file3,submitdate as columns. here are some of the rows

Customer Name File1     File2       File3
John Doe	test.jpg	test1.jpg	test2.jpg
Jane Done	C1.jpg	    kitten-1.jpg	
test test	EM_0_rzefyw.jpg	EM_0_fkddmu.jpg	EM_2.jpg
John Doe	cat_wallpaper.pdf	

Open in new window


so If I do GROUP BY customer-name only one instance of customer name is being show so John Doe shows only once which is the first row.

I am looking to display like all the images uploaded by a particular customer like

Customer Name: John Doe
display all the images

Customer Name: Jane Doe
display all the images

Thanks
LVL 2
niceoneishereAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
You'd really need to do 2 separate queries - one to select all the customers (GROUP BY or DISTINCT) and then as you loop through, select the images for each customer. Something like this:

$customers = mysqli_query($con,"SELECT DISTINCT customer-name FROM cust-results");

echo "<table border='1'>";

while($customer = mysqli_fetch_array($customers))
{
   echo "<tr>";
   echo "<td colspan='3'>" . $customer['customer-name'] . "</td>";
   echo "</tr>";

   $imageQuery = sprintf("SELECT file1, file2, file3 FROM cust-results WHERE customer-name = '%s';", $customer['customer-name']);
   $images = mysqli_query($con, $imageQuery);
   while($image = mysqli_fetch_array($images))
   {
      echo "<tr>";
      echo "<td><a href=uFiles/storage/files/". $image['file1'] .">" . $image['file1'] . "</a/></td>";
      echo "<td><a href=uFiles/storage/files/". $image['file2'] .">" . $image['file2'] . "</a/></td>";
      echo "<td><a href=uFiles/storage/files/". $image['file3'] .">" . $image['file3'] . "</a/></td>";
      echo "</tr>";
   }
}
echo "</table>";

Open in new window


It would probably work better if you had a customerID to use in the queries instead of customer-name.
niceoneishereAuthor Commented:
Thanks again coming to rescue :)

It works. I do have submitdate column and was wondering if there is way to include the submitdate and sort by the date submitted.

Thanks and appreciate it
Chris StanyonWebDevCommented:
Sure - Just add the column to the SELECT query and include an ORDER BY clause. Add a new table cell in the output

$imageQuery = sprintf("SELECT file1, file2, file3, submitdate FROM cust-results WHERE customer-name = '%s' ORDER BY submitdate;", $customer['customer-name']);
...
echo "<td>" . $image['submitdate'] . "</td>";

Open in new window


If you add an additional <td> don't forget to change the colspan of the header row as well:

echo "<td colspan='4'>" . $customer['customer-name'] . "</td>";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
niceoneishereAuthor Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.