php分层论坛风格菜单板

时间:2011-11-11 12:16:02

标签: php mysql menu hierarchical

我对如何做这件事感到有些困惑。我有一个列出数据库数据的主显示页面,然后我有一个插入表单的链接,一个编辑表单删除按钮,另一个用于评论。对于注释按钮,我需要转到一个空白表单,以便我可以将其添加为注释,以便它成为我点击的记录的子项。然后孩子应该有一个注释按钮,然后可以添加一个新的记录作为子子等。

问题是我被卡住了,所以任何帮助都会非常感激。

<!--PHP Update-->
<?php

$con = mysql_connect("localhost","root","");

if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("project1", $con);

if ($_GET['action']=='delete_ok'){

}elseif ($_GET['action']=='edit_ok'){

}elseif ($_GET['action']=='comment_ok'){

}


switch($_GET['action']){
case 'write_ok':

$error=false;

$msg='';

if($_POST['dateofbirth']=='' || !preg_match( "/^(19|20)\d\d[- \\.](0[1-9]|1[012])[-       \\.](0[1-9]|[12][0-9]|3[01])$/",$_POST['dateofbirth'])){
    $error=true;
    $msg.='Date of birth is required and in the correct format 0000.00.00\n';

}


    if($_POST['gender']==''){
    $error=true;
    $msg.='Gender is a required field\n';

}


if($_POST['title']==''){
     $error=true;
     $msg.='Please select your title\n';
}


if($_POST['firstname']==''){
     $error=true;
     $msg.='Please Enter your First Name\n';
}


if($_POST['lastname']==''){
     $error=true;
     $msg.='Please Enter your Last Name\n';
}


if($_POST['address1']==''){
     $error=true;
     $msg.='Please Enter the first line of your address\n';
}


if($_POST['city']==''){
     $error=true;
     $msg.='Please Enter your city of residence\n';
}


    if($_POST['postcode']==''|| !preg_match( "/^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$/",$_POST['postcode'])){
     $error=true;
     $msg.='Please Enter a Valid Postcode\n';
}


if($_POST['contactno']==''|| !preg_match( "/^((\+44\s?\d{4}|\(?\d{5}\)?)\s?\d{6})|((\+44\s?|0)7\d{3}\s?\d{6})$/",$_POST['contactno'])){
     $error=true;
     $msg.='Please Enter your Contact No\n';
}


if($_POST['email']==''|| !preg_match( "/^([a-z0-9])(([-.]|[]+)?([a-z0-9]+))(@)([a-z0-9])((([-]+)?([a-z0-9]+))?)*((.[a-z]{2,3})?(.[a-z]{2,6}))$/",$_POST['email'])){
     $error=true;
     $msg.='Please Enter a Valid Email Address\n';
}



if($error){

    echo '<script>alert("'.$msg.'");history.back()</script>';

}else{

$sql = "INSERT INTO project_data (Date_Of_Birth, Gender, Title, First_Name, Last_Name, Address_Line_1, Address_Line_2, City, Postcode, Contact_No, Email, Additional_Comment) VALUES ('".$_POST[dateofbirth]."','".$_POST[gender]."','".$_POST[title]."','".$_POST[firstname]."','".$_POST[lastname]."','".$_POST[address1]."','".$_POST[address2]."','".$_POST[city]."','".$_POST[postcode]."','".$_POST[contactno]."','".$_POST[email]."','".$_POST[note]."')";
    if(!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());
    }
    else{
        echo '<script>alert("Data Has Been Successfully Updated");</script>';
        echo '<meta http-equiv="Refresh" content="0;URL=pv.php">';
    }
}

break;
case 'edit_ok':
    $id = $_GET['id'];

    mysql_query("UPDATE project_data SET Date_Of_Birth='".$_POST[dateofbirth]."',Gender='".$_POST[gender]."',Title='".$_POST[title]."',First_Name='".$_POST[firstname]."',Last_Name='".$_POST[lastname]."',Address_Line_1='".$_POST[address1]."',Address_Line_2='".$_POST[address2]."',City='".$_POST[city]."',Postcode='".$_POST[postcode]."',Contact_No='".$_POST[contactno]."',Email='".$_POST[email]."',Additional_Comment='".$_POST[note]."' WHERE ID='".$_GET['id']. "'") or die ("Error in query: $query. " . mysql_error());

    echo '<script>alert("Data Has Been Successfully Updated");</script>';
    echo '<meta http-equiv="Refresh" content="0;URL=pv.php">';
