I am new to web dev, PHP, CSS and html. I want to put a CSS in my table displaying the data on my database, but it doesn't work.
Here is my code:
My CSS file is named "table.css" ...
<html>
<head>
<title>WEW</title>
<head>
<link href="table.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$con = mysql_connect("localhost","abc123","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_ME", $con);
$result = mysql_query("SELECT * FROM id");
$data->set_css_class("table");
echo "<table class="table">
<tr>
<th>id</th>
<th>password</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
</body>
</html>
I'm assuming the CSS file is well written, and that it uses the .table selector.
There's several syntax errors in there, all because you need to escape the inner " like so:
echo "A 'string with several \"nesting\" levels' needs escaping.";
echo "<table class="table">
change to
echo "<table class='table'>
AND
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
change to
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";
Related
I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X.
Here's my Php code
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and here's what i have done so far for HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php mysql_close($connector); ?>
</body>
</html>
little Help here please, Thanks !!
EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.
you not close while;
so change code to :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
/////////////////////////////////// change \\\
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db($database, $connector)
or die("Unable to connect");
/////////////////////////////////// end change \\\
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) :
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php endwhile;?>
<?php mysql_close($connector); ?>
</body>
</html>
Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.
Error code :
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname);
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
//here there is no variable like what you passing to connect mysql that's problem here
?>
solution :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.
You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.
<?php
$host = 'localhost';
$userName = 'dbuser';
$password = 'pw';
$nameOfDb = 'dbname';
$nameOfTable = 'tablea';
$pdo = new PDO('mysql:host='.$host, $userName, $password);
$pdo->query("use " . $nameOfDb);
// desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
// - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
//$query = $pdo->prepare('select * from '.$nameOfTable);
$query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
</tr>
</thead>
<tbody><?php
forEach($rows as $curRow)
{
echo '<tr>';
// use this one if you want to display the columns in the same order they are returned in the query results
// AND you'd like to display all columns in the result
forEach($curRow as $curCell)
echo '<td>'.$curCell.'</td>';
// otherwise, you can request the desired elements by name
//
// echo '<td>' . $curCell['id'] . '</td>'
// echo '<td>' . $curCell['Subject_Code'] . '</td>'
echo '</tr>';
}
?></tbody>
</table>
</body>
Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder
I'm having hard time to figure it out how to insert the checked box-es or unchecked into the database with php.
I tried many many different ways but none is working, I think I'm very close but can't figure it out the problem.
Btw I work with Javascript and never worked with PHP except this time.
index.html
<html>
<head>
<style type="text/css">
#import "demo_page.css";
#import "header.ccss";
#import "demo_table.css";
#import "select.dataTables.min.css";
#import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $case;
$result = mysqli_query($con,$sql);
}
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
?>
</body>
</html>
Any help would be appreciated.
image:
EDIT:
Solved - here is the code:
<?php
if(isset($_POST['save'])){
echo "<script>console.log(" . $checkboxes.length . ");</script>";
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,DB_NAME);
$rows = $_POST['case'];
foreach($rows as $row){
$sql = "UPDATE url SET checked = 1 WHERE number = " . $row;
$result = mysqli_query($con,$sql);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#import "demo_page.css";
#import "header.ccss";
#import "demo_table.css";
#import "select.dataTables.min.css";
#import "jquery.dataTables.min.css";
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="RowGroupingWithFixedColumn.js"></script>
<script>$(document).ready(function(){load_(); console.log('load running')});</script>
</head>
<body id="dt_example">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="endpoints">
<thead>
<tr>
<th></th>
<th>Nr.</th>
<th>Java Class Name</th>
<th>http Method</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<?php
$con = mysqli_connect('sql7.freemysqlhosting.net','sql7117068','GZqaZj69G9','sql7117068');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,'sql7117068');
$sql='SELECT * FROM url';
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
print $row['method'];
switch ($row['http_method']) {
case "GET":
echo "<tr class='gradeA'>";
break;
case "PUT":
echo "<tr class='gradeC'>";
break;
case "POST":
echo "<tr class='gradeU'>";
break;
case "DELETE":
echo "<tr class='gradeX'>";
break;
default:
echo "<tr>";
}
if($row['checked']){
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "' checked> </td>";
} else {
echo "<td><input type='checkbox' id=case name='case[]' value='" . $row['number'] . "'> </td>";
}
echo "<td align=center >" . $row['number'] . "</td>";
echo "<td align=center >" . $row['class_name'] . "</td>";
echo "<td>" . $row['http_method'] . "</td>";
echo "<td style='font-weight:bold'>" . $row['endpoint'] . "</td>";
echo "</tr>";
}
mysqli_close($con);
echo "</tbody></table>";
echo "<input type='submit' name='save' id='save' value='Save' />";
echo "</form>";
?>
</body>
</html>
PS: There may be many leaks and bad programming style in the code, but know that I'm not a PHP developer and it doesn't matter if there can be different attacks on my server. I only wanted to solve the problem and move on. Don't have time to stop at every point.
Use code below to check if a check box is checked or not. Then set some flags on some variables to insert data to to db accordingly.
//html
<input type='checkbox' name='boxname' value='1' />
//php
<?php
if(isset($_POST['boxname'])
{
echo "check box is checked";
}
?>
So it's actually hard to explain what I mean.
So I have this list here. Its pulls data from the db and then displays it in a DIV on my webpage.
I want it to only show the first 4 listing. Then after 4 seconds. I want it to show the next 4 listening and keep revolving like that infinitely. If someone could point me in the direction to be able to do that. Also, It needs to be updating dynamically every 4 seconds. Not when the user hits refresh.
<?php session_start(); ?>
<head>
<link rel="stylesheet" type="text/css" href="tablecss.css">
<style type="text/css">
.extra {
position: relative;
top: 50px;
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<?php
$con = mysqli_connect('##', '##', '##', '##');
if(mysqli_connect_errno()) {
echo 'Failed to Connect to MySQL' . mysqli_connect_errno();
}
$user = $_SESSION['username'];
$result = mysqli_query($con, "SELECT * FROM corporations");
echo "<table id='tb'border='1'>
<tr>
<th>Name</th>
<th>Owner</th>
<th>Last Price</th>
<th>Volume</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name']. "</td>";
echo "<br>";
echo "<td>" . $row['owner'] . "</td>";
echo "<br>";
echo "<td>" .'$'. $row['shareValue'] . "</td>";
echo "<br>";
echo "<td>" . $row['publicShares'] . "</td>";
echo "<br>";
}
mysqli_close($con);
?>
</div>
</body>
What you can do is javascript, and use ajax. to load it every four seconds you can use setInterval(http://www.w3schools.com/jsref/met_win_setinterval.asp), but for me its better if you use jQuery load more data on scroll.
to limit output use the limit of mysql.
see this article: http://www.mysqltutorial.org/mysql-limit.aspx
I've a php page with embedded HTML and I'm displaying data from a MySQL database. PHP is echoing the html inside the php page. All of the data is being returned; however, the data table is being displayed with an extra column and the data that should be in the last column is displayed in the extra column (e.g. my last name is 'Last Name,' but there is an extra column after 'Last Name' with the 'last name' data).
What am I doing wrong here?
Thanks.
get_records.php
//make connection
$conn = mysql_connect('localhost', 'root', '');
//select db
mysql_select_db('kis');
if (!$conn) {
die("Can not connect: " . mysql_error());
}
//select db and run query
mysql_select_db('kis');
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/TableCSSCode.css" media="all"/>
<title>Volunteer Data</title>
</head>
<body>
<div class="CSSTableGenerator">
<h1>Volunteer Records</h1>
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
echo "</tr>";
}//end while
?>
</tr>
</table>
</div>
<!--end #dr_container-->
</body>
</html>
You need to close the td's
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
You have not properly closed your td tags:
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
It should be </td> at the end.
You're echoing the row tags (<tr>). So, don't add additional ones in the plain html (just around your PHP code).
Close "td" tag.
Remove "tr" tags before and after where php code start because there are already tr tags inside the php code.
Following is the updated HTML of table:
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
echo "</tr>";
}//end while
?>
I want to have a table that contains words and their meaning from database, in another column i want to have checkbox for each row, that user will check them and show what words he/she know.
i have two question in this case:
1- how can i hide the meaning in the first and after clicking in show meaning visible them?
2- how can i set checkboxes?
i have this code until now but it doesn't work
please help me if you can
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').item(0).style.visiblility = 'visible';
}
</script>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='visibility:hidden'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
getElementByClassName is an inexistant function. You mean getElementsByClassName, which will however return a list of the elements, so you need to select one.
document.getElementsByClassName('hiding').item(0).style.visibility = 'visible';
For hide a suggestion:
echo "class='hiding' style='display:none'>" . $row['meaninig'];
To show the meaning:
//for Jquery
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
Or
//for plain old javascript
<script type="text/javascript">
function ShowMeanings(){
document.getElementsByClassName('hiding').style.visibility = 'visible';
}
</script>
Your code Edited:
<html><head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function ShowMeanings(){
$('.hiding').shoW();
}
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost", "root", "")
or die(mysql_error());
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_select_db("project", $con)
or die(mysql_error());
$result = mysql_query("select * from words");
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>check</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaninig'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input";
echo "type= 'checkbox' name = 'checkbox' id = 'checkbox' value = '' />";
echo "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
</div>
<button onclick="ShowMeanings()">showmeaning</button>
</body>