I created a form that collects a title and some content (as well as some other data), and would like to take that title and content and automatically create a link and a new and simple web page for it (think of Craigslist). So, in the end result, when other visitors have populated this table by filling out the form, new visitors see a list of links which are listed by title, select a link, and are then able to view the content. I would like to do this with PHP and MySQL. Can someone please help me get started?

SQL for title and content:

DROP TABLE IF EXISTS newcmsBlock;
CREATE TABLE newcmsBlock (
  title VARCHAR(50),
  content TEXT
  );

Form code:

<form action = "newbuildBlock.php"
            method = "post"> 


<p><input type="text" name="title" size="30" maxlength="30" /> <small>Title</small></p>
<p><textarea name="content" rows="20" cols="30"></textarea> <small>Content</small></p>

<input type="hidden" name="submitted" value="TRUE" />

The main questions I have are:

1) How do I dynamically create the link (and the new URL) with PHP and MySQL?

2) And how do I dynamically create the new webpage, containing the title and description (that were taken from the input of the form, on a previous page)?

Recommended Answers

All 5 Replies

1) How do I dynamically create the link (and the new URL) with PHP and MySQL?

//ex
$query= mysql_query("select url_fields,name from table_name");
while($row=mysql_fetch_array($query)){
echo '<a href="'.$row['url_fields'].'">'.$row['name'].'</a>'; //this will create dynamic link depend on how you style it.
}

2) And how do I dynamically create the new webpage, containing the title and description (that were taken from the input of the form, on a previous page)?
2 option

//first option
//page.php
<html>

<head>

<title><? echo $_POST['title']; ?></title>
</head>
<body>
<?php
$description=$_POST['Description'];
.
.
.
.
//and so on
?>
</body>
</html>

2nd option
http://www.tizag.com/phpT/filecreate.php or google php file create

Thanks for your help!!!! I'm close but now I have a problem with the link file (i.e., <a href="$filename"> - variable containing a concatenation of a name and a "." and "html") or in this case...When I dynamically create the link file the first time, it works just fine. But when I dynamically create the link the second time, I have a problem. I'm creating the HTML tags (i.e., <html><head></head><body></body></html> each time I dynamically create the link file. I'm guessing that I need a conditional statement in my code...

Here's what I have so far:

$output2 = <<< HERE
<html>
<head>
<title></title>
</head>
<body>
<p><a href="$filename">$title</a></p>
<br>
</body>
</html>
HERE;

//open file for output
$fp2 = fopen("Linkstuff.html", "a");//this is the file that contains the links
//write to the file
fwrite($fp2, $output2);
fclose($fp2);

This is the output (to the dynamically created link-file) on the first try:

<html>
<head>
<title></title>
</head>
<body>
<p><a href="widget15.html">FIRST TITLE</a></p>
<br>
</body>
</html>

This is the output (to the dynamically created link-file) on the second try:

<html>
<head>
<title></title>
</head>
<body>
<p><a href="widget15.html">FIRST TITLE</a></p>
<br>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<p><a href="widget16.html">SECOND TITLE</a></p>
<br>
</body>
</html>

THIS IS WHAT I WANT:

<html>
<head>
<title></title>
</head>
<body>
<p><a href="widget15.html">FIRST TITLE</a></p>
<br>
<p><a href="widget16.html">SECOND TITLE</a></p>
<br>
</body>
</html>

Can someone please help????

Thanks for your help. I was trying to dynamically create a link using the "heredoc" method, where I created the standard HTML tags (i.e., <html>, <head>, <body>, etc...) once with an output. Then I was going to use a "for" loop and then close that off with ?> and end with another "heredoc" section. I'm new to PHP so please excuse my ignorance...

I looked back at your code though and saw that it appears to do the same thing in a much cleaner way, but I'm having trouble understanding the functions....

Here's your code:

$query= mysql_query("select url_fields,name from table_name");
while($row=mysql_fetch_array($query)){
echo '<a href="'.$row['url_fields'].'">'.$row['name'].'</a>'; //this will create dynamic link depend on how you style it.}//ex

For the first line, you suggest:

$query= mysql_query("select url_fields,name from table_name");

I wrote:

$query3= mysql_query($query2,$con);

where

$query2 = SELECT * FROM newcmsBlock8 WHERE title = '$title';
HERE;

and

$con = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

(I'm confident I named the variables correctly here)


1) DO I HAVE THIS SECTION SET UP CORRECTLY?

On the second line, you suggest a "while" loop:

while($row=mysql_fetch_array($query)){

But I'm not sure about this...

I wrote:

while($row=mysql_fetch_array($query3,MYSQL_NUM)){

But, in a previous expression, I have "$row" defined as:

$result2 = mysql_query($query2,$con);
$row = mysql_fetch_row($result2);

2) DO I NEED TO CREATE A NEW VARIABLE (LIKE $ROW2)?

3) DOES THE MYSQL_FETCH_ARRAY FUNCTION REQUIRE 2 ARGUMENTS (I.E., "$QUERY3, MYSQL_NUM)?

<html>
<head>
<title></title>
</head>
<body>
<?php
$query2 = mysql_query("SELECT * FROM newcmsBlock8 WHERE title = '$title'");
wile($row1 = mysql_fetch_array($query2)){
 echo '<p><a href="'.$row1['filename'].'">'.$row1['title'].'</a></p><br>';
}
?>
</body>
</html>

it is better to define variable related to query or to statement and add comment for you easy to find it.

Thanks, Happytogether. It now works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.