I am populating a table using php from an array (populated by an mysql query). The table code I am using is:
<thead>
<tr>
<hr>
<th>UserName</th>
<th>Nick Name</th>
<th>Role</th>
<th>Unit</th>
<th>Active</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<?php
foreach ($portfolio as $row)
{
echo("<tr>");
echo("<td>" . $row["username"] . "</td>");
echo("<td>" . $row["nickname"] . "</td>");
echo("<td>" . $row["role"] . "</td>");
echo("<td>" . $row["unit"] . "</td>");
echo("<td>" . $row["active"] . "</td>");
echo("<td>" . $row["isadmin"] . "</td>");
echo("</tr>");
}
?>
I have been trying without luck to find a way to have the first column in the table a hyperlink that allows editing of that users details (IE redirects to another page/php). The array itself is being populated using this code:
//now lets get the user's stock info
foreach ($rows as $row)
{
$stock = lookup($row["username"]);
$stock["username"] = $row["username"];
$stock["nickname"] = $row["nickname"];
$stock["role"] = $row["role"];
$stock["unit"] = $row["unit"];
$stock["active"] = $row["active"];
$portfolio[] = $stock;
}
How can I make the results of the sql query / php a link within the table?
Thanks for the help, I am new to php/mysql and trying to find my feet;
Andy
You'll need to do something like the following, then on your user edit page you can get the username with $_GET['user']
<?php
foreach ($portfolio as $row)
{
echo("<td><a href='user-edit.php?user=" . $row["username"] . "'>" . $row["username"] . "</a></td>");
}
?>
Given an unique username ofc, else you can do it with the id or any unique field.
Why do you use $portfolio variable ? It just stock the data you already have in $rows.
Then, just use this :
echo '<td>
'. $row["username"] .'
</td>';
I think you have an user ID in your table ?
Simply changing this line
echo("<td>" . $row["username"] . "</td>");
to this
echo("<td>" . $row["username"] . "</td>");
will make the first column clickable. Of course you'll need to fill in the target of the link. Making the details editable is a lot more code though.
Related
This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed last year.
my task at he moment is to show the data from a sql db in a browser.
My code is fine for that, but now i have more and bigger sql tables and i am looking for a way to read and show it more dynamic. So that i dont have to write all the names of the column (the echo statements, if i have 100 columns it would not really work like this) in the code. Is there ab better way in this case?
Thanks.
<?php
error_reporting(E_ALL);
$db_link = mysqli_connect (MYSQL_HOST,
MYSQL_BENUTZER,
MYSQL_KENNWORT,
MYSQL_DATENBANK);
if ( $db_link )
{
echo('<p> Verbindung vorhanden: </p>');
}
else
{
echo('<p style="color:red"> Keine Verbindung vorhanden: </p>');
}
$db_res = mysqli_query($db_link, "SELECT AA, B1, C3 FROM tableXYZ")
or die("Fehler: " . mysqli_error());
echo("<table>");
while($row = mysqli_fetch_array($db_res))
{
echo("<tr>");
echo("<td>" . $row["AA"] . "</td>");
echo("<td>" . $row["B1"] . "</td>");
echo("<td>" . $row["C3"] . "</td>");
echo("</tr>");
}
echo("</table>");
?>
You could use a foreach loop to show all columns. Like this:
while($row = mysqli_fetch_array($db_res))
{
echo("<tr>");
foreach ($row as $column)
{
echo("<td>" . $column . "</td>");
}
echo("</tr>");
}
If this is not what you want you have to clarify, in your question, what you really want.
I have a table which is full with data from my database. I am now trying to implement a delete feature on that table, I do have the correct syntax to echo checkboxes but I don't know how to format the line correctly so the corresponding checkbox aligns correctly with each row. Any ideas? (I currently have the line echo'd above the closing table tag. Which makes the boxes appear above the table)
$sql = "SELECT * FROM products";
$answer = mysqli_query($connection, $sql);
if ($answer->num_rows > 0)
{
echo "<table><tr><th>ID</th><th>Name</th><th>Description</th><th>Price</th><th>Stock</th><th>Delete Product</th></tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["product_id"]
echo "0 results";
}
Insert the checkbox in an additional table cell (td).
A <tr> element contains one or more <th> or <td> elements. Another content is not treated as part of that row.
echo "<tr><td>" . $row["product_id"]
. "</td><td>" . $row["product_name"]
. "</td><td> " . $row["product_description"]
. "</td><td> " . $row["product_price"]
. "</td><td> " . $row["product_cost_price"]
. "</td><td> " . $row["product_stock"]
. "</td><td>" . $row["product_ean"]
. "</td><td>" .'<input name="delete['.$row['product_id'].']" type="checkbox">'
. "</td></tr>";
I am having a little bit of trouble trying to format a table to the way I want it. It is supposed to display a list of all movie ids in one column and then a dropdown in the next allowing the user to change the pay status.
The desired result is as such :
-- Movie Id -----------Pay Status-------
[___id 1____] [____dropdown_____]
[___id 2____]
[___id 3____]
However I am currently getting something like this :
-- Movie Id -----------Pay Status-------
[___id 1____]
[___id 2____]
[___id 3____]
[____dropdown_____]
The code i am working on is below :
echo "<tr>
<th>Title</th>
<th>Pay Status</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row ["movieid"] . "</tr>";
}
//// pay status
echo "<td>" . "<select name='payid'>";
while ($row = $result5->fetch_assoc()) {
echo "<option value='" . $row['payid'] . "'>" . $row['payid'] . "</option>";
}
echo "</select>" . "</td>";
Any help is appreciated. Thank you.
I have a database loaded with different churches information, I am trying to insert all the information from the database into a PHP table with 3 rows.
I would like the structure of each cell to be:
Church Name
Image
Pastor Name
I can easily insert all data into a table, but I cannot get it to display as 3 rows.
echo("<table>");
while($row = mysql_fetch_array($rs))
{
echo("<tr>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("</tr>");
}
echo("</table>");
Doing this causes me to have 3 rows in correct structure, but having duplicate data. I understand that I have not changed the id, but am not sure what I should do
You're repeating the data on the row. If you want to show 3 items per row, you need to add a counter and a statement to draw the table row breaks, as below:
$int_Col = 1;
echo("<table>");
while($row = mysql_fetch_array($rs))
{
if ($int_Col == 1) {
echo("<tr>");
}
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
if ($int_Col == 3) {
echo("</tr>");
$int_Col = 1;
} else {
$int_Col++;
}
}
if ($int_Col > 1) {
echo("<td colspan='". (3 - $int_Col) ."'> </td></tr>");
}
echo("</table>");
The last check ($int_Col > 1) should ensure the table is rendered properly with an empty cell - should span the correct amount of cells that were not drawn on the current row.
Just clarify, I think there's two definitions of "rows" here.
MySQL table rows
HTML table rows
I believe you're after getting 3 MySQL table rows into one HTML table row.
Use the following to split the HTML row every 3 MySQL rows:
$i = 0;
while($row = mysql_fetch_array($rs))
{
// whatever you're already doing(ish)
$i++;
if($i % 3 == 0)
{
echo('</tr><tr>');
}
}
You'll need to put some extra checks in in the event that the total number of MySQL rows doesn't divide exactly by 3 because in that case you'll have one or two empty cells to fill at the end of the table.
If I get your code right you are creating 3 rows for every row in the database. mysq_fetch will run the loop for every single row, so the right content of the loop would be:
echo("<tr>");
echo("<td>");
echo("<a href='" . $row['website'] . "'>" . $row['churchName'] . "</a><br>");
echo("<img src=\"" . $row['image'] . "\"><br>");
echo($row['pastorName'] . "<br><br>");
echo("</td>");
echo("</tr>");
I have an upload system in my site where users can upload files and write a brief description of them. Each upload has a corresponding SQL entry.
Another page on my site will then display every file uploaded along with some traits about that file
The question is:
How can create a table with a variable set of rows and how can I set a table to automatically populate the new rows?
I am capable with PHP but still a novice, weak with HTML (I use a wysiwyg) and completely inexperienced with Javascript.
Any nudge in the correct direction would be hugely appreciated...
In order to make the rows of your HTML table "dynamic" using PHP, you should use the foreach() control structure. For example, say you have some code to grab the data from the database and it returns it as an array or some sort of result statement that implements an Iterable interface (we'll call it $results), you can then:
<table>
<thead>
<tr>
<th>File Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php
foreach($results as $result)
{
// Start row
echo("<tr>");
echo("<td>" . $result->filename . "</td>");
echo("<td>" . $result->description . "</td>");
// End row
echo("</tr>");
}
?>
</tbody>
</table>
You can use a loop to populate the table based on results from a query of your database. Here's the code I use for one of my pages:
$result = $db->query_read("SELECT dice.RollID AS `Roll`, dice.Limit AS `Limit`,
dice.Value AS `Value`, dice.Dateline AS `Dateline`, dice.Comment AS `Comment`
FROM dice
ORDER BY Dateline DESC LIMIT 25");
// ###### MAIN CODE ######
$str = '<table>
<tr><th>ID</th><th>Roll</th><th>Time</th><th>Comment</th></tr>';
while ($resultLoop = $db->fetch_array($result)) {
$ID = $resultLoop["Roll"];
$roll = $resultLoop["Value"] . '/' . $resultLoop["Limit"];
$when = date('m-d-Y H:i:s', $resultLoop["Dateline"]);
$comment = $resultLoop["Comment"];
$str .= '<tr>
<td>' . $ID . '</td>
<td>' . $roll . '</td>
<td>' . $when . '</td>
<td>' . $comment . '</td></tr>';
}
$str .= '</table>';
echo $str;
One thing to note is that I use $db->* functions because it is integrated with a forum software. You should be using mysqli_* functions as found here: http://php.net/manual/en/book.mysqli.php