View Single Post
Old 10-09-2013, 12:08 PM   #3
jmichae3
 
Join Date: Dec 2010
Posts: 366
Default

PHP Code:
<?php require_once('Connections/tlsc_conn.php');
 
mysql_select_db($database_tlsc_conn$tlsc_conn);

function 
CalcPageCount($pageSize,$tableRowCount) {
    return 
intval($tableRowCount/$pageSize);
}
function 
CalcPageOffsetpn($pageNum) {
    return 
$pageNum-1;
}
function 
CalcPageOffsetpnps($pageSize,$tableRowCount,$pageNum) {
    return (
$pageNum $pageSize) % $pageSize// % is modulus (remainder of division)
}
function 
CalcPageLimit($pageSize) {
    return 
$pageSize;
}
function 
GetNumTableRows($dblink) {
    global 
$tbl_name;
    
$result=0;
    
$recordset mysql_query("
    SELECT 
        COUNT(*) AS c 
    FROM 
        
$tbl_name
    "
$dblink);
    if (
$recordset) {
        
$row=mysql_fetch_assoc($recordset);
        
$result=intval($row['c']);
    }
    
mysql_free_result($recordset); //always free when done!
}
function 
DoUpdate($dblink) {
    
//update a paged set of email records in bulk
    
global $pageNum$pageSize$tableRowCount,$pageCount;
    if(isset(
$_POST['submit'])) {
        
$query_Recordset1 "
            SELECT 
                * 
            FROM 
                tbl_name 
            LIMIT 
                '"
.CalcPageLimit($tableRowCount)."'
            OFFSET 
                '"
.CalcPageOffsetpnps($pageSize,$tableRowCount,$pageNum)."'
            "
;
        
$Recordset1 mysql_query($query_Recordset1$tlsc_conn) or die(mysql_error());
        
$totalRows_Recordset1 mysql_num_rows($Recordset1);
        

        
$count count($_POST['id']); //why are you alternating between POST and GET? are you using both?
        
$idArray $_GET['id'];

        while (
$row_Recordset1 mysql_fetch_assoc($Recordset1)) {
            
$row_Recordset1=mysql_query("
            UPDATE 
                
$tbl_name
            SET
                name='" 
mysql_real_escape_string($name[$i]) . "',
                lastname='" 
mysql_real_escape_string($lastname[$i]) . "',
                email='" 
mysql_real_escape_string($email[$i]) . "'
            WHERE 
                id='"
.$id[$i]."'
            "
,$dblink);
        }
        
mysql_free_result($query_Recordset1); //always free when done with query or you get scrambled results!
        
if($totalRows_Recordset1>0){ 
            
//is what you want? goto lulu if an update was done(totalRows_Recordset1>0)? 
            //or you want to goto lulu if any records in table ($tableRowCount>0)?
            
header("location:lulu.php");
            exit;
        }  
    }
}

