Victor Victor - 1 month ago 7
HTML Question

How do I create a button or hyperlink for every row of a PHP/HTML web page that displays rows of a SQL table?

I am using Linux, Apache, PHP and Postgres for my web application program. I have a web application that returns the content of SQL tables. The user has to log in and type in a valid SQL table name. How can I create a button for each row that is returned and displayed in the web UI? It has to be dynamic because the rows returned vary depending on the table the user enters.

Ultimately I'd like to have the user update the row in the SQL database through the web UI. For now I want a button to appear for each row that is returned. If the button or link was available for every cell (regardless of whether the cell was empty or not), that would work. The rows returned are currently in static alphanumeric text. I'd like there to be a hyperlink or button for each row. I can then have the user go to a new .php web page from that link or button. This page will alow for an update to the SQL table.

How do I create a button or hyperlink in a web page for each SQL row returned and displayed in a PHP web page?

The code below leaves out the authentication web page. Here is my code for the web page that is repeatedly called as it allows users to type in a table name and view it:

<?php

session_start();

$pa = $_POST["table"];

$host = $_SESSION['hostv'];
$port = $_SESSION['portv'];
$dbname = $_SESSION['dbv'];
$credentials = $_SESSION['cv'];
$comp = $credentials;

$db = pg_connect( "$host $port $dbname $credentials" );
$query = 'select * from ' . $pa . ';';
$cc = pg_query($query);


showingfunc($cc);

function showingfunc($cc)
{
/* This code below was copied and modified from razorsql.com */
$i = 0;
echo '<html><body><table><tr>';
if ($cc) {
}
else {
echo '<td>' . "Failure." . $dbname . '</td>';
}
while ($i < pg_num_fields($cc))
{
$fieldName = pg_field_name($cc, $i);
echo '<td>' . $fieldName . '</td>';
$i = $i + 1;
}
echo '</tr>';
$i = 0;
while ($row = pg_fetch_row($cc))
{


echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
pg_free_result($cc);

echo '</table></body></html>';


}

?>

<html>
<body>

<form action="repeater.php" method="post">
TableToView: <input type="text" name="table"<br>

<input type="submit">
</form>

<a href="logout.php">Logout</a>
</body>
</html>

Answer Source

Simply add a cell containing your action button.

while ($row = pg_fetch_row($cc))
{
   echo '<tr>';
    $count = count($row);
    $y = 0;
    while ($y < $count)
    {
            $c_row = current($row);
            echo '<td>' . $c_row . '</td>';
            next($row);
            $y = $y + 1;
    }
    //New Code on the line below
    echo '<td><a class="edit" href="[YOURLINK]">Edit</a></td>' //You Probably want to add an ID to your query string here
    echo '</tr>';
    $i = $i + 1;
}   

Also, add an empty cell in your header in a similar fashion.