ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, when I was studing language C, I fount lots of help here.
And I am stuing PHP now.
I write a simple guestborad with PHP.
But, the "next","first",.....link can't work well.I don't know why.
So, If you have time, please read my code and solve the problem.
If you can give me some advice on my code style or method, I will very very thank you.
Code:
<html>
<head>
<title>Welcome to my home page, I am Alf</title>
<http-equv="refresh" Content="5;url=leavemassage.php">
</head>
<body bgcolor="black" text="white">
//PHP code start
<?php
//This is the function to check the text whether or not have some bad words
function change($strtext)
{
$word=explode($strtext," ");
$i=0;
while($word[$i])
{
ereg_replace("<", "less than", $word[$i]);
ereg_replace(">", "more than", $word[$i]);
$i++;
}
$strtext=implode($word," ");
return $strtext;
}//This funtion is not perfect now.
$foo_name=$_GET["name"];
$foo_deta=$_GET["deta"];
$fo_name=change($foo_name);
$fo_deta=change($foo_deta);
//connect to the mysql and select database
$ln=mysql_connect("haalcfk","myhtml","myhtml");
mysql_select_db("myhtml");
//If the name and deta are not empty, insert the message to mysql
if(!empty($fo_name) && !empty($fo_deta))
{
$fo_date=Date("Y-m-j H:i:s");
$insert="insert into message (name,date,deta) values('$foo_name','$fo_date','$foo_deta')";
mysql_query($insert);
}
echo "<h1 align='center'><i>Here is the every message</i></h1>";
echo "<table border=1 width='60%' align='center'>";
$results=mysql_query("select * from message order by date desc");
//display the results page by page
$perpage=3;
$num=mysql_num_rows($results);
$pages=intval(($num-1)/$perpage) + 1;
//check the page number
if(ifset($POST["p"]))$page=$_POST["p"];
else $page=0;
@mysql_data_seek($results,$num+$perpage*$page);
$i=$page*$perpage;
if($page == $pages)$max=$num-$perpage*($pages-1);
else $max=$perpage;
for($k=0;$k<$max;++$k)
{
$row=mysql_fetch_array($results);
if(!$row)break;
$name=$row["name"];
$date=$row["date"];
$detail=$row["deta"];
$reply=$row["repl"];
$i++;
echo "<tr>";
echo "<td width='5%' rowspan=3 align='center' valign='middle'>$i</td>";
//Display the every message
echo "<td width='5%'>name:</td>";
echo "<td width='45%'>$name</td>";
echo "<td width='5%'>time:</td>";
echo "<td width='40%'>$date</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='5%'>detail:</td>";
echo "<td width='90%' colspan=3>$detail</td>";
echo "</tr>";
echo "<tr>";
echo "<td width='5%'>replay:</td>";
echo "<td width='90%' colspan=3>$reply</td>";
echo "</tr>";
}
mysql_close($ln);
echo "There are $num records, display $page*$perpage to ($page+1)*$perpage<br>";
//This is the table to link to first, last, etc.
echo "<table width='60%' align='center' cellspacing=8 cellpadding=0 border=1>";
echo "<tr>";
//If the first page, then no link to first and pre....
if($page == 0)
{
echo "<td width='25%' align='center'><<--</td>";
echo "<td width='25%' align='center'>-<-</td>";
}
//If not the first page
else
{
$fir=0;
$pre=$page-1;
echo "<td width='25%' align='center'><a href='leavemessage.php?p=$fir'><<--</a></td>";
echo "<td width='25%' align='center'><a href='leavemessage.php?p=$pre'>-<-</a></td>";
}
//If the last page, then no link to last and n......
if($page == $pages)
{
echo "<td width='25%' align='center'>->-</td>";
echo "<td width='25%' align='center'>-->></td>";
}
//If not the last page
else
{
$nex=$page+1;
$las=$pages;
echo "<td width='25%' align='center'><a href=leavemessage.php?p=$nex>->-</a></td>";
echo "<td width='25%' align='center'><a href=leavemessage.php?p=$las>-->></a></td>";
}
echo "</tr>";
echo "</table>";
?>//PHP code end
<br>
<br>
//This is the place for writing text.
<h1 align="center"><i>Please leave your message here</i></h1>
<form action="leavemessage.php" method="get">
<table width="50%" align="center" cellspacing="8" cellpadding="0">
<tr>
<td width="40%">Your name:</td>
<td><input taye="text" name="name" size="32" maxlength="64"></td>
</tr>
<tr>
<td valign="middle">Your message's detail:</td>
<td><textarea name="deta" rows=8 cols=32 wrap=hard></textarea></td>
</tr>
<tr>
<td align="center"><input type="submit" value="Submit"></td>
<td align="center"><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
</html>
//check the page number
if(ifset($POST["p"]))$page=$_POST["p"];
Yet you send it as GET here:
Quote:
Originally Posted by naihe2010
//If not the first page
else
{
$fir=0;
$pre=$page-1;
echo "<td width='25%' align='center'><a href='leavemessage.php?p=$fir'><<--</a></td>";
echo "<td width='25%' align='center'><a href='leavemessage.php?p=$pre'>-<-</a></td>";
}
//If the last page, then no link to last and n......
if($page == $pages)
{
echo "<td width='25%' align='center'>->-</td>";
echo "<td width='25%' align='center'>-->></td>";
}
//If not the last page
else
{
$nex=$page+1;
$las=$pages;
echo "<td width='25%' align='center'><a href=leavemessage.php?p=$nex>->-</a></td>";
echo "<td width='25%' align='center'><a href=leavemessage.php?p=$las>-->></a></td>";
}
So you should either use $_GET['p'] or even better $_REQUEST['p']
Don't worry about doing those ereg_replace() calls... just use the htmlentities() function, and it will replace all those < > signs with the correct HTML code to display them properly in the browser (ie > etc).
(You may still want to scan for other kinds of bad words, of course; that's up to you)
Code:
if(ifset($POST["p"]))$page=$_POST["p"];
You missed the underscore off the first $_POST, so it won't be recognised. That will definitely break your code.
However, as has also been said already, your form uses the "get" method, so you should be using $_GET instead of $_POST. Either that, or change the form to use the "post" method (definitely recommended for any form with a textarea!); just make sure they match. (The other alternative is to use $_REQUEST, as this combines $_POST and $_GET, so you can always be sure it'll be populated, whichever method your form uses).
Code:
?>//PHP code end
Your comments are outside the php tags, so they'll show up in the final HTML. Put the ?> on the next line after the comment.
Just one other thought -- if you're creating a new site these days, it's probably a good idea to try to make your HTML pages XML-compliant. It doesn't take much: Just make sure all your attribute values are in quotes -- eg <textarea name="deta" rows="8" cols="32" wrap="hard">, and any tags that don't have a closing pair should have a closing slash added -- eg replace <br> with <br />. It's no extra work, but it makes your code a bit more future-proof.
this won't help your problem, but it will save you time and make your code look better both in html and php. rather than repeated echo commands, replace that with:
this way of outputting html (forget the technical name) preserves newlines/tabs, and you can use double quotes around attribute values. PHP variables are also referenced, so you can use them throughout, which saves a ton of work!
the way you have it now, all of the html that is outputted from between your <?php ?> tags will output on 1 line of html, which is bad form
this won't help your problem, but it will save you time and make your code look better both in html and php. rather than repeated echo commands, replace that with:
this way of outputting html (forget the technical name) preserves newlines/tabs, and you can use double quotes around attribute values. PHP variables are also referenced, so you can use them throughout, which saves a ton of work!
the way you have it now, all of the html that is outputted from between your <?php ?> tags will output on 1 line of html, which is bad form
Actually, even this is unnecessary. Anything outside of <?php ?> tags is an automatic (implicit) echo:
In your version, the $warning_text variable will just be output as "$warning_text", whereas in airswit's version, it will be converted properly.
For your version to work, you'd need to write <?=$warning_text;?> instead; it another set of php tags for every variable to be printed. That's what airswit was trying to avoid.
On a side note I prefer to use templates to separate the code from the HTML mess. Much much cleaner. It's fairly easy to write a small templating system in PHP or even use a third party script. When you develop more complex PHP stuff, you'll need this convenience.
On a side note I prefer to use templates to separate the code from the HTML mess. Much much cleaner. It's fairly easy to write a small templating system in PHP or even use a third party script. When you develop more complex PHP stuff, you'll need this convenience.
I think your idea is perfect, and I saw lots of complex PHP stuff is developed with this method.
Could you please teach me the method clearly. I mean, can you take a examble? thank you.
I'm currently doing a CMS system in PHP/MySQL. I'm releasing it under GPL. You can find the templating code when I release it. You can also find a few tutorials on google on PHP templating.
By the way, there's a pre-existing template engine which you might want to use:
Smarty - http://smarty.php.net/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.