break;
case 'delete_ok':
    #########Delete OK Start#################
    $id= $_GET['id'];
    $result = mysql_query("DELETE FROM project_data WHERE ID = '$id'") or die ("Error in query: $query. " . mysql_error());
    echo '<script>alert("Data Has Been Successfully Updated");</script>';
    echo '<meta http-equiv="Refresh" content="0;URL=pv.php">';
    #########Delete OK End #################
break;
case 'add_ok'; 

break;
    }
    ?>
   <?php
   $id=$_GET['id'];
   $result = mysql_query("SELECT * FROM project_data WHERE id='$id'");
   $row = mysql_fetch_array($result);
   ?>

   <form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo  $form_action ?>">
<fieldset>
    <legend></legend>
    <p>
        <label for="cname">Date Of Birth</label>  *
        <input id="cname" name="dateofbirth" class="required date"  value="    <?php echo $row['Date_Of_Birth']?>" />  (eg 1978.11.11)
    </p>
    <p>
        <label for="cgender">Gender</label>   *
        <input type="radio" name="gender" value="Male" <?php   if($row['Gender']=='male'){echo 'checked';}?>/> Male <input type="radio" name="gender" value="Female" <?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td>
    </p>
    <p>
        <label for="curl">Title</label>   *
        <select name="title"   id="title"  class="required">
            <option value="">Please Select</option>
            <option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option>
            <option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option>
            <option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option>
            <option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option>
            <option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option>
            </select>
    </p>
    <p>
        <label for="ccomment">First Name</label>     *
        <input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" />
    </p>


    <p>
    <label for="cemail">Last Name</label> *
        <input id="cemail" type="text" name="lastname" value="<?php echo $row['Last_Name']?>" maxlength="75" />
   </p>
    <p>
        <label for="ccomment">Address 1</label>*
        <input type="text" name="address1" value="<?php echo $row['Address_Line_1']?>" maxlength="50" />  
    </p>
    <p>
        <label for="ccomment">Address 2</label>
        <input type="text" name="address2" value="<?php echo $row['Address_Line_2']?>" maxlength="50" />
    </p>
    <p>
        <label for="ccomment">City</label>*
        <input type="text" name="city" value="<?php echo $row['City']?>"  maxlength="50" />
    </p>
    <p>
        <label for="ccomment">Postcode</label>*
        <input type="text" name="postcode" value="<?php echo $row['Postcode']?>" maxlength= "10" />  (eg LE5 5QE)
    </p>
    <p>
        <label for="ccomment">Contact No</label>*
        <input type="text" name="contactno" value="<?php echo $row['Contact_No']?>" maxlength= "12" />  (eg 077448825723)
    </p>
    <p>
        <label for="ccomment">Email</label>*
        <input type="text" name="email" value="<?php echo $row['Email']?>" maxlength= "40"/>  (eg info@example.com)
    </p>
    <p>
        <label for="ccomment">Comment</label>
        <textarea rows="10" cols="30" name="note"  maxlength= "500"><?php echo $row['Additional_Comment']?></textarea>
    </p>
    <p>
        <input class="submit" type="submit" value="Submit"/>
    </p>
    <p>
    <a href='pv.php'>Main Page</a>
   </p>

</fieldset>


<?php

//break;
default:


 ?>


 <style type="text/css">

 </style>


 <?php

    echo "<table border='1'>
    <tr bgcolor ='#FFFACD'>
    <th>ID</th>
    <th>Date Of Birth</th>
    <th>Gender</th>
    <th>Title</th>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Address Line 1</th>
    <th>Address Line 2</th>
    <th>City</th>
    <th>Postcode</th>
    <th>Contact No</th>
    <th>Email</th>
    <th>Additional Info</th>
    <th>Action</th>
    </tr>";


$tbl_name="project_data";       //your table name
// How many adjacent pages should be shown on each side?
$adjacents = 1;

/* 
   First get total number of rows in data table. 
   If you have a WHERE clause in your query, make sure you mirror it here.
*/
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

