LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   populate a php query into a html table with <a href> (https://www.linuxquestions.org/questions/programming-9/populate-a-php-query-into-a-html-table-with-a-href-638464/)

htamayo 04-28-2008 03:14 PM

populate a php query into a html table with <a href>
 
Hi, I have several hours dealing with the next problem:
I want to populate a query into a html table, but the problem is that I want to put in the "name" field a url, for example: <a href="viewprofile.php>, So, the user may clic in a specific record and then the user can view the entire profile but I don't know how to controle the value for each record in the table, I'm looking something like it can help me to controle each value, something like <td value>and then send this value via the POST event.

So, may you help me with this?


Thanks a lot

rubadub 04-28-2008 04:19 PM

Code:

print "<table width='100%'>";
while ($a = mysql_fetch_array($res))
{
        print "<tr><td>".$a['id']."</td><td><a href='user_".$a['name'].".html'>Link</a></td></tr>";
}
print "</table>";

Something like that?

BrianK 04-28-2008 04:25 PM

It's a little hard to figure out what you're asking. It seems like you want to have a page full of links, each link will essentially send form data to the server so that it can pull info from a database, is that right?

Does it have to be a POST variable? It would be much easier with GET variables. With get, you simply make a link that looks something like:

Code:

<a href="some_page.php?profile_name=some_user_name>some_user_name profile</a>
and then the PHP would look something like:

PHP Code:

function get_profile($uname) {
  
$query "SELECT whatever FROM profile WHERE profile_name='$uname';";
  ...
  return 
$profile;
}

if isset(
$_GET['profile_name']) {
  echo 
get_profile($_GET['profile_name']);


if you *must* do it with post vars, then I would say use javascript to populate then send a form. That's a little more complex of an example. I can post it here, but won't unless you ask for it.

htamayo 04-28-2008 04:43 PM

Something like this:
Code:

<TABLE border='2'>
<tr><td>Nombres</td><td>Apellidos</td><td>Nivel Academico</td><td>NIT</td><td>NUP</td></tr>
<?php
        while($xrsa = mysqli_fetch_assoc($resa))        {
                echo "<tr><td><a href='viewprofile.php'" . $xrsa['nombres']. ">" .$xrsa['nombres'] . "</a></td><td>" . $xrsa['apellidos'] . "</td><td>" . $xrsa['nivacad'] . "</td><td>" . $xrsa['nit'] . "</td><td>" . $xrsa['nup']. "</td></tr>";               
                }
?>

the main problems that I have are:
1. this line <td><a href='viewprofile.php'" . $xrsa['nombres']. ">" , is the one that gives me a headache, because I don't know how to catach the value of some rows, because the name could be: Saquile, Larry, Patrick, Michael, etc.

2. how viewprofile.php will catch this value? because the value will not be passed trough the POST event, because is via the <a href>

Thanks a lot

rubadub 04-28-2008 04:48 PM

Code:

echo "<tr><td><a href='viewprofile.php?name='" . $xrsa['nombres']. "'>" .$xrsa['nombres'] . "</a></td><td>" . $xrsa['apellidos'] . "</td><td>" . $xrsa['nivacad'] . "</td><td>" . $xrsa['nit'] . "</td><td>" . $xrsa['nup']. "</td></tr>";
then

Code:

$name = $_GET['name'];

BrianK 04-28-2008 05:13 PM

Quote:

Originally Posted by htamayo (Post 3135942)
2. how viewprofile.php will catch this value? because the value will not be passed trough the POST event, because is via the <a href>

Just to clarify what both rubadub and I are saying, POST is not the best way to accomplish what you're after. GET is a better choice & the way both of our examples work.

FYI, GET variables are sent through the URL, i.e. http://some_site.com?variable1=value1&variable2=value2

POST values are sent via the html header & require the page to submit the data through, for example, a form.

The nice thing about GET is that you can construct the URL however you'd like through a href, i.e.

Code:

echo "<a href='http://some_side.com/page.php?variable1=$val1&variable2=$val2'>link</a><br />\n";
... and then access that data using php's $_GET array. This is the same thing as creating a GET method form & submitting it.

POST become more difficult because you actually have to submit form data - you can't just put it in the URL.


All times are GMT -5. The time now is 11:08 AM.