p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Fixes required in code for userviewer.php (p.439) and common_db.inc (p.437)


Message #1 by "Arnold Matthews" <arnoldm@m...> on Wed, 17 Jan 2001 23:01:31 -0000
Everthing tested on Linux server and Win 2000 box using php 4.0.4



Browser: Opera:  program worked but 'view record' was inactive.  Loads of % signs for the spaces in the location bar of browser.



Browser: Netscape: program would start but not sort when clicking on a column heading.  Could make it sort by going to the location
bar in Netscape browser and deleting all the spaces manually in the long string there and then hitting enter.  'view record'
inactive.



Browser: IE5: gave errors and jumped out to debugger.



Fixes:  remove all spaces in line starting "NEW_WIN = window.open..." of html_header in common_db.inc.  Do the same in quite a few
places in userviewer.php.  Then everything ran perfectly in both environments on all browsers.



My edited files below:



<?php

$dbhost = 'localhost';

$dbusername = 'phpuser';

$dbuserpassword = 'phppass';

$default_dbname = 'sample_db';

$default_sort_order = 'ASC';

$default_order_by = 'usernumber';

$records_per_page = 5;

$user_tablename = 'user';

$access_log_tablename = 'access_log';

$MYSQL_ERRNO = '';

$MYSQL_ERROR = '';

$new_win_width = 600;

$new_win_height = 400;



function html_header() {

   global $new_win_width, $new_win_height;

   ?>

   <HTML>

   <HEAD>

   <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

   <!--

   function open_window(url) {

      var NEW_WIN = null;

      NEW_WIN = window.open ("", "RecordViewer","toolbar=no,width="+<?php echo $new_win_width ?>+",height="+<?php echo
$new_win_height?>+",directories=no,status=no,scrollbars=yes,resize=no,menubar=no");

      NEW_WIN.location.href = url;

   }

   //-->

   </SCRIPT>

   <TITLE>User Record Viewer</TITLE>

   </HEAD>

   <BODY>

   <?php

}



function html_footer() {

?>

</BODY>

</HTML>

<?php

}



function db_connect($dbname='') {

   global $dbhost, $dbusername, $dbuserpassword, $default_dbname;

   global $MYSQL_ERRNO, $MYSQL_ERROR;



   $link_id = mysql_connect($dbhost, $dbusername, $dbuserpassword);

   if(!$link_id) {

      $MYSQL_ERRNO = 0;

      $MYSQL_ERROR = "Connection failed to the host $dbhost.";

      return 0;

   }

   else if(empty($dbname) && !mysql_select_db($default_dbname)) {

      $MYSQL_ERRNO = mysql_errno();

      $MYSQL_ERROR = mysql_error();

      return 0;

   }

   else if(!empty($dbname) && !mysql_select_db($dbname)) {

      $MYSQL_ERRNO = mysql_errno();

      $MYSQL_ERROR = mysql_error();

      return 0;

   }

   else return $link_id;

}



function sql_error() {

   global $MYSQL_ERRNO, $MYSQL_ERROR;



   if(empty($MYSQL_ERROR)) {

      $MYSQL_ERRNO = mysql_errno();

      $MYSQL_ERROR = mysql_error();

   }

   return "$MYSQL_ERRNO: $MYSQL_ERROR";

}



function error_message($msg) {

   html_header();

   echo "<SCRIPT>alert(\"Error: $msg\");history.go(-1)</SCRIPT>";

   html_footer();

   exit;

}

?>





<?php

include "./common_db.inc";