/* Setup vars for query. */
$targetpage = "pv.php";     //your file name  (the name of this file)
$limit = 3;                                 //how many items to show per page
$page = $_GET['page'];
if($page) 
    $start = ($page - 1) * $limit;          //first item to display on this page
else
    $start = 0;                             //if no page var is given, set start to 0

/* Get data. 

order by ?

group
parent
level



*/
$sql = "SELECT * FROM project_data LIMIT $start, $limit";
$result = mysql_query($sql);

/* Setup page vars for display. */
if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
$prev = $page - 1;                          //previous page is page - 1
$next = $page + 1;                          //next page is page + 1
$lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
$lpm1 = $lastpage - 1;                      //last page minus 1

/* 
    Now we apply our rules and draw the pagination object. 
    We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{   
    $pagination .= "<div class=\"pagination\">";
    //previous button
    if ($page > 1) 
        $pagination.= " <a href=\"$targetpage?page=$prev\">« previous</a> ";
    else
        $pagination.= " <span class=\"disabled\">« previous</span> ";   

    //pages 
    if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
    {   
        for ($counter = 1; $counter <= $lastpage; $counter++)
        {
            if ($counter == $page)
                $pagination.= " <span class=\"current\">$counter</span >";
            else
                $pagination.= " <a href=\"$targetpage?page=$counter\">$counter</a> ";                   
        }
    }
    elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
    {
        //close to beginning; only hide later pages
        if($page < 1 + ($adjacents * 2))        
        {
            for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
            {
                if ($counter == $page)
                    $pagination.= " <span class=\"current\">$counter</span> ";
                else
                    $pagination.= " <a href=\"$targetpage?page=$counter\">$counter</a> ";                   
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //in middle; hide some front and some back
        elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
            $pagination.= "...";
            $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
            $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
        }
        //close to end; only hide early pages
        else
        {
            $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
            $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
            $pagination.= "...";
            for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
    }

    //next button
    if ($page < $counter - 1) 
        $pagination.= "<a href=\"$targetpage?page=$next\">next »</a>";
    else
        $pagination.= "<span class=\"disabled\">next »</span>";
    $pagination.= "</div>\n";       
}


//$result = mysql_query("SELECT * FROM $tbl_name");
    while($row = mysql_fetch_array($result)){
      echo "<tr>";
      echo "<td>" . $row['ID'] . "</td>";
      echo "<td>" . $row['Date_Of_Birth'] . "</td>";
      echo "<td>" . $row['Gender'] . "</td>";
      echo "<td>" . $row['Title'] . "</td>";
      echo "<td>" . $row['First_Name'] . "</td>";
      echo "<td>" . $row['Last_Name'] . "</td>";
      echo "<td>" . $row['Address_Line_1'] . "</td>";
      echo "<td>" . $row['Address_Line_2'] . "</td>";
      echo "<td>" . $row['City'] . "</td>";
      echo "<td>" . $row['Postcode'] . "</td>";
      echo "<td>" . $row['Contact_No'] . "</td>";
      echo "<td>" . $row['Email'] . "</td>";
      echo "<td>" . $row['Additional_Comment'] . "</td>";
      echo "<td><a href='pv.php?action=edit&id=" . $row['ID']."'>Edit</a>&nbsp&nbsp<a href='pv.php?action=delete_ok&id=" . $row['ID']."'>Delete</a>&nbsp&nbsp<a href='pv.php?action=add&id=" . $row['ID']."'>Comment</a></td>";
      echo "</tr>";
      }
    echo "</table>";

    echo $pagination;

    echo "<a href='pv.php?action=write'>Insert</a>";



 }

 mysql_close($con);

 ?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

也许不完美,但我会订购类似的东西:

数据建模:

|pages ([idPage], ...)| 0..n
    1,1 |mainComments ([idPage, idMainComment], ...)| 0..n
        1,1 |secondaryComments ([idPage, idMainComment, idSecondaryComment], ...)|

<强> PHP:

$query = mysql_query('SELECT * FROM mainComments WHERE idPage = 12');

while ($data = mysql_fetch_assoc($query)) {
    //echo mainComments
    //echo forms for each

    $query2 = mysql_query('SELECT * FROM secondaryComments
                               WHERE idPage = 12
                               AND idMainComment = '.$data['idMainComment']);

    while ($data2 = mysql_fetch_assoc($query2)) {
        //echo secondaryComments
        //echo forms for each
    }
}