Create a path to image folder using mysql and php - php

Basically I have to create a jukebox type chart via mysql and php using xampp.
I've done the basics of setting up the table my referring to mysql database etc. I just dont get how to code the path to the image folder I have created.
My Image folder is in htdocs under my folder I created called Jukebox.
This is my code:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jukebox";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Music";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Album</th>
<th>Albumcover</th>
<th>Play</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td>" . $row["Albumcover"]
. "</td>
<td>" . $row["Play"] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
This is what comes up as my online jukebox, and in the Album cover column I want the album art to come up
And this is where my images are, how would i create the path to img folder so the album art comes up?

I'm assuming you want the image in the "Albumcover" part of your code. If so you can echo out a html line. You can name your images in the "jukebox" folder with the album name. The code will appear as
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td>" <img src=\"jukebox/".$row["Album"].".png\" >"</td>
For that all images will need to be of file type png which is easy to do, just save them normally and add .png at the end. The same album name on your database will need to be the same name given to the file. There are other filetypes you can use such as .jpg.

I don't really see your problem.
What do you store in "albumcover"?
Let's say you save the relative path to your albumcover.
So you can print out
<td><img src="' . $row["Albumcover"] . '"></td>

Use your mp3 as a tag and open that in a new tab.May be this is you needed.I assume jukebox/mp3/ as your mp3 folder
<?php
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td><img src='jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"]."></td>
<td><a href='jukebox/mp3/" . $row["Play"] . "' target='blank'>" . $row["Play"] . "</a>
</td>
</tr>";
}

Related

Phpmyadmin not opening html table (potential server issue)

I am having issues with linking mysql to html through a server, the ports are all in order and the I have had no issues in linking the server to phpmyadmin. I have no password on the server and the username is 'root'.
please help :)
<?php
$servername = "localhost";
$username = "root"
$password= "";
$database = "stock_order";
$connection = new mysqli($servername, $username, $password, $database)
I believe this might be an error because my server port is 3008 however, that shouldn't be an issue
// checking connection
if ($connection)->connect_error {
die("connection failed: " . $connection ->connect_error);
}
//read the data from the customer table
$sql = "SELECT * FROM customertable";
$result = $connection->query($sql);
if (!$result) {
die("Invalid query: " .$connection->error);
}
// while loop - reads data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>" . $row["ItemID"] . "</td>
<td>" . $row["ItemName"] . "</td>
<td>" . $row["Quantity"] . "</td>
<td>" . $row["Description"] . "</td>
<td>" . $row["DeliveryMerchant"] . "</td>
<td>" . $row["Cost_of_item"] . "</td>
<td>" . $row["VAT_Percentage"] . "</td>
<td>" . $row["VAT"] . "</td>
<td>" . $row["Total_cost_of_item"] . "</td>
<td>
<a class='btn btn-primary btn-sm' href='update'>Update</a>
<a class='btn btn-danger btn-sm' href='delete'>Delete</a>
</td>
</tr>";
}
?>
</thead>
<tbody>
<tr>
<td>78956</td>
<td>Merch 2008 Monitor 4K</td>
<td>1</td>
<td>Fragile</td>
<td>TPL Resources</td>
<td>£1080</td>
<td>20%</td>
<td>£216</td>
<td>£1296</td>
<td>
<a href='update'>Update</a>
<a href='delete'>Delete</a>
</td>
</tr>
</tbody>
</table>
<body>
</body>
<?php
$conn-mysqli_connect("localhost", "root", "", "customer_order_table");
$sql = "SELECT * FROM stock_order";
$result = $conn ->query($sql);
if ($result->num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
echo "<tr><td>" . $row["itemID"] . "<tr><td>" . $row["ItemName "] . "<tr><td>" .
$row["Quantity"] .
"<tr><td>" . $row["Description"] . "<tr><td>" . $row["DeliveryMerchant"] . "<tr>
<td>" . $row["VAT_Percentage"]
. "<tr><td>" . $row["VAT"] . "<tr><td>" . $row["Total_cost_of_item"] . "<tr><td>"
}
}
else {
echo "No Results";
}
$conn ->close();
?>
</style>
</table>
</body>
</html>
when I type in 'localhost/customertable/' in the address bar the page cannot be found, displaying an error 404.
Thanks in advance :)

Merge and display 2 database and 2 tables

