Thread: Print friendly

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Print friendly


    Hello all, I have a script which is supposed to print a row and for some reason doesn't work. the mysql connection works but it does not print the data.I've been trying to find what is wrong with it but I can't find it.Can somebody more experienced than me help me out to find what's wrong with this code?
    printer_friendly.php
    PHP Code:
    <?php
        
    /* Load Config File */
        
    require_once("./dbconf.php");

        
    // Global registers by pass
        
    require_once("./global.inc.php");
        
    pt_register('GET','id');

        
    $id++;
        
    $html_output "<font face=\"Verdana, Arial\" size=\"2\">\n";

        if (!isset(
    $id) || empty($id)) {
            echo 
    "Invalid Record Id Number";
        } else {
            if (
    $file_db && !$db) {
                
    $filedb fopen("data.dat""r") or die ("<br>Could not open data file to read.");
                
    $records = array();

                while (!
    feof($filedb)) {
                    
    $temp fgets($filedb1024);
                    if (!empty(
    $temp)) {
                        
    $records[] = $temp;
                    }
                }

                
    $columns explode("|"$records[0]);
                
    $id_records explode("|"$records[$id]);

                
    $html_output .= "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" class=\"printer_friendly\">\n";

                for (
    $x=0;$x<count($columns);$x++) {
                    
    $html_output .= "<tr><td align=\"left\" valign=\"middle\">" $columns[$x] . ":</td>\n";
                    
    $html_output .= "<td align=\"left\" valign=\"middle\">" $id_records[$x] . "</td></tr>\n";
                }
                
    $html_output .= "</table>\n\n";
            } elseif (!
    $file_db && $db) {

                
    $mySQL_link = @mysql_connect($hostname,$username,$password);

                if (
    $mySQL_link) {
                    
    mysql_select_db($dbname$mySQL_link);
                    
    $mySQL_query "SELECT * FROM " $table;

                    
    $resultset mysql_query($mySQL_query);

                    for (
    $i=0$i mysql_num_fields($resultset); $i++) {
                        
    $meta mysql_fetch_field($resultset);
                        
    $columns[$i] = $meta->name;
                    }

                    
    $results = array();

                    while(
    $row mysql_fetch_array($resultset)) {
                        
    $results[] = $row;
                    }

                    
    $html_output .= "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" class=\"printer_friendly\">\n";

                    for (
    $i=0$i count($columns); $i++) {
                        
    $html_output .= "<tr><td align=\"left\" valign=\"middle\">" $columns[$i] . ":</td>\n";
                        
    $html_output .= "<td align=\"left\" valign=\"middle\">" $results[($id 2)][$columns[$i]] . "</td></tr>\n";
                    }
                    
    $html_output .= "</table>\n\n";

                    
    mysql_close($mySQL_link);
                } else {
                    
    $html_output .= "Unable to connect to mySQL Database";
                }

            }
        }

        
    $html_output .= "</font>\n";
    ?>
    <html>
    <head>
    <title>MLATEL Contact Sheet</title>
    <meta http-equiv=Content-Type content="text/html; charset=windows-1252">
    <style>
    .printer_friendly {
        font-size: 12px;
        color: #000000;
        font-family: Verdana, Arial;
    }
    </style>
    <script language="javascript">
      window.print();
    </script>
    </head>
    <body>

    <?php
        
    echo $html_output;
    ?>

    </body>
    </html>
    Here is the global.inc.php code:
    PHP Code:
    <?php

    function pt_register()
    {
      
    $num_args func_num_args();
       
    $vars = array();

       if (
    $num_args >= 2) {
           
    $method strtoupper(func_get_arg(0));

           if ((
    $method != 'SESSION') && ($method != 'GET') && ($method != 'POST') && ($method != 'SERVER') && ($method != 'COOKIE') && ($method != 'ENV')) {
               die(
    'The first argument of pt_register must be one of the following: GET, POST, SESSION, SERVER, COOKIE, or ENV');
         }

           
    $varname "HTTP_{$method}_VARS";
          global ${
    $varname};

           for (
    $i 1$i $num_args$i++) {
               
    $parameter func_get_arg($i);

               if (isset(${
    $varname}[$parameter])) {
                   global $
    $parameter;
                   $
    $parameter = ${$varname}[$parameter];
              }

           }

       } else {
           die(
    'You must specify at least two arguments');
       }

    }

    ?>
    and the dbconf.php (connection data have been changed of course)
    PHP Code:
    <?php
    $db
    =1;
    $hostname="localhost";
    $username "myuser";
    $password "mypass";
    $dbname "mydbname";
    $table "sales";
    define("HOST""$hostname"); // The host you want to connect to.
    define("USER""$username"); // The database username.
    define("PASSWORD""$password"); // The database password. 
    define("DATABASE""$dbname"); // The database name.
     
    $mysqli = new mysqli(HOSTUSERPASSWORDDATABASE);
    // If you are connecting via TCP/IP rather than a UNIX socket remember to add the port number as a parameter.
    ?>
    by the way the script comes from on open source form generator called phpFormGenerator from phpformgen.sourceforge.net which seems has no longer support.
    Last edited by mlatelcom; August 19th, 2013 at 08:38 AM. Reason: fixed
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Normally I would just fix the code but I'm tired so no.

    See how all the code in your post is on one line? Not at all readable that way. It's what happens when you click the PHP code button and paste your code into the dialog. Please go back to edit your post (button in the lower-right corner) and copy/paste your code into the post directly between the &amp;#91;php][/php] tags. Then Preview Post so you know it looks right. When that's done reply so we know we can come back and read it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    sorry about it


    yes, I was wondering why it was in one line code and thinking how the hell would someone have the patience to copy/paste it in a blank document and then try to understand the code with that mess. thank you for telling me how to fix it.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    1. mysql funcs are deprecated. use PDO
    2. font face? really we are not in 1990s
    3. try printing your SQL directly into browser & then run it directly into MYSQL to see if there is a query problem.
    4. you are doing:
    $results[($id - 2)][$columns[$i]] where is id coming from? and it stays static so maybe that is a problem?
    5. does your columns come out correct?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    hi paulh1983,
    I don't know mysql...I have to use phpmyadmin to manage my databases

    1.PDO?what is it?
    2.I saw that but leave like that, I'm not worry about the style right know, and yeah it seems to be an old script haha
    3.How do I do that?
    4.Ok this is the main file which calls printer_friendly.php and where "id" comes from. by the way Delete option works perfect. Only the print doesn't work properly.
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TITLE>Admin Portal</TITLE>
    </HEAD>
    <BODY bgColor=#eeeeee>
        <div id="logout"><a href="logout.php">Logout</a></div>
        <div style="clear: both;"><br /></div>
    <CENTER>
    <TABLE cellSpacing=0 cellPadding=2 width=400 bgColor=#ffcc00 border=0>
                                    <TBODY>
                                    <p class=small>

    <?php
      
    include("dbconf.php");

    /*  if ($db == 1) {
        echo "Using MySQL database info:<br><br>\n\n";
      } elseif ($file_db == 1) {
        echo "Using file-based database:<br><br>\n";
      } else {
        echo "Sorry. In order to use the administration portal you must use the MySQL or file-based database option.\n\n";
      }
    */
      
    if ($file_db == && $db != 1) {
        
    $filedb fopen("data.dat","r") or die ("<br>Could not open data file to read.");
        
    $columns_str fgets($filedb,4096);
        
    $columns explode("|",$columns_str);
        echo 
    "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" width=\"90%\">\n";
        echo 
    "<tr><td colspan=\"" . (sizeof($columns)+2) . "\">\n";
        echo 
    "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n";
        echo 
    "<tr><td class=\"small\" bgcolor=\"#000000\" align=\"center\" valign=\"middle\">\n";
        echo 
    "<font color=\"#ffffff\">Records Table</td></tr></table></td></tr><tr>\n";

        for (
    $i=0;$i<sizeof($columns);$i++) {
          echo 
    "<td class=\"small\"  align=\"center\" valign=\"middle\"><b>".$columns[$i]."</b></td>";
        }
        echo 
    "<td class=\"small\" align=\"center\" valign=\"middle\">Delete Record</td>";
        echo 
    "<td class=\"small\" align=\"center\" valign=\"middle\">Printer Friendly</td></tr>";

        
    $i=0;
        while (!
    feof($filedb)) {
          
    $temp fgets($filedb,4096);
          if (
    $temp != "") {
            
    $records[$i]=$temp;
            
    $i++;
          }
        }

        for(
    $j=0;$j<$i;$j++) {
          echo 
    "<tr>";
          
    $holder explode("|",$records[$j]);
          for (
    $k=0;$k<sizeof($holder);$k++) {
            echo 
    "<td class='small'><center>$holder[$k]</td>";
          }
          echo 
    "<td class='small'><center><a href='delete_file_rec.php?id=".$j."'>delete</a>";
          echo 
    "<td class='small'><center><a href=\"printer_friendly.php?id=" $j "\">Print</a></tr>";
        }
        echo 
    "</table>";
      } else if(
    $db==1) {
        
    // mySQL Table
        
    $db_con mysql_connect($hostname,$username$password) or die("Connetion to database failed!");
        
    mysql_select_db($dbname);
        
    $query "select * from ".$table;
        
    $result mysql_query($query);
        
    $i 0;

        while (
    $i mysql_num_fields($result)) {
          
    $meta mysql_fetch_field($result);
          
    $columns[$i] = $meta->name;
          
    $i++;
        }

        echo 
    "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" width=\"90%\">\n";
        echo 
    "<tr><td colspan=\"" . (sizeof($columns)+2) . "\">\n";
        echo 
    "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n";
        echo 
    "<tr><td class=\"small\" bgcolor=\"#000000\" align=\"center\" valign=\"middle\">\n";
        echo 
    "<font color=\"#ffffff\">Records Table</td></tr></table></td></tr><tr>\n";

        for(
    $i=1;$i<sizeof($columns);$i++) {
          echo 
    "<td class='small'><center><b>".$columns[$i]."</td>";
        }
        echo 
    "<td class='small'><center>Delete Record</td>\n";
        echo 
    "<td class=\"small\" align=\"center\" valign=\"middle\">Printer Friendly</td></tr>\n";

        
    $query "select * from ".$table;
        
    $result mysql_query($query);
        
    $j=0;

        while(
    $row=mysql_fetch_array($result)) {
          echo 
    "<tr>";

          for(
    $i=1;$i<sizeof($columns);$i++) {
            echo 
    "<td class='small'><center>".$row[$columns[$i]]."</td>";
          }

          
    $j=$row[$columns[0]];
          echo 
    "<td class='small'><center><a href='delete_track.php?id=".$j."'>delete</a></td>";
          echo 
    "<td class='small'><center><a href=\"printer_friendly.php?id=" $j "\">Print</a></tr>";
        }
        echo 
    "</table>";
      }

      
    // mySQL ends

    ?>

    <br /><br />
    </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
    </center></form>

    </body></html>
    5.This is how columns come out:

    id:
    Name:
    Lastname:
    eMail:
    Phone:
    Company:
    Message:
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    you won't like to hear it, but please throw away this script and stop copypasting stuff you found somewhere on the Internet.

    This stuff is terrible. It reminds me of the worst PHP garbage people wrote in the late 90s. I talking about:

    • no security at all
    • the worst spaghetti code I've seen in a while
    • ancient table layouts
    • ancient style attributes
    • HTML 4.0? WTF? This version has been superseded by HTML 4.01 in 1999(!)

    Either this code has actually been written in the 90s and never been updated since. Or the programmer has some serious issues with learning and keeping up to date.

    Either way, this is not good. An average school kid today would probably do a lot better. I see three options: Learn PHP and write your own code. Find a reputable application written by people who know what they're doing. Or hire someone.

    But copypasting random free code is usually the worst solution. You'll realize that most of it is plain, unfiltered garbage from people who really shouldn't be writing code. I know this sounds harsh. But that's how it is. Not everybody was made for programming.

    When you adopt those scripts, you'll easily spend more time fixing the bugs than you save by not having written the code.
    Last edited by Jacques1; August 19th, 2013 at 09:23 AM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    @Jacques1


    yeah I don't want to hear that, but it is what it is...so thank you for the advise. I'm learning php by myself; but I just started. So I 'm pretty much blind about security and other things with php. regarding html I just left it like that (just copy/pasted it)to get it to work and once it works then I could change tables by divs, replace all the obsolete code, add styles, etc.well...that's what I had in mind; but I guess I was wrong. I'll keep learning php and in the mean time I will find some decent script and drop this one. thank you very much for your help
    the worst spaghetti code I've seen in a while
    that was funny lol

IMN logo majestic logo threadwatch logo seochat tools logo