Link to home
Create AccountLog in
Avatar of GD_GRAY
GD_GRAYFlag for United States of America

asked on

How do I turn the result of a query into a html type link

I have a mssql query setup on a php page that pulls the data into a simple table like the image below. What I need to work out is the data in the  Company ID field becomes a link that when pressed puts that data into another query as the variable. Did I ask the right question ?
User generated image
select phone from company where cmp_id='the data from the table'

Open in new window

Avatar of gamebits
gamebits
Flag of Canada image

Yes, but would be helpful to see your code, here is a quick example.


$sql = "SELECT firstname FROM table WHERE lastname = '$somename'";
$res = mysql_query($sql);

while($data = mysql_fetch_array($res)){

     echo "<a href=\"whereyouwanttogo.php?fname=".$data['firstname']."\">".$data['firstname']."</a>";

}
hi

in the loop that displays the table to add the link to another page that executed the 2nd query

simple example:

in the table code
            <td>                  
             <a href=company.php?id='.$row[cmp_id].'>'.$row[cmp_id].'</a>        
            </td>

to execute the 2nd  


if (isset($_GET['id'])){

$sql = "SELECT * FROM company WHERE cmp_id='$id' ";
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);

echo  ' Company id : '.$row['cmp_id'].' <br>
            Comany name :'.$row['cmp_name'].'  <br>
            ....
            ....
            ';


Avatar of GD_GRAY

ASKER

Cool, here is the full code. And thank you...
<?php


$conn=odbc_connect('mydb','abc','xyz');
if (!$conn)
  {exit("Connection Failed: " . $conn);}
$sql="SELECT Distinct orderheader.ord_number, orderheader.ord_refnum, orderheader.ord_tractor,orderheader.ord_hdrnumber, orderheader.ord_Driver1, stops.stp_type, stops.cmp_name, company.cmp_address1, company.cty_nmstct, stops.cmp_id, stops.stp_phonenumber, stops.stp_schdtearliest, stops.stp_reftype, stops.stp_refnum, stops.stp_weight, stops.stp_weightunit, stops.stp_count, stops.stp_countunit, stops.stp_lgh_mileage
FROM orderheader, stops, company WHERE orderheader.ord_hdrnumber = stops.ord_hdrnumber AND orderheader.ord_status='STD' AND company.cmp_id = stops.cmp_id AND orderheader.ord_Driver1='$unit' ORDER BY stops.stp_schdtearliest ASC";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table id='load' border='1'><tr>";
echo "<th>Order #</th>";
echo "<th>Ref Number</th>";
echo "<th>Tractor #</th>";
echo "<th>Stop Type</th>";
echo "<th>Company Name</th>";
echo "<th>Company ID</th>";
echo "<th>Address</th>";
echo "<th>City & State</th>";
echo "<th>Phone #</th>";
echo "<th>Dlev. Date</th>";
echo "<th>Ref Type</th>";
echo "<th>Ref #</th>";
echo "<th>Weight</th>";
echo "<th>Case Count</th>";
echo "<th>Mileage</th></tr>";
while (odbc_fetch_row($rs))
  {
  $ord_number=odbc_result($rs,"ord_number");
  $ord_refnum=odbc_result($rs,"ord_refnum");
  $ord_tractor=odbc_result($rs,"ord_tractor");
  $stp_type=odbc_result($rs,"stp_type");
  $cmp_name=odbc_result($rs,"cmp_name");
  $cmp_id=odbc_result($rs,"cmp_id");
  $cmp_address1=odbc_result($rs,"cmp_address1");
  $cty_nmstct=odbc_result($rs,"cty_nmstct");
  $stp_phonenumber=odbc_result($rs,"stp_phonenumber");
  $stp_schdtearliest=odbc_result($rs,"stp_schdtearliest");
  $stp_schdtearliest=date("m-d-Y H:i A", strtotime(odbc_result($rs,"stp_schdtearliest")));
  $stp_reftype=odbc_result($rs,"stp_reftype");
  $stp_refnum=odbc_result($rs,"stp_refnum");
  $stp_weight=odbc_result($rs,"stp_weight");
  $stp_count=odbc_result($rs,"stp_count");
  $stp_lgh_mileage=odbc_result($rs,"stp_lgh_mileage");
  echo "<tr><td>$ord_number</td>";
  echo "<td>$ord_refnum</td>";
  echo "<td>$ord_tractor</td>";
  echo "<td>$stp_type</td>";
  echo "<td>$cmp_name</td>";
  echo "<td>$cmp_id</td>";
  echo "<td>$cmp_address1</td>";
  echo "<td>$cty_nmstct</td>";
  echo "<td>$stp_phonenumber</td>";
  echo "<td>$stp_schdtearliest</td>";
  echo "<td>$stp_reftype</td>";
  echo "<td>$stp_refnum</td>";
  echo "<td>$stp_weight</td>";
  echo "<td>$stp_count</td>";
  echo "<td>$stp_lgh_mileage</td></tr>";
  }
odbc_close($conn);
echo "</table>";
?>

Open in new window

Avatar of GD_GRAY

ASKER

Sorry, here is the secound script I want to feed to.
if(isset($_POST['submit'])){  

$unit = $_POST['unit'];

$conn=odbc_connect('mydb','abc','xyz');
if (!$conn)
  {exit("Connection Failed: " . $conn);}

$sql="SELECT cmp_directions from company where cmp_id='$unit'";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table id='load' border='1'><tr>";
echo "<th>Directions</th></tr>";
while (odbc_fetch_row($rs))
  {

  $cmp_directions=odbc_result($rs,"cmp_directions");
  echo "<tr><td>$cmp_directions</td></tr>";

  }
odbc_close($conn);
echo "</table>";

Open in new window

echo "<tr><td><a href=?id='.$cmp_directions.'>'.$cmp_directions.'</a>  
Line 3 of the script at ID:34469579 should be like this:

$unit = $_GET['unit'];

The GET array is populated with information from the URL.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of GD_GRAY

ASKER

Ray, after I change line 51 the field is blank. I made another change like below and still blank. What might I be doing wrong.
echo "<td><a href=[url_directions.php]?unit=$cmp_id</td>";


echo "<td><a href=[url_directions.php]?unit=$cmp_id>$cmp_id</a></td>";

Open in new window

Avatar of GD_GRAY

ASKER

Ray, sorry Im dumb... I found it. The code below works like a champ and your code from ID:34472741 get's the sent $unit and loads the directions just as i had in mind.... Thank you so much....thank all of you.
echo "<td><a href=url_directions.php?unit=$cmp_id>$cmp_id</a></td>";

Open in new window

Yep, that looks great!
Avatar of GD_GRAY

ASKER

Thank you all for the help. One day when i grow up i want to be just like you...  Seriously, its people like you that make EE
Thank you !
Thanks for the points, and for your kind words.  Best always, ~Ray