Hey guys i have a problem, there is a page where i need to display data acording to date, but from 2 databases and from 2 tables from each databases,
1 Database name = highmob_comenzi Tables = players and vanzari
2 Database name = highmob_comenzi2 Tables = players and vanzari
this is the code what i got , i have tryed "select * from players, vanzari" but still no luck, even that wont extract data from both databases :(
<table class='table table-responsive-sm table-bordered table-striped table- sm' border="2px">
<thead>
<tr>
<th>Locatia Vanzari</th>
<th>Tip. Cert.</th>
<th>Nr.</th>
<th>Status Comanda Mobila</th>
<th>Status Comanda Tapiterii</th>
<th>Status Livrare</th>
<th>Ora Livrare</th>
<th>Detalii Comanda</th>
<th>Total</th>
<th>Avans</th>
<th>Rest</th>
<th></th>
</tr>
<?php
$servername = "localhost";
$username = "id";
$password = "pw";
$dbname = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
WHERE statuslivrare >= CURRENT_DATE()");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
</tbody>
</table>
you can access to the table in the same query without problem eg (assuming the tables have the same struct in both the database):
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
this is only a sample for accessing to the 2 database in a single query. you must rethink the query with you join and condition
but based on you comment seem you need just (assuming that statuslivrare is a colum of the vanzari table)
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
WHERE vanzari.statuslivrare >= CURDATE()");
<?php
$servername = "localhost";
$username = "ID";
$password = "PW";
$dbname1 = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname1 );
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select * from highmob_comenzi2.players cross join highmob_comenzi2.vanzari
");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
This is what i got and its working, it displays data from 1 database nammed highmob_comenzi2 and tables players and vanzari, but im getting alot of duplicates on display and also i need to insert a WHERE clause :
WHERE statuslivrare >= CURRENT_DATE()
any ideea?

How to upload a folder of images using php and mysql

Basically I have to create a jukebox type chart via mysql and php using xampp.
I've done the basics of setting up the table my referring to mysql database etc. I just don't get how to code the path to the image folder I have created. My Image folder is in htdocs under my folder I created called Jukebox. This is my coding:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jukebox";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Music";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Album</th>
<th>Albumcover</th>
<th>Play</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td>" . $row["Albumcover"]
. "</td>
<td>" . $row["Play"] . "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
} ?>
</body>
</html>
This is what my php coding looks like
This is my image folder which i wish to create a path to so that all the album art can come up in the albumcover column
How do I create this path with php and mysql
<style>
.preview
{
width:400px;
border:solid 1px #dedede;
padding:10px;
color:#cc0000;
}
</style>
<?php
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td><img class='preview' src='jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"]."></td>
<td>" . $row["Play"] . "</td>
</tr>";
}
No need to do anything special. You are working from a base directory of /jukebox/, your images reside in /jukebox/img/. Let's make it work:
<img src=\"/jukebox/img/".$row['Albumcover']."\"/>
Within your loop. Now the album cover will appear as an image in that row.

How do I create a directory path on PHP

I have created a database on phpmyadmin via xampp. I'm currently trying to make a music player and I am having difficulty linking my music folder to my php coding. I don't know the right syntax or the location I should be working on either within my coding to implement the music player.
This is how my php translate currently and if you look at the Play column, it references the audio file's name
And this is where all my music is stored
Someone please help me, please..1
Here's my coding as well
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jukebox";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM Music";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Artist</th>
<th>Title</th>
<th>Album</th>
<th>Albumcover</th>
<th>Play</th>
</tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo
"<tr>
<td>" . $row["Artist"]. "</td>
<td>" . $row["Title"]. "</td>
<td>" . $row["Album"]. "</td>
<td><img src='/jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"]."></td>
<td>" . $row["Play"] . "></td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
?>
</body>
</html>
<td>
<img src='"<?php echo $_SERVER['DOCUMENT_ROOT']; ?>"'/jukebox/img/" . $row["Albumcover"] ."' alt=".$row["Albumcover"].">
</td>
try this one

I want to upload a XML file from a form page. After the upload, the values of the xml file had to be put in my array

