andy
andy

Reputation: 391

php links in a table based on sql query

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

Upvotes: 0

Views: 70

Answers (3)

timclutton
timclutton

Reputation: 13004

Simply changing this line

echo("<td>" . $row["username"] . "</td>");

to this

echo("<td><a href="???">" . $row["username"] . "</a></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.

Upvotes: 0

Vincent Decaux
Vincent Decaux

Reputation: 10714

Why do you use $portfolio variable ? It just stock the data you already have in $rows.

Then, just use this :

echo '<td>
         <a href="your_script.php?user='. $row['id'] .'">'. $row["username"] .'</a> 
      </td>';

I think you have an user ID in your table ?

Upvotes: 0

Tom Tom
Tom Tom

Reputation: 3698

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.

Upvotes: 1

Related Questions