I am retrieving data from a database and displaying in a table. I would like the table to be in a 4 x 3 layout. The data retrieves just fine, it's the layout that isn't working. Here is my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href=''>";
echo "<img src='".$row['image']."' width='160' height='54'>";
echo "</a>";
echo "</div> <a href=''>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
What is supposed to go in each cell is a picture of the vehicle with the name underneath the picture.
Here is an example from another website as to how I would like it to look:
http://www.denisonmotors.com
I am not stealing any information from their site I am just trying to get my data to display in the format that they have on their site.
I am not concerned about the empty href tag as they will be filled in later.
After doing some research, I think that I have to use 2 for loops to create the table stating how many columns and row that I want.
Is it as simple as moving the last 3 lines that close the outer table outside the while loop?
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$db = new mysqli('localhost', 'root', '', 'ezwayautos');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = "SELECT * FROM vehicles ORDER BY RAND() LIMIT 12";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
echo "<table>";
echo "<tr>";
echo "<td>";
while($row = $result->fetch_assoc())
{
echo "<table border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
echo "</table>";
}
echo "</td>";
echo "</tr>";
echo "</table>";
?>
The table generation is not correct. I think you don't mean to create a table inside another table, so I think this is the way you should do:
echo "<table>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td height='160' valign='top' class='featured'>";
echo "<div class='Image-Thumbnail'>";
echo "<a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>";
echo "<img src='".$row['image']."' width='160' height='54' alt='".$row['vehicle_name']."'>";
echo "</a>";
echo "</div> <a href='inventory/view/7995179/2005-Volvo-XC90-4dr-2.html'>" .$row['vehicle_name']. "</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
You have to create the table once, and then a row for each record.
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 help, I cannot figure out, I cannot find why I am having errors and I am not able to achieve something freaking simple.
Long story short, I have a website to manage projects, so when I run the search function it throws a table with some records from the database, there is a button called "see details" which is assigned to a project id with database i.e. 21, 1, 48 etc, the problem is that when I click "see details" it throws everything from the table proposals instead of 1 project, no matter which button I click on, if its id 1, 21, 48, it prints everything.
details page
details.php:
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table WHERE id LIKE '_%'";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Notes</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
search/result page
proposals.php
<?php
include '../includes/config.php';
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt select query execution
$sql = "SELECT * FROM proposals_table";
if($result = mysqli_query($link, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered'>";
echo "<tr>";
echo "<th>Organisation</th>";
echo "<th>Project</th>";
echo "<th>Proposal Date</th>";
echo "<th>Date Received</th>";
echo "<th>Options</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['company'] . "</td>";
echo "<td>" . $row['project'] . "</td>";
echo "<td>" . $row['proposal_date'] . "</td>";
echo "<td>" . $row['date_received'] . "</td>";
echo "<td> <a class='btn btn-primary' href='details.php?id={$row['id']}'>See details</a></td>";
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
?>
If you want to show only the selected element on your details page then you need to fetch only that selected item from the database.
First of all you should separate HTML from PHP. The best would be to have them in separate files. In PHP you prepare the data to be displayed and then in HTML you fill in the blanks with PHP values.
To select a value from MySQL using a given ID you must use prepared statements with parameter binding. So if you create your link in this way:
echo "<td> <a class='btn btn-primary' href='details.php?id=".urlencode($row['id'])."'>See details</a></td>";
You can receive this ID in your details page using $_GET['id']. You can bind that value to your WHERE clause in SQL.
<?php
include '../includes/config.php';
// Attempt select query execution
$stmt = $link->prepare("SELECT * FROM proposals_table WHERE id=?");
$stmt->bind_param('s', $_GET['id']);
$stmt->execute();
$proposals = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
if($proposals) {
?>
<table class='table table-bordered'>
<tr>
<th>Organisation</th>
<th>Project</th>
<th>Proposal Date</th>
<th>Date Received</th>
<th>Notes</th>
</tr>
<?php foreach($proposals as $row): ?>
<tr>
<td><?=$row['company'] ?></td>
<td><?=$row['project'] ?></td>
<td><?=$row['proposal_date'] ?></td>
<td><?=$row['date_received'] ?></td>
<td><?=$row['notes'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php
} else {
echo 'No records matching your query were found.';
}
And of course your config.php page should look like this:
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = new mysqli('localhost', 'user', 'pass', 'db');
$link->set_charset('utf8mb4'); // always set the charset
I can't seem to get this figured out. What am I doing wrong? I have a table in my database and some rows have an empty column for the "company" field.
I'm trying to get the "Users own uploaded files in the html table below. It belongs with "1629"
<?php
// Make a MySQL Connection
mysql_connect("localhost", "", "") or die(mysql_error());
//echo "Connected to MySQL<br />";
mysql_select_db("") or die(mysql_error());
//echo "Connected to Database";
$query = "SELECT company, dmy, COUNT(company) FROM AdTracking WHERE DATE(dmy) = CURRENT_DATE GROUP BY company";
$result = mysql_query($query) or die(mysql_error());
echo "<div style='margin-top:100px;'><center><h2>";
echo date('l jS \ F Y');
echo "<br />";
echo "</h2><center></div>";
echo '
<center> <table class="pure-table pure-table-horizontal">
<thead>
<tr>
<th>Company</th>
<th>Total</th>
</tr>
</thead>
<tbody>
';
// Print out result
while($row = mysql_fetch_array($result)){
if ($row['company'] == NULL) {
echo "Users own uploaded files";
};
echo "<tr>";
echo "<td><strong>" . $row['company'] . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
echo '
</tbody>
</table> </center>
';
?>
You are printing outside of td and tr.
You have maintain format .
Try like this
echo "<tr>";
echo "<td><strong>" .($row['company'] == NULL ? "Users own uploaded files" : $row['company']). "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
you should use mysql_fetch_assoc() instead of mysql_fetch_array()
while($row = mysql_fetch_array($result)){
if (!$row['company']) // it check null or have something value
$value = "Users own uploaded files";
else
$value = $row['company'];
echo "<tr>";
echo "<td><strong>" . $value . "</strong></td>";
echo "<td>" . $row['COUNT(company)'] . "</td>";
echo "</tr>";
}
use mysqli or PDO instead of mysql because "mysql" is deprected.
Ok so I have a simple php select script and I have the code below to define variables.
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
I then have:
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
and this is supposed to pass the variables from the php select script into the url when the <a> is clicked.
All I end up with is the account/practiceSheets?id= with no actual id. I'm sure this is something stupidly simple and I do apologize as I am new to PHP and also didn't know what to call this to get a useable result in search engines! My full code is below if it helps.
<?php
$con = mysqli_connect("50.63.106.47", "usd309bands", "MacBook1!", "usd309bands");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
if ($numrows == 0) {
echo "<div class='alert alert-danger'>";
echo "No Entries, See your instructor for details.";
echo "</div>";
} else {
echo "<table class='mws-table table-striped table-hover'>";
echo "<thead align='center'>";
echo "<tr>";
echo "<th>Sheet Number</th>";
echo "<th>Total Minutes</th>";
echo "<th>Due Date</th>";
echo "<th>View</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody align='center'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>
$result = mysqli_query($con,"SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
Can't see the where the $row is coming from.
I think you've forgotten to fetch the MySQL response into an associated array.
(You could use mysqli_fetch_assoc() to acomplish that)
Here's a quick example:
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$allRows = mysqli_fetch_assoc($result);
foreach($allRows as $row) {
echo $row['id'].'<br/>';
}
This one should list all ID's from the database request.
This sql query is probably not going to return anything unless names are stored in your table like this
firstlast
"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'"
you probably want something like this
$_SESSION[SESS_FIRST_NAME] . " " . $_SESSION[SESS_LAST_NAME]
to return a name like
first last
I changed
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'></i></a></td>"
to this:
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "'> <i class='icon-eye-open'> </i> </a> </td>";
and got the desired results!
I am new to PHP and trying to learn enough to do some basic functions. I've been able to create a table for my users to edit themselves, and redisplay but I've come across a question.
Using the script below, users can input their skill level for various products. I wanted to be able to highlight each cell in which they input "0" or blank. User's input will be between 0-5 (or blank if they haven't filled it in yet).
This is all being done on my localhost so I'll admit all the security measures are not quite there.
I've read a lot of posts and tried to figure it out myself, but I'm doing something fundamentally wrong I believe.
Any assistance on this would be greatly appreciated. I've been known to buy a beer (via paypal) for those who help me with coding :)
Here is my existing code for printing out the results of the database:
<?php
//This will connect to the database in order to begin this page
mysql_connect("localhost", "root", "time2start") or die (mysql_error());
//Now we will select the database we need to talk to
mysql_select_db("joomla_dev_15") or die (mysql_error());
$query = "SELECT * FROM enterprise_storage WHERE id=1";
$result = mysql_query($query) or die (mysql_error());
echo "<table border='1'>";
echo "$row";
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th>Luke Soares</th> <th>Josh Wenger</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['model'];
echo "</td><td>";
echo $row['beeg'];
echo "</td><td>";
echo $row['hamke'];
echo "</td><td>";
echo $row['jaczyk'];
echo "</td><td>";
echo $row['jontow'];
echo "</td><td>";
echo $row['macdonald'];
echo "</td><td>";
echo $row['munozcano'];
echo "</td><td>";
echo $row['shaffer'];
echo "</td><td>";
echo $row['soares'];
echo "</td><td>";
echo $row['wenger'];
echo "</td></tr>";
}
echo "</table>";
?>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Return to the Home Page" ONCLICK="window.location.href='http://localhost/~user/joomla15/custom/skilldisplay.php'">
</FORM>
Maybe
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr>";
foreach($row as $content) {
if($content == 0) {
echo "<td style='background-color:gray;'>";
}
else {
echo "<td style='background-color:green;'>";
}
echo $content . "</td>";
}
echo $row['wenger'];
echo "</td>";
}
echo "</tr></table>";
try something like this
add this to of your generated document
<style type="text/css">
.red{ background-color: red; }
</style>
This is your PHP:
<?php
// sanitize value
$value = trim($row['model']);
$class = (empty($value)) ? 'red' : '';
// display
echo "<td class=\"$class\">$value</td>";
...
?>
Ok, so I managed to get it working finally. The two replies above helped me figure out the right approach to doing this.
Of course, my approach may not be the best method, but I've tested it and it works for my needs. For any future searchers, here's what I did:
<?php
//This will connect to the database in order to begin this page
mysql_connect("localhost", "root", "time2start") or die (mysql_error());
//Now we will select the database we need to talk to
mysql_select_db("joomla_dev_15") or die (mysql_error());
$query = "SELECT * FROM enterprise_storage";
$result = mysql_query($query) or die (mysql_error());
echo "<table border='1'>";
echo "$row";
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th><a href='http://localhost/~user/joomla15/custom/updateform.php'>Luke Soares</a></th> <th>Josh Wenger</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['model'];
echo "</td>";
if ($row['beeg'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['beeg'] ;
}else{
echo '<td>' .$row['beeg'];
}
echo "</td>";
if ($row['hamke'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['hamke'] ;
}else{
echo '<td>' .$row['hamke'];
}
echo "</td>";
if ($row['jaczyk'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['jaczyk'] ;
}else{
echo '<td>' .$row['jaczyk'];
}
echo "</td>";
if ($row['jontow'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['jontow'] ;
}else{
echo '<td>' .$row['jontow'];
}
echo "</td>";
if ($row['macdonald'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['macdonald'] ;
}else{
echo '<td>' .$row['macdonald'];
}
echo "</td>";
if ($row['munozcano'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['munozcano'] ;
}else{
echo '<td>' .$row['munozcano'];
}
echo "</td>";
if ($row['shaffer'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['shaffer'] ;
}else{
echo '<td>' .$row['shaffer'];
}
echo "</td>";
if ($row['soares'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['soares'] ;
}else{
echo '<td>' .$row['soares'];
}
echo "</td>";
if ($row['wenger'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['wenger'] ;
}else{
echo '<td>' .$row['wenger'];
}
echo "</td></tr>";
}
echo "";
?>