function list_records() {

   global $default_dbname, $user_tablename;

   global $default_sort_order, $default_order_by, $records_per_page;

   global $sort_order, $order_by, $cur_page;

   global $PHP_SELF;

   

   $link_id = db_connect($default_dbname);

   if(!$link_id) error_message(sql_error());



   $query = "SELECT count(*) FROM $user_tablename";



   $result = mysql_query($query);

   if(!$result) error_message(sql_error());

      

   $query_data = mysql_fetch_row($result);

   $total_num_user = $query_data[0];

   if(!$total_num_user) error_message('No User Found!');

   $page_num = $cur_page + 1;



   $total_num_page = $last_page_num 

                   = ceil($total_num_user/$records_per_page);

   

   html_header();

   

   echo "<CENTER><H3>$total_num_user users found. Displaying the page

                     $page_num out of $last_page_num.</H3></CENTER>\n";

   

   if(empty($order_by)) {

      $order_by_str = "ORDER BY $default_order_by";

      $order_by = $default_order_by;

   }

   else $order_by_str = "ORDER BY $order_by";

   if(empty($sort_order)) {

      $sort_order_str = $org_sort_order = $default_sort_order;

      $sort_order = 'DESC';

   }

   else {

      $sort_order_str = $org_sort_order = $sort_order;

      if($sort_order == 'DESC') $sort_order = 'ASC';

      else $sort_order = 'DESC';

   }



   if(empty($cur_page)) {

      $cur_page = 0;

   }

     $limit_str = "LIMIT ". $cur_page * $records_per_page . 

                                     ", $records_per_page";

   $query = "SELECT usernumber, userid, username FROM $user_tablename $order_by_str $sort_order_str $limit_str";

   

   $result = mysql_query($query);   

   if(!$result) error_message(sql_error());

?>



<DIV ALIGN="CENTER">

<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">

   <TR>

      <TH WIDTH="25%" NOWRAP>

         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=usernumber"; ?>">

         User Number

         </A>

      </TH>

      <TH WIDTH="25%" NOWRAP>

         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=userid"; ?>">

         User ID

         </A>

      </TH>

      <TH WIDTH="25%" NOWRAP>

         <A HREF="<?php echo "$PHP_SELF?action=list_records&sort_order=$sort_order&order_by=username"; ?>">

            User Name

         </A>

      </TH>

      <TH WIDTH="25%" NOWRAP>Action</TH>

   </TR>

<?php



   while($query_data = mysql_fetch_array($result)) {

      $usernumber = $query_data["usernumber"];

      $userid = $query_data["userid"];

      $username = $query_data["username"];

      echo "<TR>\n";

      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$usernumber</TD>\n";

      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$userid</TD>\n";

      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">$username</TD>\n";

      echo "<TD WIDTH=\"25%\" ALIGN=\"CENTER\">

            <A HREF=\"javascript:open_window('$PHP_SELF?action=view_record&userid=$userid');\">View
Record</A></TD>\n";

      echo "</TR>\n";

   }

?>

</TABLE>

</DIV>

<?php      

   echo "<BR>\n";

   echo "<STRONG><CENTER>";

   if($page_num > 1) {

      $prev_page = $cur_page - 1;



      echo "<A
HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=0\">[Top]</A>";



      echo "<A
HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$prev_page\">[Prev]</A> ";

   }

   if($page_num <  $total_num_page) {

      $next_page = $cur_page + 1;

      $last_page = $total_num_page - 1;



      echo "<A
HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$next_page\">[Next]</A> ";



      echo "<A
HREF=\"$PHP_SELF?action=list_records&sort_order=$org_sort_order&order_by=$order_by&cur_page=$last_page\">[Bottom]</A>";

   }



   echo "</STRONG></CENTER>"; 

   html_footer();

}



