Page 1 of 1

Turn a info into a URL

Posted: Wed Jun 21, 2006 2:22 pm
by bluelad
i am a new newbie so dont get upset with my naive questions..... i want to turn the info added by the users into a URL. Since i dont know what this "thing" would be called..can someone teel me what it is called and are there any snippets for them

Posted: Wed Jun 21, 2006 2:26 pm
by John Cartwright
I have no idea what you are asking. What does "turn the info added by the users into a url" mean exactly?

Moved to PHP-Code.

Posted: Wed Jun 21, 2006 2:29 pm
by Luke
Do you mean adding input from a form to the query string?

http://www.something.com/yourpage.php?f ... itted_data

If that's what you're asking, just change make this your form opening tage:

Code: Select all

<form method="get" action="blabla">

Posted: Wed Jun 21, 2006 2:31 pm
by Benjamin

Code: Select all

<form action="test.php" method="get">
<input type="text" name="firstname" value="" /> 
<input type="text" name="lastname" value="" />
</form>
test.php

Code: Select all

<?php
$firstname = $_GET['firstname'];
$lastname = $_GET['lastname'];

echo "Your name is $firstname, $lastname";
?>

Posted: Wed Jun 21, 2006 2:31 pm
by bluelad
soory for not explainig. My users enter links to web address and i want to convert it into an URL. How to do that is the qustion

Secondly when the links are added the latest link appears last. i want to change this so that the latest link appears on the front of the index page

Posted: Wed Jun 21, 2006 2:33 pm
by Benjamin
From phpbb code, I may have modified it slightly...

Code: Select all

function MakeClickable($String) {
  $String = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $String);
  $String = ' ' . $String;
  $String = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $String);
  $String = substr($String, 1);
  return($String);
}

Posted: Wed Jun 21, 2006 2:56 pm
by bluelad
I have 2 files---index.php(which fetches and outputs the queries) and insert.php(where users enter the links). Where am i supposed to enter that function---my best bet isindex.php.

Index.php

Code: Select all

<html>
<head><title>Cool Links</title>
</head>
<body>
 <?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);
?>


<table width="100%" align="center" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse" 

bordercolor="#808080">"
<tr>
<td width="4%" align="center"><b><font color="#FFB340">Id</font></b></td>
<td width="36%" align="center"><b><font color="#FFB340">URL</font></b></td
<td width="60%" align="center"><b><font color="#FFB340">Description</font></b></td>
</tr>
<?php
$sql = "select * from link";
$query = mysql_query($sql);

while($row=mysql_fetch_array($query))
{
?>
<tr>
<td align="center"><font color="#EBEBEB" size="2.5"><?=$row[0]?></font></td>
<td align="center"><font color="#EBEBEB" size="2.5"><?=$row[1]?></font></td>
<td align="left"><font color="#EBEBEB" size="2.5"><?=$row[2]?></font></td>
</tr>

<?php
}
?>

</table>
</body>
</html>
Insert.php

Code: Select all

<html>
<head><title>Cool links</title>
</head>
<body bgcolor="#0A5C8E" link="#EBEBEB" vlink="#EBEBEB" alink="#FFB340">

<?php
$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);
?>

<?php
$link_id = 0;
$link_name = '';
$link_desc = '';

if (isset($_POST['submit']))
{
    if ( isset($_POST['id']) && !empty($_POST['id']) )
    {
        $link_id = $_POST['id'];
    }

    if ( isset($_POST['name']) && !empty($_POST['name']) )
    {
        $link_name = $_POST['name'];
    }

    if ( isset($_POST['desc']) && !empty($_POST['desc']) )
    {
        $link_desc = $_POST['desc'];
    }

    $sql = "insert into link values ('', '$link_name', '$link_desc')";

    $query = mysql_query($sql) or die("Could not query the table: " . mysql_error());
}
?>


<form action="insert.php" method="post">
<table width="100%" align="center" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse" bordercolor="#808080">
<tr>
<td width="10%" align="center"><font color="#FFB340" size="2.5">URL</font></td>
<td  width="90%"><input type="text" name="name" value="" size="25"></td>
</tr>
<td width="10%" align="center"><font color="#FFB340" size="2.5">Description</font></td>
<td width="90%"><input type="description" name="desc" value="" size="65"></td>
</tr>
</table>
<br>
<div align="center"><input type="submit" name="submit" value="Submit"></div>
</form>
<br>
<div align="center"><a href="index.php"><font color="#FFB340" size="2.5">Back to Link</font></a> | <a href="http://127.0.0.1"><font color="#FFB340" size="2.5">Back to Forum</a>
</div> 


</body>
</html>

Posted: Wed Jun 21, 2006 2:59 pm
by Benjamin

Index.php

Code: Select all

<html>
<head><title>Cool Links</title>
</head>
<body>
 <?php
function MakeClickable($String) {
  $String = preg_replace('#(script|about|applet|activex|chrome):#is', "\\1:", $String);
  $String = ' ' . $String;
  $String = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*)#is", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $String);
  $String = preg_replace("#(^|[\n ])([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+)#i", "\\1<a href=\"mailto:\\2@\\3\">\\2@\\3</a>", $String);
  $String = substr($String, 1);
  return($String);
} 

$link=mysql_connect("localhost", "root", "");
$db=mysql_select_db("chetan_db", $link);
?>


<table width="100%" align="center" border="1" cellspacing="0" cellpadding="0" style="border-collapse:collapse" 

bordercolor="#808080">"
<tr>
<td width="4%" align="center"><b><font color="#FFB340">Id</font></b></td>
<td width="36%" align="center"><b><font color="#FFB340">URL</font></b></td
<td width="60%" align="center"><b><font color="#FFB340">Description</font></b></td>
</tr>
<?php
$sql = "select * from link";
$query = mysql_query($sql);

while($row=mysql_fetch_array($query))
{
?>
<tr>
<td align="center"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[0]); ?></font></td>
<td align="center"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[1]); ?></font></td>
<td align="left"><font color="#EBEBEB" size="2.5"><?php echo MakeClickable($row[2]); ?></font></td>
</tr>

<?php
}
?>

</table>
</body>
</html>

Posted: Wed Jun 21, 2006 3:06 pm
by bluelad
Thanks man.That worked fine. Also help with the other question.

Posted: Wed Jun 21, 2006 3:10 pm
by Benjamin
What is your database table structure? For example if you have an autoincrement field you could do..

Code: Select all

$sql = "select * from link order by `id` desc";
which would put the last posted link first.

Posted: Wed Jun 21, 2006 3:20 pm
by bluelad
thanks a lot . learnt a lot today