Below you can see my formpage, at this page you upload a xml file.
<html>
<body>
<form enctype="multipart/form-data" action="arrayincludefiletest.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table width="600">
<tr>
<td>File name:</td>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
Below you can see my PHP code, if you take a look you can see that now everything is manual. I have to fill the array by myself. But I want that the array will be fill automatic by the values of the XML File. So if you upload a XML File the values of the file will automatically fill my array. Also you can see that I insert the array into my database.
<html>
<head>
<title> Bom Array </title>
</head>
<body>
<?php
$bom= array(
array("Aantal" =>1, "Manufactorer" =>"Panasonic", "Partno" =>"EEEFC1H1R0R",
"Description" =>"Capacitor 0603","Footprint" =>"CAP0603", "Refdes" =>"B1"),
array("Aantal" =>2, "Manufactorer" =>"Vishay", "Partno" =>"MAL215371228E3",
"Description" =>"Capacitor 1210","Footprint" =>"CAP1210", "Refdes" =>"C6,C7"),
array("Aantal" =>3, "Manufactorer" =>"Ferroxcube", "Partno" =>"MAL215371109E3",
"Description" =>"Buzzer 80dB 3,4 KHz","Footprint" =>"KPEG238", "Refdes" =>"C8,C25"),
array("Aantal" =>4, "Manufactorer" =>"Philips", "Partno" =>"EEEFC1E101P",
"Description" =>"Tantaal 100uF, 6,3V Case_B","Footprint" =>"Case_B", "Refdes" =>"C1")
);
echo "<table border='1'>";
echo "<tr>
<th>Aantal</th>
<th>Manufactorer</th>
<th>Partno</th>
<th>Description</th>
<th>Footprint</th>
<th>Refdes</th>
</tr>";
echo "<tr>
<td>" . $bom[0]['Aantal'] . "</td>
<td>" . $bom[0]['Manufactorer'] . "</td>
<td>" . $bom[0]['Partno'] . "</td>
<td>" . $bom[0]['Description'] . "</td>
<td>" . $bom[0]['Footprint'] . "</td>
<td>" . $bom[0]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[1]['Aantal'] . "</td>
<td>" . $bom[1]['Manufactorer'] . "</td>
<td>" . $bom[1]['Partno'] . "</td>
<td>" . $bom[1]['Description'] . "</td>
<td>" . $bom[1]['Footprint'] . "</td>
<td>" . $bom[1]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[2]['Aantal'] . "</td>
<td>" . $bom[2]['Manufactorer'] . "</td>
<td>" . $bom[2]['Partno'] . "</td>
<td>" . $bom[2]['Description'] . "</td>
<td>" . $bom[2]['Footprint'] . "</td>
<td>" . $bom[2]['Refdes'] . "</td>
</tr>";
echo "<tr>
<td>" . $bom[3]['Aantal'] . "</td>
<td>" . $bom[3]['Manufactorer'] . "</td>
<td>" . $bom[3]['Partno'] . "</td>
<td>" . $bom[3]['Description'] . "</td>
<td>" . $bom[3]['Footprint'] . "</td>
<td>" . $bom[3]['Refdes'] . "</td>
</tr>";
echo "</table>";
// Connectie database and Insert into database
$con = mysqli_connect("localhost", "csa", "csa", "csa");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL:" . mysqli_connect_error();
}
if(is_array($bom))
{
$sql= "INSERT INTO bom (Aantal, Manufactorer, Partno, Description, Footprint, Refdes) values";
$valuesArr = array();
foreach ($bom as $row)
{
$aantal = (int) $row['Aantal'];
$manufactorer = mysqli_real_escape_string($con, $row['Manufactorer']);
$partno = mysqli_real_escape_string($con, $row['Partno']);
$description = mysqli_real_escape_string($con, $row['Description']);
$footprint = mysqli_real_escape_string($con, $row['Footprint']);
$refdes = mysqli_real_escape_string($con, $row['Refdes']);
$valuesArr[] = "('$aantal', '$manufactorer', '$partno', '$description', '$footprint', '$refdes')";
}
$sql .= implode(',', $valuesArr);
mysqli_query($con, $sql) or die('Error:' . mysqli_errno($con));
}
mysqli_close($con);
?>
</body>
</html>
You can use the PHP SimpleXML extension to read XML files and convert them to objects. If you run PHP 5.1.2 or later, it is enabled by default.
The PHP manual provides some excellent examples.
Step 1: saving the XML file
The first thing to decide is whether you want to store the XML files. In that case, have a look at the "POST method upload" example (http://www.php.net/manual/en/features.file-upload.post-method.php). If you want to move the uploaded XML file to a directory named "xml" inside your current folder, it would look like this:
if(!empty($_FILES)) {
$uploaddir = dirname(__FILE__) . '/xml/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo 'The XML file has been successfully uploaded.';
} else {
echo 'Error occurred while saving the uploaded file.';
}
}
Step 2: reading the XML file
The next step is to load the XML file using SimpleXML and convert it to an object (see examples at http://www.php.net/manual/en/simplexml.examples-basic.php). First you need to define an XML structure for your data. For example:
<?xml version='1.0' standalone='yes'?>
<boms>
<bom>
<aantal>1</aantal>
<manufactorer>Panasonic</manufactorer>
<partno>EEEFC1H1R0R</partno>
<description>Capacitor 0603</description>
<footprint>CAP0603</footprint>
<refdes>B1</refdes>
</bom>
<bom>
<aantal>2</aantal>
<manufactorer>Vishay</manufactorer>
<partno>MAL215371228E3</partno>
<description>Capacitor 1210</description>
<footprint>CAP1210</footprint>
<refdes>C6,C7</refdes>
</bom>
</boms>
Then to read the XML file and loop through the rows to echo them or save them to the database:
if(isset($uploadfile) && file_exists($uploadfile)) {
$bom = simplexml_load_file($uploadfile);
if(!$bom) {
die('Unable to load XML file.');
}
foreach($bom as $row) {
echo "<tr>
<td>" . $row->aantal . "</td>
<td>" . $row->manufactorer . "</td>
<td>" . $row->partno . "</td>
<td>" . $row->description . "</td>
<td>" . $row->footprint . "</td>
<td>" . $row->refdes . "</td>
</tr>";
}
}
A few notes on these examples:
There is no validation as to whether the uploaded file actually is an XML file and if it has the structure you defined. Make sure you check that before parsing it.
The "xml" directory has to be writable by your webserver.
Hope this helps.

Categories