Jump to content

Barand

Moderators
  • Posts

    24,511
  • Joined

  • Last visited

  • Days Won

    819

Posts posted by Barand

  1. here's an example for you

    <?php
    
    $link1 = "<a class='LOOKUP' href='http://google.com'  target='_blank'>Site 1</a>";
    $link2 = "<a class='LOOKUP' href='http://yahoo.com'  target='_blank'>Site 2</a>";
    
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $().ready( function() {
            $(".LOOKUP").click( function(event) {
                event.preventDefault()
                var loc = $(this).attr("href")
                var linktext = $(this).html()
                $("#mytext").append(linktext + "\n")
                window.open(loc, '_blank');
            })
        })
    </script
    </head>
    <body>
        <?=$link1?>
        <br>
        <?=$link2?>
        <br>
        <h3>Visited links</h3>
        <textarea rows="5" cols="35" id="mytext" ></textarea>
    </body>
    </html>

     

  2. Around the turn of the century I had just discovered PHP and was currently using ASP with VB for intranet applications. One particular page enabled users to view log entries each day. This had worked fine for months when I received a complaint from one of the users that it was running very slowly. I tried it myself and, sure enough, it took about 15 seconds to display the data.
    Examining the entries in the SQL Server table for that day I noticed that there were some abnormally long text entries, way longer than usual. Curious about the time taken I decided to write the application in PHP (for the practice if nothing else) and see if that improved things.
    It did.
    For the same day, the screen displayed in 0.25 seconds. That was 70 times faster! Too good to be true, I thought. So I compared the two programs.
    The structure of the ASP program was

    loop
        get record
        concatenate output into a string
    endloop
    outout the string
    


    The PHP structure was

    loop
        get record
        output 
    endloop
    


    The ASP structure was a better "separation of concerns" which is the method usually preached, but it did come at a cost as further experimentation was to prove.
    My next task was to rewrite the ASP using the PHP structure and vice versa so that I could get a much fairer comparison.
    As the chart below shows, concatenating does have an overhead (especially with long strings) and this is even more marked with VB which seemed extremely poor at handling the task.

    concatenation_costs.PNG.a3299e27c5a1a3394b6864aff9a766da.PNG

     

  3. The PHP code embedded inside html tags is just generating more html code dynamically (usually containing data extracted from a database) which becomes part of the overall html page.

    This then received by the browser (PHP's job is now done) on the client and rendered to display on the screen.

    EDIT:

    Your PHP code may look something like this

    <html>
    <body>
        <table border='1'>
            <tr>
                <th>IP ADDRESS</th>
                <th>PAGE VISITED</th>
                <th>PREVIOUS PAGE</th>
                <th>DATE</th>
                <th>TIME</th>
            </tr>
            
            <?php
                // printing table rows
                while($row = mysqli_fetch_row($sql)) {
                    echo '<tr>'; 
                    foreach ($row as $col) {
                        echo "<td>$col</td>";
                    }
                    echo "</tr>\n";
                }
                
            ?>
        </table>
    </body>
    </html>
    

    and what your browser then receives is something like this

    <html>
    <body>
        <table border="1">
            <tr>
                <th>IP ADDRESS</th>
                <th>PAGE VISITED</th>
                <th>PREVIOUS PAGE</th>
                <th>DATE</th>
                <th>TIME</th>
            </tr>
            
            <tr><td>111.222.001.123</td><td>a.php</td><td>N/A</td><td>10/11/19</td><td>9:00am</td></tr>
            <tr><td>111.222.001.123</td><td>a.php</td><td>N/A</td><td>10/12/19</td><td>8:00am</td></tr>
            <tr><td>111.222.001.123</td><td>a.php</td><td>xyz</td><td>10/12/19</td><td>12:00pm</td></tr>
            <tr><td>111.222.001.123</td><td>a.php</td><td>N/A</td><td>10/12/19</td><td>1:00pm</td></tr>
        </table>
    </body>
    </html>

     

  4. You are creating variables that you never use ($column_count, $fields_num)

    Your HTML markup for the table is wrong

    • You have two table opening tags.
    • You should have a <tr> tag at start of every row, not just when $i==1.

    Use CSS styles instead of deprecated html attributes (eg width, align)

    You don't need the $conn->close(); - that happens anyway at the end of the script.

    Do not use "SELECT * ...". Specify the columns you want. (The table structure could be changed in future). I also told you earlier that dates should be stored as yyyy-mm-dd format. Same goes for times (hh:mm or hh:mm:ss). If you don't then you can't use them to order your data.

    mysql> SELECT time12
                , time24 
           FROM tbtime 
           ORDER BY time12;
    +--------+--------+
    | time12 | time24 |
    +--------+--------+
    | 2:00AM | 02:00  |
    | 2:00PM | 14:00  |
    | 4:00AM | 04:00  |
    | 4:00PM | 16:00  |
    | 6:00PM | 18:00  |
    | 8:00AM | 08:00  |
    | 8:00PM | 20:00  |
    +--------+--------+

    You can reformat them for human benefit.

    <?php
    $dbHost = "localhost";
    $dbName= "rptDatabase";
    $dbUsername = "username";
    $dbPassword = "password";
    
        mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);
        $conn = mysqli_connect($dbHost, $dbUsername, $dbPassword, $dbName);
        
        $sql = mysqli_query($conn, "SELECT ip
                                         , page
                                         , CASE WHEN referrer = ''
                                                THEN 'N/A'
                                                ELSE referrer
                                           END as referrer     
                                         , DATE_FORMAT(date, '%m/%d/%y') as formatdate
                                         , TIME_FORMAT(time, '%l:%i%p') as formattime
                                    FROM tblTraffic 
                                    ORDER BY ip, date, time");
    
    
    ?>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Example</title>
    <style type="text/css">
        table  { font-family: verdana, sans-serif; font-size: 10pt; width: 100%; border-collapse: collapse; }
        th, td { width: 20%; text-align: center; padding: 8px; }
        th     { background-color: #396; color: #FFF; }
    </style>
    </head>
    <body>
        <table border='1'>
            <tr>
                <th>IP ADDRESS</th>
                <th>PAGE VISITED</th>
                <th>PREVIOUS PAGE</th>
                <th>DATE</th>
                <th>TIME</th>
            </tr>
            
            <?php
                // printing table rows
                while($row = mysqli_fetch_row($sql)) {
                    echo '<tr>'; 
                    foreach ($row as $col) {
                        echo "<td>$col</td>";
                    }
                    echo '</tr>';
                }
                
            ?>
        </table>
    </body>
    </html>

     

  5. I have yet to meet the developer who's happy with GoDaddy.

    You are trying to connect to a MySQL database on GoDaddy's server, so the host is their server's server name or IP address.

    The username and password also need to be valid for access to that database on that server.

    Can you connect to the database using PhpMyAdmin? If so that's a good place to start with the right credentials.

  6. 1 hour ago, ajetrumpet said:

    I added my username to the database as an authorized user and clicked "allow all privaleges".

    Be careful what privileges you give to an online connection. Just give the bare minimum required for the site to function.

  7. This should fly better

    <?php
    $dbHost = "ipaddress";                          //   USE 
    $dbName= "rptDatabase";                         //   VALID
    $dbUsername = "username";                       //   CREDENTIALS
    $dbPassword = "password";                       //   HERE
    
    $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
    $page = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
    $referrer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';    // check they exist
    
    $date = date("Y-m-d");
    $time = date("h:i:a");
    
        mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT);                 // tells mysqli to automatically report errors
        $conn = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);;
    
        $stmt = $conn->prepare("INSERT INTO tblTraffic (ip, page, referrer, date, time) 
                                VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("sssss", $ip, $page, $referrer, $date, $time);
        $stmt->execute();
    
        $stmt->close();
        $db->close();
    ?>

     

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.