Jump to content

Need help - Simple PHP list


prawn_86

Recommended Posts

Hi all,

 

I am just starting out in the baffling world of PHP so go easy on me. What i want to do is pretty simple (I think).

 

I want to be able to have a form with 2 boxes 'title' and 'link' and when a user inputs the 2 the title is then displayed on the page hyperlinked to the address.

 

How would i go about this? Are there any free scripts for this?

 

I have started to set up the MYSQL database and have the 2 feild but dont know where to go from here.

 

 

Link to comment
Share on other sites

Here a simple version, Know you got to add the url as

 

full >>>http://www.google.co.uk/<<<

 

Need you to use preg_replace, If the url has

not got the correct begging of the web site name to add it .

 

your challenge it you accept, and no one else, go for it i am waiting.

 

<?php
if(isset($_POST['submit'])){

$title=trim($_POST['title']);

$url=trim($_POST['url']);

echo"<center> <a href='$url'>$title</a> </center>";

exit;

}

?>
<center>
<form method="POST" action=" ">
Please enter the title of the url
<br><br>
<input type="text" name="title">
<br><br>
Please add the url
<br><br>
<input type="text" name="url">
<br><br>
<input type="submit" name="submit" value="see the url woth your title">
</center>

Link to comment
Share on other sites

thansk redarrow,

 

it didnt work properly, but i think its a problem with my database, as im new to it all so i will got back to mySQL and have a look at that and let you know.

 

What if i wanted to have the link stay on the page and add more and more? would i need any other script?

 

thanks again

Link to comment
Share on other sites

Hi again all,

 

I have found a site which has something similar to what i want. I contacted the owner but they were not very helpful with regards to the PHP side of things.

 

Essentially i want the same feature as what happens at this site:

http://www.featureddomainsites.com/index.php

 

Any ideas/help out there? I have tried looking for a free script but have not had success so far.

Link to comment
Share on other sites

It sounds your on the right course.

 

You take the information from the form.  Put it into a database.  When the user views the page it gathers the info from the database and inserts it into the page.

 

To gather the info from the form:

HTML forms (if you set method="POST") will store their input in an array called $_POST.  The array is associative where each key is the name of the form field.  Meaning if you have two text fields, one called 'title' and one called 'url' then the array indexes $_POST['title'] and $_POST['url'] will hold the data from them respectively.

 

As for storing and returning the data, that's a little beyond the scope of a short post.  May I recommend W3Schools PHP Tutorial.  It's a good starter for not only basic PHP, but also MySQL.  Not bullet proof, but a good place to start.

Link to comment
Share on other sites

here's a *VERY* basic script to do what you want. I threw it together just for your needs, while leaving a lot of room for improvement (so you can learn).

 

script.php (or whatever you want to name it)

<?php
$database_user = "******"; //user for the database
$database_password = "******"; //password for the user listed above
$database_name = "******"; //the name of the database
$database_host = "localhost"; //You prolly won't need to change this one.

$link = mysql_connect($database_host, $database_user, $database_password);
mysql_select_db($database_name, $link);

//process any requests they have sent
if (isset($_GET['submit']) && $_GET['submit'] == true){
$title = mysql_real_escape_string($_POST['title']);
$url = mysql_real_escape_string($_POST['url']);
$sql = "INSERT INTO `test_table` VALUES('', '$title', '$url');";
mysql_query($sql);
}
?>
<html>
<center>
	<form method="POST" action="?submit=true">
		<table border="1">
			<tr>
				<td>Title:</td>
				<td><input type="text" name="title"></td>
			</tr>
			<tr>
				<td>URL:</td>
				<td><input type="text" name="url" value="http://"></td>
			</tr>
			<tr>
				<td> </td>
				<td><input type="submit" value="Submit"></td>
			</tr>
		</table>
	</form>
	<br />
<?php
//time to show you what you have in the database
$sql = "SELECT * FROM `test_table` ORDER BY `id` DESC LIMIT 100;";
$result = mysql_query($sql);
print "			<table border=\"1\">\n";
while ($row = mysql_fetch_assoc($result)){
$title = $row['title'];
$url = $row['url'];
	echo "				<tr>
				<td><a href=\"$url\" target=\"_blank\">$title</a></td>
			</tr>
";
}
print "			</table>\n";
?>
</center>
</html>
<?php
mysql_close($link);
?>

the database schema (layout):

CREATE TABLE IF NOT EXISTS `test_table` (
  `id` int(12) NOT NULL auto_increment,
  `title` varchar(200) NOT NULL,
  `url` varchar(256) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;

Link to comment
Share on other sites

That's because the browser is resending the data you input to the form when it refreshes.

 

You'll have to just compare all data going in against the database to see if it already exists.  This probably isn't a bad idea anyways incase someone wants to spam your site and get all his buddies to each send his url to you ten times.

Link to comment
Share on other sites

So im guessing what i have to do is say "if someone presses submit, take them to page X.php"

 

If thinking it will be in this part of the code:

 

//process any requests they have sent

if (isset($_GET['submit']) && $_GET['submit'] == true){

$title = mysql_real_escape_string($_POST['title']);

$url = mysql_real_escape_string($_POST['url']);

$sql = "INSERT INTO `test_table` VALUES('', '$title', '$url');";

mysql_query($sql);

}

 

But what command do i use?

Link to comment
Share on other sites

any help on the above post?

 

I know what i need to do, re: creating a new page etc, im just not sure of the command.  ???

 

Also, if i wanted tog et really technical and only allow X number of submissions per day per IP address how would i do that? Thats way beyond my scope of knowledge at the moment...

Link to comment
Share on other sites

you dont learn php this way you rely need to go throw a good tutoral as you no,

you opting out off the course really.

 

This takes a user to a page when submitted.

 

revamped code please look

<?php

if($_POST['submit']){

//add the values posting and mysql_real_escape_string() to protect database.

$sql1="select from what_ever where url='$url'";
$res1=mysql_query($res1)or die(mysql_error());

if(mysql_num_rows($res1)==0){

$sql="insert into whatever (a,b,c,url,number_of_times_user_posted) 
values('$a','$b','$c','$url,$number_of_times_user_posted')";
$res1=mysql_query($sql)or die(mysql_error());

}else{

$sql2="update what_ever set number_of_times_user_posted=number_of_times_user_posted+1 where url='$url'";
$res2=mysql_query($sql2)or die(mysql_error());

}

while($row=mysql_fetch_assoc($res1)){


// if the user with that ip has posted 5 times send them to a new page.
if($row['number_of_times_posted']=="5"){

	header('location: where_ever.php');
}
}

}

?>


 

 

 

 

 

Link to comment
Share on other sites

I have noticed that after a certain amount the initial parts of the list start disappearing off the page.

 

I have looked through the code, but cant find any part where its set as to how many can be displayed, so whats happening? Shouldnt it just keep displaying them until i set a parameter?

Link to comment
Share on other sites

Also, if i wanted tog et really technical and only allow X number of submissions per day per IP address how would i do that? Thats way beyond my scope of knowledge at the moment...

 

After thinking about this a bit further i think it might be better for me to just limit the amount of times each 'url' can be submitted, so i'll try and figure that out and see how i go :)

 

Thanks for all your help so far guys, i find i learn a lot better through stuff like this then just reading tuts.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.