Solved

Passing variables between pages

Posted on 2004-09-21
3
249 Views
Last Modified: 2008-03-17
Hi,
I am using the code below to display a table on a page with some information for a helpdesk. I want the user to have the ability to look at more detailed information on the incident by clicking the incident number. The link to the other page works using the code :
              echo '<td> <a href="details.php">'.$row['incidentno'].'</a></td>';   (for full code listing see below)

This link works but on the 'details.php' page I only want to show details of the incident selected by the user. I know that I need to pass a variable to the other page but how?

PHP code used on page:

<?php
  mysql_connect("localhost", "administrator", "password");
  echo '<table width="85%" border="1">';

echo '<tr><th>Incident Number</th><th>Type</th><th>Decription</th><th>Date</th><th>Engineer</th></tr>';  

$sql="SELECT incidentno, type, shortdesc, date, engineer FROM helpdesk.incidents WHERE status = 'Open'";
$result=mysql_query($sql);

if(mysql_num_rows($result)>0)
{
 
  while($row=mysql_fetch_assoc($result))  
  {
        
    echo '<tr>';
    echo '<td> <a href="details.php">'.$row['incidentno'].'</a></td>';
    echo '<td>'.$row['type'].'</td>';
    echo '<td>'.$row['shortdesc'].'</td>';
    echo '<td>'.$row['date'].'</td>';
    echo '<td>'.$row['engineer'].'</td>';
    echo '</tr>';

  }
}
else
{
  echo '<tr><td colspan="200">no data retieved</td></tr>';  
}
echo '</table>';
?>
0
Comment
Question by:dm_mcelduff
3 Comments
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 125 total points
Comment Utility

Make your while statement like this:

  while($row=mysql_fetch_assoc($result))  
  {
       
    echo '<tr>';
    echo '<td> <a href="details.php?id=' . $row['incidentno'] . '">'.$row['incidentno'].'</a></td>';
    echo '<td>'.$row['type'].'</td>';
    echo '<td>'.$row['shortdesc'].'</td>';
    echo '<td>'.$row['date'].'</td>';
    echo '<td>'.$row['engineer'].'</td>';
    echo '</tr>';

  }

Then, in your details.php page, use this code and SQL Statement:

# if the id is set in the url, use it, otherwise use 0
$ino = isset($_GET['id']) && is_numeric($_GET['id']) ? $_GET['id'] : 0;
$sql="SELECT incidentno, type, shortdesc, date, engineer FROM helpdesk.incidents WHERE incidentno = '" . $ino . "'";



Any more questions, feel free to ask...
0
 
LVL 4

Expert Comment

by:llcooljayce
Comment Utility
You can pass variables in the URL by simply adhering to this format:

<a href="mypage.php?VariableName=$VariableValue">Link</a>

To add more than one variable to pass simply use the & symbol.  For example:

<a href="mypage.php?name=$username&birthday=$dob">Link</a>

You can pass as many variables as you need to.  To access the variable value in the following php page simply refer to it as the VariableName (as in my first example).  Or in my second example:

echo "Hello there ".$name." welcome to my new page!";

Hope this helps.  Cheers.

Jayce
0
 
LVL 2

Author Comment

by:dm_mcelduff
Comment Utility
Cheers cLFlaVA that worked a treat :)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now