-2

我有一个包含一些数据的 mysql 数据库。现在我想向大家展示 mysql 数据。带有编辑选项。

<?php

$username = "VKSolutions";

$password = "VKSolutions@1";

$hostname = "VKSolutions.hostedresource.com";

$database = "VKSolutions";

$dbhandle = mysql_connect($hostname, $username, $password)

 or die("Unable to connect to MySQL");

$selected = mysql_select_db($database,$dbhandle)

 or die("Could not select $database");

$query= 'SELECT * FROM customer_details';

$result = mysql_query($query)

or die ('Error in query');

echo '<table width=80% border=1 align="center">';

echo '<tr><td><b>ID</b></td><td width="20px"><b>Name</b></td><td width="25px">    <b>Telephone</b></td><td width="30px"><b>E-mail</b></td><td width="20px"><b>Country     Applying for</b></td><td width="20px"><b>Visa Categeory</b></td><td width="20px"><b>Other     Categeory</b></td><td width="20px"><b>Passport No</b></td><td width="50px">    <b>Remarks</b></td></tr>';

while ($row=mysql_fetch_row($result))

{

echo '<tr>';

echo '<td>'.$row[0].'</td>';

echo '<td>'.$row[1].'</td>';

echo '<td>'.$row[2].'</td>';

echo '<td>'.$row[3].'</td>';

echo '<td>'.$row[4].'</td>';

echo '<td>'.$row[5].'</td>';

echo '<td>'.$row[6].'</td>';

echo '<td>'.$row[7].'</td>';

echo '<td>'.$row[8].'</td>';

echo '</tr>';

}

echo '</table>';

mysql_free_result($result);

mysql_close($dbhandle);

?>

我需要对此代码进行一些修改,例如

1) MySql 数据单页显示。但不是我想要的。我需要显示具有多个页面的数据,例如(page1,page2,page3,.....page54 ...等)

2)并且当用户想要更改时还提供编辑选项。

谢谢。

4

2 回答 2

1

您需要将 LIMIT 添加到您的选择语句中。

如果您在这里阅读:http: //dev.mysql.com/doc/refman/5.0/en/select.html

因此,您可以将语句更改为: SELECT * FROM customer_details LIMIT 0,20

这将获得前 20 条记录,然后获得下一批结果: SELECT * FROM customer_details LIMIT 20,20

限制后的数字是,第一个是它仍然开始的地方,第二个是返回多少。

我希望这能为您指明正确的方向。

于 2013-06-25T06:37:22.870 回答
0

嘿,如果你想用页面显示数据,也可以使用分页,如果你想编辑值,那么你必须在这样的文本框中显示数据并通过表单发送

<?php

$username = "VKSolutions";

 $password = "VKSolutions@1";

 $hostname = "VKSolutions.hostedresource.com";

 $database = "VKSolutions";

 $dbhandle = mysql_connect($hostname, $username, $password)

   or die("Unable to connect to MySQL");

 $selected = mysql_select_db($database,$dbhandle)

 or die("Could not select $database");

 $query= 'SELECT * FROM customer_details';

 $result = mysql_query($query)

  or die ('Error in query');
?>

  <table width=80% border=1 align="center" id=pag>

<tr><td><b>ID</b></td><td width="20px"><b>Name</b></td><td width="25px"><b>Telephone</b></td><td width="30px">    <b>E-mail</b></td><td width="20px"><b>Country     Applying for</b></td><td width="20px"><b>Visa Categeory</b></td><td width="20px"><b>Other Categeory</b></td><td width="20px"><b>Passport No</b></td><td width="50px"><b>Remarks</b></td></tr>

<?php
while ($row=mysql_fetch_row($result))

{
?>
<tr>

<td><input type="text" name="newid" value="<?php echo $row[0];?>" /></td>
<td><input type="text" name="newname" value="<?php echo $row[1];?>" /></td>
<td><input type="text" name="aaaa" value="<?php echo $row[2];?>" /></td>
......

 </tr>
<?php
 }
?>
</table>
//pagination code
<div class="pagination" align="center" >
        <div id="pageNavPosition"></div>
      </div>
        <script type="text/javascript"><!--
    var pager = new Pager('pag',10); // pag is id of table and 10 is no of records you want to show in a page. you can change if you want
    pager.init(); 
    pager.showPageNav('pager', 'pageNavPosition'); 
    pager.showPage(1);
   //--></script>
 </div> 
<?php
mysql_free_result($result);

mysql_close($dbhandle);

?>

在头部添加这个。

<script type="text/javascript" src="paging.js"></script> /* adding javascript pagination source page */

paging.js 的代码在这里。只需将代码复制并粘贴到文件名 paging.js 中

    function Pager(tableName, itemsPerPage) {
    this.tableName = tableName;
    this.itemsPerPage = itemsPerPage;
    this.currentPage = 1;
    this.pages = 0;
    this.inited = false;

    this.showRecords = function(from, to) {        
        var rows = document.getElementById(tableName).rows;
        // i starts from 1 to skip table header row
        for (var i = 1; i < rows.length; i++) {
            if (i < from || i > to)  
                rows[i].style.display = 'none';
           else
                rows[i].style.display = '';
        }
    }

    this.showPage = function(pageNumber) {
        if (! this.inited) {
            alert("not inited");
            return;
        }

        var oldPageAnchor = document.getElementById('pg'+this.currentPage);
        oldPageAnchor.className = 'pg-normal';

        this.currentPage = pageNumber;
        var newPageAnchor = document.getElementById('pg'+this.currentPage);
        newPageAnchor.className = 'pg-selected';

        var from = (pageNumber - 1) * itemsPerPage + 1;
        var to = from + itemsPerPage - 1;
        this.showRecords(from, to);
    }   

    this.prev = function() {
        if (this.currentPage > 1)
            this.showPage(this.currentPage - 1);
    }

    this.next = function() {
        if (this.currentPage < this.pages) {
        this.showPage(this.currentPage + 1);
        }
    }                        

    this.init = function() {
        var rows = document.getElementById(tableName).rows;
        var records = (rows.length - 1); 
        this.pages = Math.ceil(records / itemsPerPage);
        this.inited = true;
    }

    this.showPageNav = function(pagerName, positionId) {
        if (! this.inited) {
            alert("not inited");
            return;
        }
        var element = document.getElementById(positionId);

        var pagerHtml = '<span onclick="' + pagerName + '.prev();" class="pg-normal"> &#171 Prev </span>   &nbsp;&nbsp; ';
        for (var page = 1; page <= this.pages; page++) 
            pagerHtml += '<span id="pg' + page + '" class="pg-normal" onclick="' + pagerName + '.showPage(' + page   + ');">' + page + '</span> &nbsp;&nbsp; ';
        pagerHtml += '<span onclick="'+pagerName+'.next();" class="pg-normal"> Next &#187;</span>';            

        element.innerHTML = pagerHtml;
    }
}
于 2013-06-25T07:57:46.560 回答