function view_record() {

   global $default_dbname, $user_tablename, $access_log_tablename;

   global $userid;

   global $PHP_SELF;

   

   if(empty($userid)) error_message('Empty User ID!');

   

   $link_id = db_connect($default_dbname);

   

   if(!$link_id) error_message(sql_error());



   $query = "SELECT usernumber, userid, username, 

                    usercountry, useremail, userprofile, 

                    registerdate, lastaccesstime FROM $user_tablename

                    WHERE userid = '$userid'";

   $result = mysql_query($query);

   

   if(!$result) error_message(sql_error());

      

   $query_data = mysql_fetch_array($result);

   $usernumber = $query_data["usernumber"];

   $userid = $query_data["userid"];

   $username = $query_data["username"];

   $usercountry = $query_data["usercountry"];

   $useremail = $query_data["useremail"];

   $userprofile = $query_data["userprofile"];

   $registerdate = $query_data["registerdate"];



   $lastaccesstime = substr($query_data["lastaccesstime"], 0, 4) . '-' . 

            substr($query_data["lastaccesstime"], 4, 2) . '-' .

            substr($query_data["lastaccesstime"], 6, 2) . ' ' .

            substr($query_data["lastaccesstime"], 8, 2) . ':' .

            substr($query_data["lastaccesstime"], 10, 2) . ':' .

            substr($query_data["lastaccesstime"], 12, 2);



   html_header();

   echo "<CENTER><H3>

         Record for User No.$usernumber - $userid($username)

         </H3></CENTER>";



?>

<DIV ALIGN="CENTER">

<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">

   <TR>

      <TH WIDTH="40%">Country</TH>

      <TD WIDTH="60%"><?php echo $usercountry ?></TD>

   </TR>

   <TR>

      <TH WIDTH="40%">Email</TH>

      <TD WIDTH="60%"><?php echo "<A HREF=\"mailto:$useremail\">$useremail</A>"; ?></TD>

   </TR>

   <TR>

      <TH WIDTH="40%">Profile</TH>

      <TD WIDTH="60%"><?php echo $userprofile ?></TD>

   </TR>

   <TR>

      <TH WIDTH="40%">Register Date</TH>

      <TD WIDTH="60%"><?php echo $registerdate ?></TD>

   </TR>

   <TR>

      <TH WIDTH="40%">Last Access Time</TH>

      <TD WIDTH="60%"><?php echo $lastaccesstime ?></TD>

   </TR>

</TABLE>

</DIV>

<?php

   echo "<HR SIZE=\"2\" WIDTH=\"90%\">\n";

   $query = "SELECT page, visitcount, accessdate FROM $access_log_tablename

             WHERE userid = '$userid'";



   $result = mysql_query($query);

   if(!$result) error_message(sql_error());

   if(!mysql_num_rows($result))

      echo "<CENTER>No access log record for $userid ($username).</CENTER>";

   else {

      echo "<CENTER>Access log record(s) for $userid ($username).</CENTER>";

?>

<DIV ALIGN="CENTER">

<TABLE BORDER="1" WIDTH="90%" CELLPADDING="2">

   <TR>

      <TH WIDTH="40%" NOWRAP>Web Page</TH>

      <TH WIDTH="20%" NOWRAP>Visit Counts</TH>

      <TH WIDTH="40%" NOWRAP>Last Access Time</TH>

   </TR>

<?php 

      while($query_data = mysql_fetch_array($result)) {

         $page = $query_data["page"];

         $visitcount = $query_data["visitcount"];

         $accessdate = substr($query_data["accessdate"], 0, 4) . '-' .

                  substr($query_data["accessdate"], 4, 2) . '-' .

                  substr($query_data["accessdate"], 6, 2) . ' ' . 

                  substr($query_data["accessdate"], 8, 2) . ':' . 

                  substr($query_data["accessdate"], 10, 2) . ':' . 

                  substr($query_data["accessdate"], 12, 2);



         echo "<TR>\n";

         echo "<TD WIDTH=\"40%\">$page</TD>\n";

         echo "<TD WIDTH=\"20%\" ALIGN=\"CENTER\">$visitcount</TD>\n";

         echo "<TD WIDTH=\"40%\" ALIGN=\"CENTER\">$accessdate</TD>\n";

         echo "</TR>\n";

      }

?>

   </TR>

</TABLE>

</DIV>

<?php



   }

   

   html_footer();   

}



switch($action) {

   case "view_record":

      view_record();

   break;

   default: 

      list_records();

   break;

}

?>












  Return to Index