views:

130

answers:

2

Hi All,

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"&gt;$name&lt;/a&gt;");
   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  A: 

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'&gt;$name&lt;/a&gt;";
  echo "<a href=''>$age</a>";
  echo "<a href=''>$color</a>";
}
ShiVik
+2  A: 

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'&gt;$name&lt;/a&gt;";
    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:

OMG Ponies
Thank you very much :)
lucifer