0

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Could you provide a bit more detail around how I should use the $_GET['user'] please?
Well you just need to use it in a SQL query (SELECT * WHERE username=) to get the user. Do note that you'll have to format it for security reason (see SQL Injections)
Thanks. I had tried as you described above but forgot to actually update the file on my apache server (doh!). Thanks for the help. Works perfectly.
0

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 ?

1 Comment

I do have a user id in the table. I am using the portfolio vairable so I can change some of the data from the database to a more human readable format when displayed (example isadmin is 1 or 0 in the database but is displayed as YES or NO for ease). I am just giving the solutions a try now. Thanks for the help!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.