0

I have a table in my ms sql database and am using PHP.

What I am trying to do is:

Foreach User in table, get his age and favorite color. And for each entry i want to edit it before it is displayed. For example Each User that is retrieved and displayed on the webpage will be hyperlinked. His/her age will be hyperlinked and the color will also be hyperlinked.

Incase I wasn't clear above, let me explain in a short psuedo-code (sorry):

foreach(item i in table.items)
{
   var $name = i.name;
   var $age = i.age;
   var $color = i.color;

   webpage.display("<a href="http://domain.com/page.php?name=$name">$name</a>");
   webpage.display("<a href="">$age</a>");
   webpage.display("<a href="">$color</a>");
}

Can somebody please help me/put me in the right direction?

Thank you

2
  • What do you mean when you say - i want to edit it before it is displayed? If you want to edit it, can't you display the values in a html form? Commented Aug 17, 2010 at 3:43
  • i mean i must edit it programmatically before it is presented to the user. Commented Aug 17, 2010 at 3:44

2 Answers 2

2

How much do you need?

$serverName = "xxx";   
$uid = "xxx";     
$pwd = "xxx";    
$databaseName = "xxx";   

$connectionInfo = array( "UID"=>$uid,                              
                         "PWD"=>$pwd,                              
                         "Database"=>$databaseName);   

/* Connect using SQL Server Authentication. */    
$conn = sqlsrv_connect( $serverName, $connectionInfo);    

$tsql = "SELECT name, age, color FROM USER";   

/* Execute the query. */    
$stmt = sqlsrv_query( $conn, $tsql);    

if ( $stmt )    
{    
  while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
  {
    $name = $row["name"];
    $age = $row["age"];
    $color = $row["color"];
    echo "<a href='http://domain.com/page.php?name=$name'>$name</a>";
    echo "<a href=''>$age</a>";
    echo "<a href=''>$color</a>";
  }
}     
else     
{    
  echo "Submission unsuccessful.";
  die( print_r( sqlsrv_errors(), true));    
}

/* Free statement and connection resources. */    
sqlsrv_free_stmt( $stmt);    
sqlsrv_close( $conn);

Reference:

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

Comments

1

I am presuming that you're passing the database result as an array (see mssql_fetch_array)

foreach($items as $row) {
  $name = $row["name"];
  $age = $row["age"];
  $color = $row["color"];
  echo "<a href='http://domain.com/page.php?name=$name'>$name</a>";
  echo "<a href=''>$age</a>";
  echo "<a href=''>$color</a>";
}

Comments

Your Answer

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