i'm trying to do a table with php and mysqli and i want to see the results in the html. For some reason it is not working
Thanks in advance.
Here is my code:
<?php
require 'connect.php';
$link->query("SELECT nome, id_utilizador FROM utilizador");
echo "NSA INFORMATION";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>NAME</td>";
while ($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row["nome"] . "</td>";
echo "<td>" . $row["id_utilizador"] . "</td>";
echo "</tr>";
}
?>
echo "</table>"; after the while loop. In your code outputted html is not valid as table element has no required closing tag. You also forgot the closing tag for tr before the while loop and close the opening tag for table.
Related
I am trying to show a table as per the screen shot below.
Screen Shot
The goal is to have the 'folder' image as a clickable link getting the link data from the MySql table.
However, when i try to do this (step by step) the hyperlinks sit outside the table. When i add tags around the following
echo ''.$row['file'].'' ;
like this
echo "<td>" ''.$row['file'].'' "</td>";
The subsequent PHP page will not load
// Attempt select query execution
$sql = "SELECT * FROM versioncontrol";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>Image Link</th>";
echo "<th>Manual Link File</th>";
echo "<th>Operating Procedure ID</th>";
echo "<th>Operating Procedure Name</th>";
echo "<th>Operating Procedure Version</th>";
echo "<th>Upload Date and Time</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>
<a href=\"sopversioncontrolKL001.php" . $record['images'] . "\" >
<img src=\"images/document.jpg" . $record['images'] . "\" height=\"30\"
/></a></td>";
echo ''.$row['file'].'' ;
echo "<td>" . $row['SOP_ID'] . "</td>";
echo "<td>" . $row['SOP_Name'] . "</td>";
echo "<td>" . $row['SOP_Version'] . "</td>";
echo "<td>" . $row['reg_date'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
echo "<td>" ''.$row['file'].'' "</td>";
The subsequent PHP page will not load
Well, it wouldn't. That's a syntax error.
After echo "<td>" you need to have either a ; or an operator.
You can't just run straight into another string literal.
Put the <td> and </td> inside the string literals you already have.
echo '<td>'.$row['file'].'</td>';
Better yet, get rid of the enormous pile of echo statements and just output HTML.
?>
...
<td><?php echo $row['file']; ?></td>
...
<?php
(You should probably make use of htmlspecialchars to protect yourself against stored XSS attacks too).
I need to fetch data from PHP MyAdmin database to WordPress website page. Since I am new to WordPress I am not too sure which method I have to follow. I tried to edit the source code of the page by adding the new PHP code inside PHP tags. But it did not work. And I found a file named 'functions.php' in the local directory where my project located. Is that the file I need to use this code? I've already written the code and I just need to know where to use it.
<?php
$results = $wpdb->get_results( "SELECT * FROM $table_name"); // Query to fetch data from database table and storing in $results
if(!empty($results)) // Checking if $results have some values or not
{
echo "<table width='100%' border='0'>"; // Adding <table> and <tbody> tag outside foreach loop so that it wont create again and again
echo "<tbody>";
foreach($results as $row){
$userip = $row->user_ip; //putting the user_ip field value in variable to use it later in update query
echo "<tr>"; // Adding rows of table inside foreach loop
echo "<th>ID</th>" . "<td>" . $row->id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>User IP</th>" . "<td>" . $row->user_ip . "</td>"; //fetching data from user_ip field
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Post ID</th>" . "<td>" . $row->post_id . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
echo "<tr>";
echo "<th>Time</th>" . "<td>" . $row->time . "</td>";
echo "</tr>";
echo "<td colspan='2'><hr size='1'></td>";
}
echo "</tbody>";
echo "</table>";
}
?>
I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop
I have some basic code which selects data from a database and echo's it into a HTML table and also add's a link to the echo'd out data. This all works fine. What I want to do is add another piece of data from my database (product_id) to the URL. Here is the code:
<?php
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tbl_products");
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='product.php?id='>" . $row['product_name'] . "</a></td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I want to add product_id from my database at the end of
<a href='product.php?id='
so the product_id becomes the ID of the page. How would I do this? I have experimented but it has resulted in numerous errors. I am sure this is a simple syntax thing but it' s bugging me.
You are closing the tag A before placing your data into href.
I think it should work:
echo "<table border='1'>
<tr>
<th>Products:</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
Answer back if it doesn't work.
The problem is you should put the '> after " . $row['product_name'] . " so that the line would read:
echo "<td><a href='product.php?id=" . $row['product_name'] . "'>" . $row['product_name'] . "</a></td>";
I have been trying to work this out for hours and I’m not sure how to achieve the correct result, hopefully someone will be able to help me.
I have the below code that will echo the result is a table vertically, but I want the cells next to each other horizontally, how can I achieve this?
<?php
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo '<div style="width:100%;">';
while($row = mysql_fetch_array($boxlink))
echo "<tr>";
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
Brill that worked thanks a lot, why is it the small things can cause such a problem!
Move your tr tags outside of the loop. Each time a tr tag is seen it makes another row.
try this:
<?php
echo '<div style="width:100%;">';
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo "<tr>";
while($row = mysql_fetch_array($boxlink))
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>