I'm quite new to php and sql and was wondering how i style the data correctly, so that it is displayed in a table rather than block text (see picture to see what i mean)
This is my php code that i have used.
$dbhandle = mysql_connect($servername, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
//select a database to work with
$selected = mysql_select_db("isongs",$dbhandle)
or die("Could not select examples");
$result = mysql_query("SELECT tbl_artists.artistname, tbl_songs.songtitle, tbl_songs.yearrelease, tbl_artists.genre, tbl_songs.price
FROM tbl_artists INNER JOIN tbl_songs ON tbl_artists.artistID = tbl_songs.artistID;");
if(!$result) { die(mysql_error()); }
//fetch the data from the database
while ($row = mysql_fetch_array($result)) {
//echo "Song:".$row{'songtitle'}." Year Released:".$row{'yearrelease'}."Price:".$row{'price'}."<br>";*/
echo "Artist:". $row[0]. " Song:". $row[1]. " Year Released:". $row[2] . " Genre:". $row[3] . " Price:". $row[4] . "<br>"; //display the results
}
//close the connection
mysql_close($dbhandle);
?>
Thank you.
PHP can't style your data. All it can do is retrieve the data from your database and put it on your webpage. What you need to do is style that data using HTML and CSS.
For example:
<div>
<p>Artist: <?php echo $row[0]; ?></p>
<p>Song: <?php echo $row[1]; ?></p>
<p>Year Released: <?php echo $row[2]; ?></p>
<p>Genre: <?php echo $row[3]; ?></p>
<p>Price: <?php echo $row[4]; ?></p>
</div>
Related
I’ve created a PHP file that creates dropdown list from a MySQL table. When selected I want each entry in the dropdown list, to change an image shown on the page. I can display the last image in the dropdown list, but cant get it to change thereafter.
Do I need to do displaying image part with javascript??
Any help greatly received (with code sample if possible)
Regards,
Iain
<script language=”Javascript”>
function setImage(select){
var image = document.getElementsByName("image-swap")[0];
image.src = select.options[select.selectedIndex].value;
}
</script>
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Select all data:
echo "All Tartans:<br><br>";
$sql = "SELECT ID, TartanID, Clan, Variation, Manufacturer, Supplement, SupplementVAT, ImagePath FROM Tartan";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row into dropdown list
?>
<select name="Tartan" id="Tartan" onchange="setImage(this);")
<?php
while($row = $result->fetch_assoc())
{
echo "ID: " . $row["ID"]. "<br>" .
"TartanID: " . $row["TartanID"]. "<br>" .
"Clan: " . $row["Clan"]. "<br>" .
"Variation: " . $row["Variation"]. "<br>" .
"Manufacturer: " . $row["Manufacturer"]. "<br>" .
"Supplement: " . $row["Supplement"]. "<br>" .
"SupplementVAT: " . $row["SupplementVAT"]. "<br>" .
"ImagePath: " . $row["ImagePath"]. "<br>";
$clan = $row["Clan"];
$img_path = $row["ImagePath"];
echo "<option value='$Clan' >$clan</option>";
}
?>
</select>
<!-- Display Image here -->
<div class="img-block">
<img src="<?php echo $img_path; ?>"
name="image-swap"
alt="<?php echo $clan; ?>"
title="<?php echo $clan; ?>"
width="200"
height="200" class="img-responsive" />
</div>
<?php
}
else
{
echo "0 results";
}
echo "<br>";
$conn->close();
?>
I've been trying to find an easy way for this. A search (dropdown menu) of all tables in mysql, and show their content when I click the table I want to show on the page. Instead of showing just every table on the page I thought it can be easier? Any help would be appreciated! My code so far:
<?php
$host = "localhost";
$user = "heijsdb_user";
$pass = "maus";
$db_name = "heijsdb";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
echo "borsten HFP controle";
$result = mysqli_query($connection,"SELECT * FROM borstenHFPcontrole");
$all_property = array(); //declare an array for saving property
//showing property
echo '<table class="data-table w3-table-all" border="2px">
<tr class="data-heading">'; //initialize table tag
while ($property = mysqli_fetch_field($result)) {
echo '<td>' . $property->name . '</td>'; //get field name for header
array_push($all_property, $property->name); //save those to array
}
echo '</tr>'; //end tr tag
//showing all data
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
foreach ($all_property as $item) {
echo '<td>' . $row[$item] . '</td>'; //get items using property value
}
echo '</tr>';
}
echo "</table>";
////////////////////////////////////////////////////////////////////////////////////////////////////////////
This is pretty much the idea, you can play from here and adapt it to your solution. Sorry I used my way, I prefer PHP template style when embedding in HTML. ;)
$host = "localhost";
$user = "heijsdb_user";
$pass = "maus";
$db_name = "heijsdb";
//create connection
$connection = mysqli_connect($host, $user, $pass, $db_name);
//test if connection failed
if(mysqli_connect_errno()){
die("connection failed: "
. mysqli_connect_error()
. " (" . mysqli_connect_errno()
. ")");
}
//check if the form was submitted
$table = filter_input(INPUT_POST, 'table', FILTER_SANITIZE_STRING);
?>
<html>
<head>
<title>showing table content on user action</title>
</head>
<body>
<div>
<form id="form-menu" method="post">
<label for="select-menu">Choose a table</label>
<select id="select-menu" name="table">
<option></option>
<?php
$result = mysqli_query($connection,"SELECT table_name FROM information_schema.tables where table_schema='test'"); // <-- the table_schema field here is your database name, change 'test' for yours
while ($row = mysqli_fetch_array($result)) : $selected = $row['table_name'] == $table ? 'selected' : ''; ?>
<option value="<?php echo $row['table_name'] ; ?>" <?php echo $selected; ?>><?php echo $row['table_name'] ; ?></option>
<?php endwhile; ?>
</select>
</form>
<hr>
<div>
<?php if (empty($table)) : ?>
<h3>Please select a table to show its content</h3>
<?php else : ?>
<h3>Content for the table `<?php echo $table; ?>`</h3>
<?php
$result = mysqli_query($connection,"SELECT * FROM `{$table}`");
$all_property = []; //declare an array for saving property
?>
<!-- showing property -->
<table class="data-table w3-table-all" border="2px">
<tr class="data-heading"> <!-- initialize table tag -->
<?php while ($property = mysqli_fetch_field($result)) : ?>
<td><?php echo $property->name; ?></td> <!-- get field name for header -->
<?php $all_property[] = $property->name; //save those to array ?>
<?php endwhile; ?>
</tr> <!-- end tr tag -->
<!-- showing all data -->
<?php while ($row = mysqli_fetch_array($result)) : ?>
<tr>
<?php foreach ($all_property as $item) : ?>
<td><?php echo $row[$item]; ?></td> <!-- get items using property value -->
<?php endforeach; ?>
</tr>
<?php endwhile; ?>
</table>
<?php endif; ?>
</div>
</div>
<script>
document.getElementById('select-menu').addEventListener('change', function() {
document.getElementById('form-menu').submit();
});
</script>
</body>
</html>
Handy links:
- Get table names using SELECT statement in MySQL
- Examples of how to do query, style, dom, ajax, event etc like jQuery with plain javascript.
Hope this helps :)
I want to know where to put the condition to display data in a table after I select the value from a dropdown list.
Both have the same id (dropdown and table).
php table
<html>
<head>
</head>
<body>
<?php
$con=mysqli_connect("localhost","root","root","company");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT employees.id,employees.jobs FROM employees WHERE employees.jobs in ("programmer","hr","qa")";
if ($result=mysqli_query($con,$sql)){
?>
<label for="y">Select the job:</label>
<select name="loads" id="loads" onchange="">
<?php while($ri = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $ri['id'];?>" > <?php echo $ri['jobs']; ?> </option>
<?php
}
}
?>
</select>
<table class="striped" border="1" align="center" id="demo">
<tr class="header">
<td align="center"><b>Name</b></td>
</tr>
<?php
$con=mysqli_connect("localhost","root","root","company");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql2="SELECT employees.id,employees.name FROM employees WHERE employees.jobs in ("programmer","hr","qa")";
if ($result=mysqli_query($con,$sql2)){
// Fetch one and one row
while ($row=mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row["name"] . " " . "</td>";
echo "</tr>";
}
}
mysqli_close($con);
?>
</table>
</body>
</html>
If you mean: you want to something happens to your table when an item get selected in dropdown list(select tag). then it is not possible through php because php codes compiled once after each load on a page and it doesnt work live!
so you have to use JQUERY and AJAX to do that.
if this is what you searching for, reply me so i can help you.
by the way you dont need to connect 2 times into database and run the same query, i just edited your code a little bit:
<html>
<head>
<title></title>
</head>
<body>
<?php $con = mysqli_connect("localhost","root","root","company");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT * FROM employees WHERE employees.jobs in ("programmer","hr","qa")";
$result = mysqli_query($con, $sql);
if ($result) { ?>
<label for="y">Select the job:</label>
<select name="loads" id="loads" onchange="">
<?php while($ri = mysqli_fetch_array($result)) { ?>
<option value="<?php echo $ri['id'];?>" > <?php echo $ri['jobs']; ?> </option>
<?php
}
}
?>
</select>
<table class="striped" border="1" align="center" id="demo">
<tr class="header">
<td align="center"><b>Name</b></td>
</tr>
<?php
// Fetch one and one row
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["name"] . " " . "</td>";
echo "</tr>";
}
mysqli_close($con);
?>
</table>
</body>
</html>
I've written up a pretty simple query to output all the data from the Artist table and output them in the established tables. I've double checked the database and all the spelling is correct, but I'm not getting any data being outputted for some reason.
Connector Code
<?php
$conn = mysqli_connect("localhost", "b4014107", "Windows1", "b4014107_db2") or die (mysqli_connect_error());
?>
Main Code
!DOCTYPE HTML>
<html>
<head>
<title>View Artist Table</title>
</head>
<body>
<?php
//Includes speicifed details in order to connect to MySQL
include('ConnectorCode.php');
//mysql_query command is used to select data from Artist table
$result = mysqli_query("SELECT * FROM tbl_Artist");
echo "<table border='1'>";
echo "<tr> <th>Artist ID</th> <th>Artist Name</th> </tr>";
//Results are looped and then displayed in tables
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row ['Artist_id'] . "</td>";
echo "<td>" . $row ['Artist_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
//Connection is closed
mysqli_close($conn);
?>
<p>Add a new Artist</p>
<p>Edit a current Artist</p>
</body>
</html>
What am I doing wrong?
I think this is your problem:
Use: $result->fetch_assoc()
Instead of: mysqli_fetch_array($result)
I found the solution! I just need to add $conn within the mysqli_query.
$result = mysqli_query($conn, "SELECT * FROM tbl_Artist");
It is showing that connection has been created. But I don't know what is the problem in query. After that query I tried to echo something and it is visible there in browser.
<html>
<head>
</head>
<body>
<?php
$servername = "localhost:8888";
$username = "root";
$password = "";
$dbname = "employees";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else {
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['emp_no'] ?></td>
<td><?php echo $row['dept_no']?></td>
<td><?php echo $row['from_date'] ?></td>
<td><?php echo $row['to_date'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
$sql = "select * from dept_manager";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
// your code here
}
Try with this..
Try this in the place of while loop:
<?php
$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
echo "<table>";
while($row=mysqli_fetch_assoc($res))
{
echo "<tr><td>" . $row['emp_no'] . "</td><td>" . $row['dept_no'] . "</td><td>" . $row['from_date'] . "</td><td>" . $row['to_date'] . "</td></tr>";
}
echo "</table>";
?>
You have mentioned servername:localhost:8888 just check once remove
8888 and try.
I have tried with your code in my local system,same code working fine.
Just i have entered my database name and table name,it's working fine.
So you just follow my example and test once.
COde:-
<html>
<head>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydashboard";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else{
echo "Connected successfully";
}
$res = mysqli_query($conn,"select * from content_values");
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
<tr>
<td><?php echo $row['name'] ?></td>
</tr>
</table>
<?php
}
?>
</body>
</html>
Output:-
Connected successfully
Name:
Test QA AndroidSD
image sampl
GMAIL
test PDF
Nat Geo Video