I have the following php code, which gathers data from a database, and displays it in a table. It works fine when used in chrome, But is not working properly, i.e, the border, rows etc.. are not being displayed, like the table tag itself is being disregarded. Here is the code, I have.
<html>
<head>
<title> Reviews List. </title>
</head>
<body>
<?php
include 'settings.php';
if (!isset($dbc)){
$dbc = new mysqli(DB_HOST , DB_USER , DB_PASSWORD , DB_NAME) or die("Cannot connect to the database.");
}
$query = "SELECT * FROM reviews";
$result = $dbc->query($query) or die ("Cannot query");
?>
<table id="review_table" border="1" width="1">
<?php
while ($row = mysqli_fetch_assoc($result)){
echo '<tr>';
echo '<td>';
echo $row['id'];
echo '</td>';
echo '<td>';
echo $row['pid'];
echo '</td>';
echo '<td>';
echo $row['publish'];
echo '</td>';
echo '</tr>';
}
?>
</table>
</body>
Here are the screen shots of the page viewed in safari and in chrome.
Safari:
I did try to click on the toggle formatting button, which did nothing.
Chrome:
Remove border and width from table tag and add this css instead.
#review_table {
border: 1px solid #000;
border-collapse: collapse;
}
#review_table td {
border: 1px solid #000;
}
You are missing the <tbody> tag
Related
I am just starting with SQL, PHP, and HTML and I plan to take some courses, but I need a basic working model before I can move forward. I have an SQL database that has a few fields. I can display this data without a problem. I would like to filter this data with a drop-down box. Currently, I have the drop-down box populated with a Select Distinct command. I can not seem to figure out the syntax to modify my SQL Select command to only show records that match my pull-down box. Ha, I will be the first to admit that I have no clue what I am doing, but my problem is that I might not know the term I need to look up how to solve my problem. Google and I are tight, but we just can't put our finger on this one. Can you point me in the right direction? Thanks!
<!DOCTYPE html>
<head>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even){
background-color: #dddddd;
}
</style>
<body>
<form action='new1.php' method="get" name='fPIC'>
<select class="form-dropdown validate[required]" style="width:150px" id="input_PIC" name="sPIC">
<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");
mysqli_select_db($link,'pd4poc');
$query = "SELECT DISTINCT PIC FROM dummydata order by PIC asc";
$result = mysqli_query($link,$query);
$menu=" ";
while($row = mysqli_fetch_array($result))
{
$menu .= "<option value=".$row['PIC'].">" .$row['PIC']. "</option>";
}
echo $menu;
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" . $row['PIC'] . "<td><td>" , $row['Reason'] . "<td><td>", $row['Status'] . "</td></tr>";
}
echo "</table>";
mysqli_close($link);
?>
</select>
<input type="submit" name="nPIC" value="Filter">
</form>
<?php
$link = mysqli_connect("localhost", "root", "**********", "pd4poc");
mysqli_select_db($link,'pd4poc');
$query = "SELECT * FROM dummydata"; //need to be picked from the pull down
$result = mysqli_query($link,$query);
echo "<table>";
echo "<tr>
<th>PIC</th>
<th>Reason</th>
<th>PlanApply</th>
<th>HOT</th>
<th>Status</th>
<th>Comments</th>
</tr>\n";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>" .
$row['PIC'] . "<td>",
$row['Reason'] . "<td>",
$row['PlanApply'] . "<td>",
$row['HOT'] . "<td>",
$row['Status'] . "<td>",
$row['Comments'] . "</td>\n</tr>";
}
echo "</table>";
mysqli_close($link);
?>
</body>
</head>
this is my first question here.
I am trying to generate pdf from a particular row of my database using mpdf. I want the code to download a particular row's data when needed. A download link is beside every row. When the download button is pressed it will download that row's data.
The code is working but it is fetching all the values from the db and assigning the values side by side.
Here is my generate pdf php
<?php
//==============================================================
//==============================================================
//==============================================================
include("mpdf/mpdf.php");
include "config.php";
$res = mysql_query("select date1, date2 from test");
if (!$res)
die("query error : ".mysql_error());
$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first
level of a list
// LOAD a stylesheet
$stylesheet = file_get_contents('mpdfstyletables.css');
$mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is
css/style only and no body/html/text
$html = '
<center><h3>Test</h3></center>
<center>
<table border="1">
<tr>
<th>Date 1</th><th>Date 2</th>
</tr>
<tr>';
while($row = mysql_fetch_array($res)){
$html .=
'<td>'.$row['date1'].'</td>
<td>' . $row['date2']. '</td>';
}
$html .= '
</tr>
</table></center>
';
$mpdf->WriteHTML($html,2);
$mpdf->Output('mpdf.pdf','I');
exit;
//==============================================================
//==============================================================
//==============================================================
?>
And Here is the view.php part for viewing the database
<html>
<body>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
text-align: center;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
textarea
{
width:100%;
}
.textwrapper
{
border:1px solid #dddddd;
margin:5px 0;
padding:1px;
}
</style>
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('test') or die(mysql_error());
$query=mysql_query("select * from test limit 0,10") or die(mysql_error());
echo'<table border="1" ><th >Id</th><th>Date 1</th><th>Date 2</th>';
while($res=mysql_fetch_array($query))
{
echo'<tr><td>'.$res['id'].'</td><td>'.$res['date1'].'</td>
<td>'.$res['date2'].'</td>
<td><a href="gen.php?id=<?php echo $row["id"]; ?> Downlaod</a></td>
</tr>';
}
echo'</table>';
?>
</body>
</html>
Screenshot of my view.php
In your view page,
There is no array called $row. Rather $res holds your query's resultset.
Use this instead:
while($res=mysql_fetch_array($query)){
echo"<tr>";
echo "<td>$res['id']</td><td>$res['date1']</td>";
echo "<td>$res['date2']</td>";
echo "<td>Download</td>";
echo "</tr>";
}
In you pdf generating code, change:
$res = mysql_query("select date1, date2 from test");
To:
$res = mysql_query("SELECT date1,date2 FROM test WHERE id=".intval($_GET['id']));
Please, please, please upgrade your mysql functions to mysqli_ immediately.
I recommend using a prepared statement to protect your query from malicious injection. In the meantime, you can use intval() as a lazy/temporary fix.
I insert MySQL db result into HTML table, after that I am trying to change row colors using two colors,
for example if 1st row has red 2nd row has yellow again 3rd row has
red like wise..
what is the best possible way to do that, I wrote PHP code using PHP modulus function, is there any easist way to do that thank you..
<?php
$result = mysqli_query($link, "SELECT * FROM example");
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($i % 2 == 0) {
$bgColor = ' style="background-color:#CCFFFF;" ';
} else {
$bgColor = ' style="background-color:#FFFF99;" ';
}
echo "<tr>";
echo "<td $bgColor>";
echo $row['name'];
echo "</td>";
echo "<td $bgColor>";
echo $row['age'];
echo "</td>";
echo "</tr>";
$i++;
}
?>
</table>
It can be done via CSS
tr:nth-child(even) {background: yellow}
tr:nth-child(odd) {background: red}
Source: http://www.w3.org/Style/Examples/007/evenodd.en.html
include css
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
replace table tag with
<table class="table table-striped" >
Instead of inline styles you should use classes .red and .yellow.
<?php
$result = mysqli_query($link, 'SELECT * FROM example');
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr class="' . (($i % 2 == 0) ? 'red' : 'yellow') . '">';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo $row['age'];
echo '</td>';
echo '</tr>';
$i++;
}
?>
</table>
You able to make it not using PHP at all.
Just use CSS with :nth-child pseudo-class
tr:nth-child(2n) {
background: #f0f0f0; /* row background color */
}
tr:nth-child(1) {
background: #666; /* row background color */
color: #fff; /* text color */
}
By the way, It's very bad way to show data in interface to mixing process of getting it from DB and showing it in view (putting to output buffer). This code looks like as an traning example fragment from old ugly bloody book for PHP3. It's reqired to separate 2 process: first getting ALL data from DB, and after that puting it into output with appearance.
I don't understand why this isn't working, I have been stuck on this for ages and tried lots of different alternatives, but it just doesn't print the data from the database.
At the moment I am just trying to get the id to print, but eventually I want to print most of the data in the database (not including the hash).
Here is my code:
<!DOCTYPE html>
<html>
<head>
<title>Staroids Leaderboard</title>
</head>
<body>
<table border=1px>
<thead>
<tr>
<td>name</td>
<td>score</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "password");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("staroids");
$results = mysql_query("SELECT id FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['id']
?>
<tr>
<td><?php echo '$name'?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
The image below shows what it looks like in html:
This image shows the database in local host and as you can see there is lots of data, but none of the names seem to print?!
Correct your syntax where it could be
$name = $row['id']; //Put ; here
<?php echo $name;?> //Remove quotes and put ;
Select name from DB and you can get name then.It should be
$results = mysql_query("SELECT id,name FROM scores");
while($row = mysql_fetch_array($results)) {
$name = $row['name'];
?>
<td><?php echo $name;?></td>
And dont use mysql_* functions due to they are deprecated.Instead use mysqli_* functions or PDO statements.
And as #Nedstark said use try die(mysql_error()); for the errors regarding the mysql errors.
<td><?php echo $name;?></td>
or use
<td><?php echo "$name";?></td> <!--(Bad idea but works)->
Variables work in double quotes("") not in single quotes('')
<?php
session_start();
if (!(isset($_SESSION['UserName'])))
{
echo "<script type=\"text/javascript\">alert('Unauthorize user are redirected to Login page');".
header('Location:http://localhost/campus');
}
include_once "connect.php";
$find = mysql_query("YOUR SELECT STATEMENT ") or die('error');
ob_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ST. MICHAEL's COLLEGE ILIGAN CITY</title>
<style>
AlignJst {
text-align:justify;
text-justify:inter-word;
}
pTable {
margin:2cm 4cm 3cm 4cm;
}
body {color: black; font-size: 10px; font-family: Helvetica, Arial, non-serif;}
a:link {color: #FF8C00;}
a:visited {color: #FF8C00;}
a:hover {color: #FF8C00; background: #ADD8E6; text-decoration:none;}
a:active {color: #FF0000;}
p {line-height: 2em;
font-size:85%;
color:black;
letter-spacing: 0.3em
}
h1 {
font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;
font-size: 12pt;
color: navy;
padding-top: 12px;
padding-bottom: 3px;
}
</style>
</head>
<body>
<?php
echo "<CENTER>"."<H1>TABLE TITLE</H>" . "<BR />";
echo "<H1>SUBTITLE</H>"."</CENTER>"."<BR/>";
echo "<CENTER>"."<p>"."<b>" . "PAST MORNING PRAYER SCHEDULE" ."</b>"."</p>"."</CENTER>"."<BR/>";
echo "<table border='1' width='100%' align ='center'>";
echo "<tr>";
echo "<th>SPONSOR NAME</th>";
echo "<th>VENUE </th>";
echo "<th>DATE EVENT</th>";
echo "<th>TIME </th>";
while($row = mysql_fetch_array($find)){
echo "<tr>";
echo "<td>".$row['sponsor_name']."</td>";
echo "<td>".$row['Venue']."</td>";
echo "<td>".$row['Date_Event']."</td>";
echo "<td>".$row['Time_Event']."</td>";
echo "</tr>";
}
echo "</table>";
echo "<br />". "<br />" ."<br />";
echo "<p align = 'right'>"."Prepared By:" . $_SESSION['UserName'] ."</p>";
?>
</body>
Other than changing mysql ==> msqli, I'd recommend a couple of debug strategies, in this case I would:
fix the echo, you need to pick one of those two options:
<?php echo $variable; ?>
<?php echo "this is my variable {$variable}"; ?>
If you put a single quote PHP don't parse the content of what is about to print, it just print it as text, so what you have should print $name in the HTML...but, since I don't see any $name text in the black screenshot I think you might even not get into that loop...
a good debug strategy would be to query for something broader, i.e. "SELECT * FROM `scores`", then you can do a
<?php print_r($row); ?>
right after while ($row = mysql_fetch_array($results)) {
Had this all working with html. Then I tried making it into html5 and I'm coding some of my first css code. The only thing I'm tackling right now is why isn't my image showing?? I'm using google chrome btw. The code passed in the url is this: "?fname=raichu&yesorno=true%2F" And there is no image tag in my generated html :/ I'm assuming that the if statement is equating to false??
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
td{
text-align: center;
padding:15px;
background-color:black;
color:#00FF00;}
th{
background-color:black;
color:yellow}
</style>
<title>Search Results</title>
</head>
<body style="color:#FFFFFF">
<?php
$dbhost = 'server';
$dbname = 'database1';
$dbuser = 'me';
$dbpass = 'password';
$link = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
mysqli_select_db($link,$dbname);
$naame = $_GET["fname"];
if( $_GET["yesorno"] == 'true' OR !$_GET["yesorno"])
{$query = sprintf("SELECT image_url, Type
FROM Pokemon c
WHERE c.name='%s'", mysqli_real_escape_string($link,$naame));
$result = mysqli_fetch_assoc(mysqli_query($link,$query));
echo '<img height="450" width="330" src="'.$result['image_url'].'" alt="blue"/>';}
$res = mysqli_query($link,"SELECT Name,HP,Type,Pokedex_Number AS 'Pokedex Number',Weakness,Resistance,Retreat AS 'Retreat Cost'
FROM Pokemon
WHERE Pokedex_Number!=0 AND name='$naame'");
if (!$res) {
die("Query to show fields from table failed");}
$fields_num = mysqli_num_fields($res);
echo "<h1>Stats</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{$field = mysqli_fetch_field($res);
echo "<th>{$field->name}</th>";}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($res))
{
echo "<tr>";
// $row is array... foreach( .. ) puts every element
// of $row to $cell variable
foreach($row as $cell)
echo "<td>$cell</td>";
echo "</tr>\n";
}
echo "</table>";
mysqli_close($link);
?>
<br />
<form method="link"
action = "http://engr.oregonstate.edu/~bainro/welcome.php" ><input
type="submit" value="(>O.O)>RETURN<(O.O<)"></form>
<p></p>
</body>
</html>
You have
&yesorno=true%2F
therefore $_GET['yesorno'] will equate to 'true/' as %2f is a forward slash.
This doesn't match
if( $_GET["yesorno"] == 'true' OR !$_GET["yesorno"])
So you're correct - that line is failed so you won't get the image.
Solution: remove the %2F from the query.
The variable you are retrieving from the url, is never evaluating to true because '%2F' is a forward slash. Check your code.