function 
DoShowPageNavigation() {
    global 
$pageNum$pageSize$tableRowCount$pageCount;
    
//this is rudimentary. I personally would display it as an inline list, 
    //that's easily changeable with css. see o'reilly css cookbook page 675 or google w3c css selectors
    
echo "<ul class='pageList'>\n";
    for (
$pn=1$pn <= $pageCount$pn++) {
        echo 
"<li>" $pn "</li>\n";
    }
    echo 
"</ul>\n";
}
function 
DoShowForm($dblink) {
    global 
$pageNum$pageSize$tableRowCount$pageCount;
    if(isset(
$_POST['submit'])) {
        
$query_Recordset1 "
            SELECT 
                * 
            FROM 
                tbl_name 
            LIMIT 
                '"
.CalcPageLimit($pageSize,$tableRowCount)."'
            OFFSET 
                '"
.CalcPageOffset($pageSize,$tableRowCount)."'
            "
;
        
$Recordset1 mysql_query($query_Recordset1$tlsc_conn) or die(mysql_error());
        
$totalRows_Recordset1 mysql_num_rows($Recordset1);
        
?><!DOCTYPE html>
<html lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 <title>bulk admin email editing page</title>
<style>
ul.pageList {
    margin:0px;
    padding:0px;
}
ul.pageList li {
    display:inline;
}
ul.pageList li:nth-child(1):before {
    content:"Page [ ";
}
ul.pageList li:after {
    content:" | ";
}
ul.pageList li:last-child:after {
    content:" ]";
}
</style>
</head>
<body>
  <form name="form2" method="post" action="processform.php"><!--must have action to do anything !-->
  <table width="634" border="1">
<?php 
        $counter
=0;
        
$headerAboveEveryNRows=5;//header above every 5 rows
        
while ($row_Recordset1 mysql_fetch_assoc($Recordset1)) {
        
//displaying the header should be the first thing you do,somakew it the top thing in the while loop and first in count.
        
if (0==$counter headerAboveEveryNRows)  {
?>
    <tr>
       <td>id</td>
       <td>name</td>
       <td>lastname</td>
       <td>email</td>
    </tr>
<?php
        
}

?>
    <tr>
      <td><?php echo htmlentities(stripslashes($row_Recordset1['id'])); ?>
      <input name="id[]" type="hidden" value="<?php echo $row_Recordset1['id']; ?>">
      </td>
      <td><input name="name[]" type="text" value="<?php echo stripslashes($row_Recordset1['name']); ?>"></td>
      <td><input name="lastname[]" type="text" value="<?php echo stripslashes($row_Recordset1['lastname']); ?>"></td>
      <td><input name="email[]" type="text" value="<?php echo  stripslashes($row_Recordset1['email']); ?>"></td>
    </tr>
<?php 
        
//while
?>  
   </table>
    <p><input type="submit" name="submit" value="Submit"></p>
  </form>
<?php     DoShowPageNavigation(); ?>   
</body>
</html>
<?php
        mysql_free_result
($query_Recordset1); //always free when done with query or you get scrambled results!
    
}//if submit
}//func DoShowForm
    
$pageSize=20;//20 rows
if (isset($_GET['page'])) {
    
$pageNum=intval($_GET['page']);//1-based
} else {
    
$pageNum=1;//1-based
}
$TableRowCount=GetNumTableRows();
$pageCount=CalcPageCount($pageSize,$tableRowCount);
$pageOffset=CalcPageOffsetpnps($pageSize,$tableRowCount,$pageNum);//0-based. I *think* the SQL OFFSET is like a page number, but it may be a 0-based row number.

DoUpdate($tlsc_conn);
DoShowForm($tlsc_conn);  
?>
[/QUOTE]

it looks like you are doing bulk admin editing of email addresses and names,by the fact that you are doing [] on your input elements, a standard PHP way of doing arrays of elements.

I am going to add a "page" parameter/variable to $_GET.

several problems you are going to have here:

  • mysql API is deprecated. use mysqli or PDO::mysql.
  • be consistent about the way you close your void elements (singletons). if using XHTML or XML, use a / but if not don't.
  • validate HTML/XHTML by making this the URL for a bookmark,like in the linkx bar:
    Code:
    javascript:void(document.location='http://validator.w3.org/check?charset=%28detect+automatically%29doctype=Inline&ss=1&group=0&verbose=1&uri='+escape(document.location))
  • validate css by making this the URL for a bookmark, like in the links bar:
    Code:
    javascript:void(document.location='http://jigsaw.w3.org/css-validator/validator?profile=css3&usermedium=all&warning=1&lang=en&uri='+escape(document.location))
  • you are selecting the ENTIRE TABLE. this doesn't scale well. this could get REALLY HUGE and break the browser and make people upset with long loads and huge pages. page-ify it or something by using LIMIT nrows OFFSET nskip on the end of your SELECT statement. look it up in the mysql manual. http://dev.mysql.com/doc/refman/5.5/en/select.html this means you are also going to have to come up with your own page navigation system. or only update a portion of the table, like a specific ID. you could let them pick an ID to update, and have an edit.php link on that id which shows the row content. and that could be paged. also, this makes MUCH less inpact on the server when it's paged (which keeps your site from being flagged by hosting admins for "oh, you need more expensive hosting! you are using up too much resources!")
  • arrays don't expand too well within php douvble-quoted strings.
  • XHTML doesn't work with IE. use HTML 4.01 or HTML5.
  • assigning to $id[] may have the unintended consequences of appending to the array.
  • you are always better off starting fromthe beginning of the resultset and processing it whole or just using only the first row in the case of COUNT().
  • you didn't have a separate query for your form fields.
  • you don't have an action="processform.php" on your form element. this will result in a dud (non-working) form that's only good for DHTML, not PHP.
__________________
------------
Jim Michaels
HTML Code:
improperly<strong>nested<em>elements</strong>cause</em>
browser confusion (I believe the term is 'tag soup')!
jmichae3 is offline   Reply With Quote