Page 1 of 1

Calling a function via URL - possible?

Posted: Thu Feb 12, 2004 3:48 am
by hairyjim
Hi guys,

Is it possible to call a function via a URL?

I have the following code and what I want to be able to do is call the NewsList function via a URL.

The reason being I want to merge two php files I have created, one shows a listing of all news articles and another which shows the article detail. So the idea is when a user initially goes to the file it loads the NewsList function, and when they click on a record it will lod a ShowDetail function. (Not created yet!)

At the minute I am struggling to even call the ShowList function from the URL bar. Am I completly on the wrong track here?

Code: Select all

<?php

OpenConnection();

echo "this is a test line";

function OpenConnection() 
&#123;
	Global $dbconn;
	Global $result;
	$dbconn = mysql_connect("localhost", "user", "password");
	$result = mysql_select_db("database", $dbconn);
	if ( $result == false)
		&#123;
			echo mysql_error();
		&#125;
&#125;

function NewsList() 
&#123;
$sql = "Select sid, title, cdate from table_news order by cdate desc";
		$result = mysql_query( $sql  );
		if ( $result != false )
		&#123;
				while ( $data = mysql_fetch_assoc( $result ) )
				&#123;
					echo "<table width="80%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="70%"><b><a href="news_detail.php?id=".$data&#1111;'sid']."">".$data&#1111;'title']."</a></b></td>";
    				echo "<td width="30%">$data&#1111;cdate]</td>";
  					echo "</tr>";
					echo "</table>";
				&#125;
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;
	
?>

Posted: Thu Feb 12, 2004 6:23 am
by LonelyProgrammer
Quick sample

a.php

echo "<a href=\"b.php?action=showlist">";
b.php
[/quote]
$action = $_GET["action"];
if ($action=="showlist") Newslist();

This is the gist of it.
This is actually quite dangerous, for it allows people to edit the URL (if they have the address bar on) and call other functions directly.

Posted: Thu Feb 12, 2004 7:50 am
by hairyjim
Cheers Mate. I'll give it a go.

Soooo simple when someone tells you how ;)