I am working on a research project regarding Bing and it's search results, and since I am still very new to PHP, I am looking for a little bit of help. Yesterday I already got some help here and the result was so great that I am trying once more :)
I have the following code:
<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
foreach($r->d->results as $value) {
foreach($value->Web as $result) {
echo $result->Title, "\r\n";
echo $result->Description, "\r\n";
echo $result->Url, "\r\n";
}
}
?>
This basically shows the title, description and URL of the three sites of the json results from Bing (Note: It's not always just 3 results, normally it's 10 but can be any number)
Now here is what my big mission is:
For my research project, I need to shuffle the results randomly, meaning I would like to see it for example like:
First time I load the page:
CakePHP: the rapid development php framework. Pages
PHP Presents
PHP: Hypertext Preprocessor
Second time I load the page:
PHP Presents
PHP: Hypertext Preprocessor
CakePHP: the rapid development php framework. Pages
Third time I load the page:
PHP: Hypertext Preprocessor
CakePHP: the rapid development php framework. Pages
PHP Presents
Again, the order above is just an example - I really want it randomly with no specific order.
I would like to thank my previous helpers and I am looking forward to your answers.
Thank you for your time reading this :)
Update:
If anyone would be so kind and also show me an example of only shuffling the results except the first three ones - that would be really great.
You can use a temporary array to store the lines and the shuffle method to randomize it.
<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
$tmpArray = array();
foreach($r->d->results as $value) {
foreach($value->Web as $result) {
$string = $result->Title."\r\n";
$string.= $result->Description."\r\n";
$string.= $result->Url."\r\n";
$tmpArray[] = $string;
}
}
shuffle($tmpArray);
foreach($tmpArray as $string){
echo $string;
}
?>
Also this might work:
$r = json_decode($r);
$r->d->results = shuffle($r->d->results);
EDIT : To shuffle every key but the first three:
<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = json_decode($r);
$tmpArray = array();
foreach($r->d->results as $value) {
foreach($value->Web as $result) {
$string = $result->Title."\r\n";
$string.= $result->Description."\r\n";
$string.= $result->Url."\r\n";
$tmpArray[] = $string;
}
}
$limit = min(3,count($tmpArray)) -1;
for($i=0;$i<$limit;$i++){
echo array_shift($tmpArray); // echo and remove first item
}
shuffle($tmpArray);
foreach($tmpArray as $string){
echo $string;
}
?>
<?php
$r = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/Composite?Sources=\u0027web\u0027&Market=\u0027en-US\u0027&Query=\u0027php\u0027&Adult=\u0027off\u0027&$skip=0&$top=1","type":"ExpandableSearchResult"},"ID":"1c509d25-5ca4-4db5-bfc5-cafd6917e2c2","WebTotal":"10600000","WebOffset":"0","ImageTotal":"","ImageOffset":"","VideoTotal":"","VideoOffset":"","NewsTotal":"","NewsOffset":"","SpellingSuggestionsTotal":"","AlteredQuery":"","AlterationOverrideQuery":"","Web":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=0&$top=1","type":"WebResult"},"ID":"4cf2a8d6-21b7-433d-81e9-84e74091a44a","Title":"PHP: Hypertext Preprocessor","Description":"What is PHP? PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.","DisplayUrl":"www.php.net","Url":"http://www.php.net/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=48&$top=1","type":"WebResult"},"ID":"2d8f8107-895e-4052-9edc-b656e74c3f2e","Title":"CakePHP: the rapid development php framework. Pages","Description":"Official website. Offers a manual for beginners and links towards the last version.","DisplayUrl":"cakephp.org","Url":"http://cakephp.org/"},{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/ExpandableSearchResultSet(guid\u00271c509d25-5ca4-4db5-bfc5-cafd6917e2c2\u0027)/Web?$skip=49&$top=1","type":"WebResult"},"ID":"816d781c-ff8b-4a60-b5b7-28d807bba28a","Title":"PHP Presents","Description":"Welcome to the PHP Presentation System. Here we list all of the available presentation categories stored within this system.","DisplayUrl":"talks.php.net","Url":"http://talks.php.net/"}],"Image":[],"Video":[],"News":[],"RelatedSearch":[],"SpellingSuggestions":[]}]}}';
$r = shuffle(json_decode($r));
foreach($r->d->results as $value) {
foreach($value->Web as $result) {
echo $result->Title, "\r\n";
echo $result->Description, "\r\n";
echo $result->Url, "\r\n";
}
}
?>
Related
In the course of a master's thesis I developed an ontology which I imported into Ontotext GraphDB. At this point I need to connect a website (HTML / PHP) with the ontology I imported into Ontotext GraphBD. My technical knowledge is not high so I wondered if it is possible to connect these two components and if yes how can I do it?
I have on one side a website and on the other an ontology in GraphDB. Now I need that in this website it is possible for example to do CRUD operations so that these operations are also done in the ontology that is in Ontotext GraphDB.
Example: Consult through my website all the individuals present in the ontology.
I in the Ontotext GraphDB workbench through the Sparql queries I get these operations, but I want to do it through the website that I'm doing in HTML, PHP and CSS.
Thanks for your attention.
Best regards
I think I solved my problem with this here.
In general, you need to download Semsol's ARC2 library.
Then you create the php file with a structure like this:
<?php
/* ARC2 static class inclusion */
include_once('semsol/ARC2.php');
$dbpconfig = array(
"remote_store_endpoint" => "http://dbpedia.org/sparql",
);
$store = ARC2::getRemoteStore($dbpconfig);
if ($errs = $store->getErrors()) {
echo "<h1>getRemoteSotre error<h1>" ;
}
$query = '...';
/* execute the query */
$rows = $store->query($query, 'rows');
if ($errs = $store->getErrors()) {
echo "Query errors" ;
print_r($errs);
}
/* display the results in an HTML table */
echo "..."
?>
I thank everyone who tried to help me.
You need to somehow query your GraphDB from your PHP application using a remote sparql service. If this is what you want, in java, this can be easily done using Jena QueryExecutionFactory.sparqlService method.
However, a simple googling for PHP results in A PHP forward proxy for remote access to SPARQL endpoints. Where you can send queries and receive results from a SPARQL endpoint, I guess this is what you actually need.
Furthermore, this link gives you multiple options for SPARQL implementations including some php ones.
I have solid but basic understanding of HTML, CSS, javascript, Sql, and PHP. I thought it would be cool to build a website since I have most of the skills I need. The overall purpose of the site requires a web crawler to extract price data from finance websites. The web crawler itself is a little advanced for me so I used a pre-made one from the following link http://cdn.makeuseof.com/wp-content/uploads/2010/12/my-first-crawler.zip?3cb89b . My code is as follows:
<?
if(count($_POST)>0){
include_once('simple_html_dom.php');
$tickerSelected = filter_input(INPUT_POST,"tick");
$url_tick = $tickerSelected;
if($url_tick !=""){ //MAKE SURE SOMETHING IS IN THE FIELD
$target_url = "http://www.marketwatch.com/investing/stock/$url_tick";
$html = new simple_html_dom();
$html->load_file($target_url);
foreach($html->find('h1[id=instrumentname]') as $name){
//$text = convert_html_to_text($name);
print $name;
}
foreach($html->find('p[class=data bgLast]') as $dollar) //THIS IS WHERE MY PROBLEM IS
{
print $dollar; //<--I WANT TO MANIPULATE THIS NUMBER INSTEAD OF ONLY BEING ABLE TO PRINT IT
}
}
else
print"<h2>Please Enter a Ticker</h2>";
}
?>
Using the webcrawler to print out the information has been a success. So there is not issue in the setup. Instead the problem is that I cannot figure out how to turn the ticker price which is returned as $dollar into a floating point number. If I want to do any math with the returned stock value an error is returned (below).
Notice: Object of class simple_html_dom_node could not be converted to int.
Anyway, I am a finance major so this stuff is all new to me (however, it is cooler than finance..) I would appreciate any help. Thanks!
Go to the official documentation. There is an example here
I want to use zenphoto with my symfony2 website. I would do so by making calls to zenphoto to retrieve images. Here's a non-symfony2 coding example from another one of my sites that it would use as a basis:
<?php
define('WEBPATH', 'zenphoto');
require_once(WEBPATH . "/zp-core/template-functions.php");
$currentPage = $_SERVER["PHP_SELF"];
$imageNum = 0;
if (isset($_GET['imageNum']))
$imageNum = $_GET['imageNum'];
$gallery = new Gallery();
$numAlbums = $gallery->getNumAlbums();
$gallery->getAlbums();
for ($i = 0 ; $i < $numAlbums; $i++)
{
$album = $gallery->getAlbum($i);
$title = $album->getTitle();
if ($title == "Overview") break;
}
$album->getImages();
$totalImages = $album->getNumImages() - 1;
$image = $album->getImage($imageNum);
$sizedImage = $image->getSizedImage(600);
?>
There's more code buried in the HTML. All the code would end up in a controller. My question is how to best integrate zenphoto if at all. I could just install the code independent of the framework, but I imagine there's a better way.
There's a whole front end for uploading and managing images as well as admin functions. These web pages would be independent of Symfony as it doesn't seem possible to integrate them.
I went with the simple approach of just including the libraries into the controller.
define('SERVERPATH',$_SERVER['DOCUMENT_ROOT'] . "/zenphoto");
define('WEBPATH','/zenphoto');
require_once(SERVERPATH.'/zp-core/template-functions.php');
The zenphoto directory is at the same level as my symfony directory. This worked find, but when I tried a simple call to the API, it failed in the zenphoto code. When I asked about the problem on the zenphoto forum, I was told I would have to do a lot of manual set up to make it work. At this point, I gave up and went with placing my images on flickr and using their API.
I have a task to convert the standalone PHP files to Magento's MVC. These PHP files were created by another developer. The code in the PHP file accesses the database, converts the result into JSONP format and forward it to the frontend developer.
I don't have any knowledge of Magento's MVC. Is this task of conversion similar to the modules in the app/code/core/Mage in the Magento folder?? How can I do this? Is the magento MVC the same as the PHP MVC?
I am including the php file that I need to convert to Magento MVC. So it will be easier for you to understand..
<?php header('content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost", "db_username", "password", "db_dbname");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$pid = $_REQUEST['prodid'];
/* Select queries return a resultset */
$result = mysqli_query($link, "SELECT round(rating_summary / 20) AS search_rating FROM review_entity_summary where store_id = 1 and entity_pk_value=" . $pid);
// printf("Select returned %d rows.\n" . "<br>\n", mysqli_num_rows($result)) . "<br>\n";
//$string = $_REQUEST['varname'];
$rows = array();
/* while ($row = $result->fetch_row()) {
printf("%s\n", $row[0]);
}*/
//while($r = mysql_fetch_assoc($result)) {
while ($r = mysqli_fetch_assoc($result)) {
$rows = $r;
//print_r ($rows) . "<br>\n";
}
$json_data = json_encode($rows);
print_r ($json_data);
/* free result set */
// mysqli_free_result($result)
mysqli_close($link);
?>
So how can I convert this file to Magento's MVC style?? IS this necessary to convert this file to magento MVC?
I think what they are asking you to do, is to convert code that look like
require_once 'path/to/magento'. "/Mage.php";
umask(0);
Mage::app("default");
....
In to Magento MVC (module)
\app\code\local\MyNamespace
If you're new to OOP, take a look here: http://www.php.net/manual/en/language.namespaces.rationale.php
\app\code\local\MyNamespace\Appname
Name of new custom module - try to keep at least first letter capital, or there WILL BE truble with Magento's understanding
\app\code\local\MyNamespace\Appname\Block
In classic MVC architecture, this represents View part of MVC
\app\code\local\MyNamespace\Appname\controllers
This is fairly easy to understand, if not, have fun: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
\app\code\local\MyNamespace\Appname \etc
Contains the most significant part in Magento's MVC architecture - the xml field that will connect all things together
\app\code\local\MyNamespace\Appname\Helper
Intended for files that contain repeatable routines or simple procedural methods
\app\code\local\MyNamespace\Appname\Model
Same thing as for controller, take a look at the link above
\app\code\local\MyNamespace\Appname\sql
This was interesting thing to find out what's it for, it's to define custom database tables and process any upgrades to your extension.
\etc\modules
Contains all Modules included in Magento - here's where it all really begins for our module
see http://inchoo.net/ecommerce/magento/basic-folder-structure-for-new-magento-module/
Interesting question. IMHO, you are in the position I was with regard to Magento some time ago. To write and use mySql queries against the Magento database requires nothing from the MVC model. In our world, I call these voodoo files. I create tools for my staff with very specific purpose and non-published URLs. You can put them anywhere in the file system, just make sure that the directory name and file name do not have any magic meaning within Magento or you might inadvertently disable some function. My sales tax reporting tool is exactly like yours, where you open a connection to the mySql database and make direct, manual queries against it.
RS's answer is a great primer for getting into the MVC. I have started using it for my voodoo tools as well. Making queries the "Magento way" via their controllers can make getting information out of the EAV much easier than trying to put together the info via manual queries because the infrastructure is already coded for you. An example is customer information! I figured out sales tax queries (which was no minor undertaking), but getting a complete name, address, and email was absurd.
I created an export tool which outputs JSON data. I then CURL it from the server at work to store the sales into a mysql database. From there, they get imported into quickbooks. This order export tool is 100% Magento objects like from the example below.
Here is the code I used in my voodoo customer info tool:
// this limits access to the page to this IP address, perhaps your office?
$remote_host_ip='198.75.43.24';
if( $_SERVER['REMOTE_ADDR'] != $remote_host_ip ){
$error[ date("Y-m-d_H:i:s") ]="\$_SERVER['REMOTE_ADDR'] is ".$_SERVER['REMOTE_ADDR']." which is not the office ip! $remote_host_ip\n";
}
// from: http://fishpig.co.uk/blog/direct-sql-queries-magento.html
require_once '/home/(your host username)/public_html/app/Mage.php';
// from: http://stackoverflow.com/questions/7145373/magento-fatal-error-call-to-a-member-function-getmodelinstance-on-a-non-obje/7145570#7145570
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
// from: http://www.magentocommerce.com/boards/viewthread/297092/
$_dealers = Mage::getModel('customer/customer')
->getCollection()
->addFieldToFilter('group_id', array(6,5,3))
// ->addFieldToFilter('group_id', 6)
;
// print_r($_dealers);
$dealers=Array();
foreach( $_dealers as $id => $obj){
// $info=$obj->load();
// print_r($info);
// from: http://www.42droids.co.uk/magento-get-customer-and-address-details
$customerAddressId=$obj->getDefaultBilling();
$address = Mage::getModel('customer/address')->load($customerAddressId);
$_addr=$address->format();
$addr.=",".$address->telephone;
$addr.=",".$obj->email;
echo "$addr\n"; //var_dump($addr);
}
I do most of my learning by google, trial, and error; so please consider that "my way" is simply one that works for me and probably still isn't the best or easiest way. Also, I still don't completely understand how some of the objects work, hence debug output that's commented out.
My advice would be to not try too hard to make something for the "frontend" facing the customer. Getting pages to display and work correctly in Magento is a miserable experience. If you're able to do sneaky hidden voodoo tools, try to do that. If you're forced to make a module, there are a couple dozen step by step how-to's on the net that can guide you.
I have, what I would think, would be a very easy conversion....I have a database that has 1 table with 4 fields....I query the table to fill the webpage....I need to convert this PHP to .aspx I've looked and it seems that most of what I find is much more advanced than i need. I know NOTHING about asp. Thank you very much. Here is what I have:
<?php
include("config.php");
$result = mysql_query("SELECT * FROM TestMeas ORDER BY name ASC",$connect);
while($myrow = mysql_fetch_array($result))
{
?>
<?php
echo "".$myrow['name']. ""; ?>
<?php echo $myrow['desc'];
}
?>
For starters, there is a big difference between ASP and ASP.net. In very simple terms, ASP is a scripting language whereas ASP.net is an application framework. Since ASP and PHP are both scripting languages, the usage is fairly similar. ASP.net is a whole different beast with almost no similarity.
Given the differences between working in a scripting language and working within the structure of the .Net framework, it's difficult to say "just do XYZ to convert your PHP to .Net". The .Net framework is largely built around separating presentation from content, which is not a focus in PHP unless you use a framework like CakePHP or CodeIgniter (or others).
Hopefully the linked tutorial in the comments got you on your way. There is also an MSDN article with a simple example of connecting to SQL Server.
Since this appears to be a "one-off" favor for a friend, your simplest option is probably to build a string in the code and pass it to your view.
string output = "";
using ( SqlConnection conn = new SqlConnection( connectionString ) )
{
conn.Open();
using( SqlCommand command = new SqlCommand( "SELECT url, name FROM TestMeas ORDER BY name ASC",
conn ) )
{
using( SqlDataReader reader = command.ExecuteReader() )
{
if( reader.HasRows )
{
while ( reader.Read() )
{
output += String.Format( "{1}",
reader[ 0 ],
reader[ 1 ] );
}
}
reader.Close();
}
}
conn.Close();
}
// Do something with output
You could do this in a code-behind, you could do it in a Controller if you're using MVC, you could do it right in the form/view/page if you want.