0

can anyone help?

Apologies if I'm being stupid but I'm very new to this and I don't really know what I'm doing...

I have generated a table which uses php to get data from a mysql database, but I want to put an extra column at the end (of every row) which contains a link. The link will be to further details about that entry.

The code I have which displays the table is as follows:

$result = mysql_query("SELECT * FROM orders");

echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
</tr>";

// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['order_no'] . "</td>";
  echo "<td>" . $row['ord_date'] . "</td>";
  echo "<td>" . $row['est_completion_date'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['invoice_date'] . "</td>";
  echo "<td>" . $row['inv_amount'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['fName'] . "</td>";
  echo "<td>" . $row['lName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

Like I say, I'm a beginner. Basically I am reasonably happy (using the code above) in making html tables which display the results of a mysql query. However I need the users to be able to click on rows/cells in order link to other tables.

Any help much appreciated...

4
  • 1
    It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article. Commented Aug 23, 2012 at 16:51
  • What are you struggling with exactly? You can echo whatever HTML you need. Commented Aug 23, 2012 at 17:01
  • Can I suggest that you accept an answer here? You've basically asked the same question twice, and in both cases you received good answers. Commented Aug 23, 2012 at 19:00
  • possible duplicate of Using PHP forms in mysql queries Commented Aug 23, 2012 at 19:01

2 Answers 2

1
echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<!-- extra column here -->
<th>&nbsp;</th>
</tr>";

while($row = mysql_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['order_no'] . "</td>";
    echo "<td>" . $row['ord_date'] . "</td>";
    echo "<td>" . $row['est_completion_date'] . "</td>";
    echo "<td>" . $row['status'] . "</td>";
    echo "<td>" . $row['invoice_date'] . "</td>";
    echo "<td>" . $row['inv_amount'] . "</td>";
    echo "<td>" . $row['name'] . "</td>";
    echo "<td>" . $row['fName'] . "</td>";
    echo "<td>" . $row['lName'] . "</td>";
    // add link here
    echo "<td><a href=''>link</a></td>";
    echo "</tr>";
}

Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

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

7 Comments

Maybe use ' for the link string at least or escape the " in the string?
Matt, another question. The code you've given above works for linking to a URL. However if I want to link to a page which runs a mysql query based on the order_no of the row being clicked, how do I do that? The code in Anoop's answer below doesn't seem to be working (i.e. when I put that in my page and refresh the page is blank).
@user1620419 just add the order_no to the querystring of the link: echo "<a href='somepage.php?order=$order_no'>..."; You can retrieve the value using the $_GET superglobal: $_GET['order_no'].
I've tried it with echo "<td>" . "<a href="orderConsistsOf.php?key=$row['order_no']">View More</a>" . "</td>"; (as below), when I save that and refresh I only get a blank page. I have just tried echo "<td><a href='orderConsistsOf.php?order_no=$order_no'></td>"; (is this what you just suggested?) which does give me an extra column in the table, but no links. Sorry for all the super-basic questions - its confusing when you don't know how...
Try concatenating the variable value into the string, rather than forcing PHP to parse it and "figure out" if it's a variable. Obviously, if you never define $order_no nothing will print (clearly, this was just an example - $order_no should have been replaced with whatever variable you use to store the order number). Try this instead: echo "<td><a href='orderConsistsOf.php?order_no=" . $row['order_no'] . "'>View More</a></td>";
|
0

try this. add a column. It will link to another page "view_more_details.php". To identify rows uniquely, pass a unique id with this link. Here I passed order_no. In the "view_more_details.php" page, you can select this value by using $_GET['key']

$result = mysql_query("SELECT * FROM orders");

echo "<table border='5'>
<tr>
<th>order_no</th>
<th>ord_date</th>
<th>est_completion_date</th>
<th>status</th>
<th>invoice_date</th>
<th>inv_amount</th>
<th>name</th>
<th>fName</th>
<th>lName</th>
<th>&nbsp;</th>
</tr>";

// -- Use 'while' to check each row in $result in turn:
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['order_no'] . "</td>";
  echo "<td>" . $row['ord_date'] . "</td>";
  echo "<td>" . $row['est_completion_date'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['invoice_date'] . "</td>";
  echo "<td>" . $row['inv_amount'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['fName'] . "</td>";
  echo "<td>" . $row['lName'] . "</td>";
  echo "<td>" . "<a href="view_more_details.php?key=$row['order_no']">View More</a>" . "</td>";
  echo "</tr>";
  }
echo "</table>";

3 Comments

Hi Anoop - thanks for this. Just to be clear (like I say, I'm pretty ignorant with this stuff) can I then use $_GET in a mysql query on the 'view_more_details.php' (or whatever I call it) page to filter results? Is this similar to $_POST (which I have used elsewhere thanks to advice from these pages)?
Hi Anoop - when I put the code above in my page and refresh the page is blank. Do you know why this could be? Do I need to include action="view_more_details.php" method="get"> anywhere as with forms or is this implied by the code you've given?
please check whether your database table is empty. There is no need for action page for this. You can get value in view_more_details.php by the following code. $value=$_GET['key']; . Then you will get the order no of the item. Then you can execute queries by using that key.

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.