LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2006, 06:05 AM   #1
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Rep: Reputation: 15
Please help me solve the problem of my PHP code


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>
 
Old 03-06-2006, 06:30 AM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
You check for POST variable p here:
Quote:
Originally Posted by naihe2010
//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']
 
Old 03-06-2006, 06:50 AM   #3
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
I modified the POST to GET, but It can't display anything now.
So, I think is not the problem. I remembered the "php?some=something" is POST synax.
 
Old 03-06-2006, 07:45 AM   #4
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
No, that is the GET variable. myscript.php?var=value

POST is submitted form data when using the method="post".
 
Old 03-06-2006, 08:52 AM   #5
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
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 &gt; 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.

Hope that helps
 
Old 03-06-2006, 05:01 PM   #6
airswit
Member
 
Registered: Dec 2005
Distribution: Fedora 4
Posts: 89

Rep: Reputation: 15
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:
PHP Code:
echo <<< SOME_KEYWORD
  <tr>
    <td>Some html code</td>
  </tr>
  <div style="color:red">Warning: 
$warning_text</div>

SOME_KEYWORD;
more_php_stuff();
?> 
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

Last edited by airswit; 03-06-2006 at 05:03 PM.
 
Old 03-06-2006, 06:47 PM   #7
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by airswit
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:
PHP Code:
echo <<< SOME_KEYWORD
  <tr>
    <td>Some html code</td>
  </tr>
  <div style="color:red">Warning: 
$warning_text</div>

SOME_KEYWORD;
more_php_stuff();
?> 
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:
PHP Code:
<?php
some_php_stuff
();
?>
  <tr>
    <td>Some html code</td>
  </tr>
  <div style="color:red">Warning: $warning_text</div>

<?php
more_php_stuff
();
?>
 
Old 03-07-2006, 07:42 AM   #8
Spudley
Member
 
Registered: Mar 2003
Location: Berkshire, England.
Distribution: SuSE 10.0
Posts: 299

Rep: Reputation: 32
Spooon -- airswit's code was correct.

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.
 
Old 03-07-2006, 08:08 AM   #9
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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.
 
Old 03-07-2006, 04:49 PM   #10
naihe2010
Member
 
Registered: Oct 2005
Location: China
Distribution: ArchLinux
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Harishankar
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.
 
Old 03-07-2006, 07:39 PM   #11
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
merge ASP code with PHP code.. possible ?? ALInux Programming 7 12-30-2005 08:40 AM
a problem on my php code. babyboss Programming 12 08-16-2005 09:07 PM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM
PHP MySQL code problem pnh73 Programming 3 09-15-2003 01:01 AM
The only problem I couldn't solve... Bager Linux - Laptop and Netbook 2 07-21-2003 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration