Jump to content

cornelombaard

Members
  • Posts

    11
  • Joined

  • Last visited

Posts posted by cornelombaard

  1. Thank you so much for the reply.

     

    Yeah I know copying from a book is risky but how do I learn? And if you say outdated in what sense please? The book is not that old

    I did what you said and now I get the following error which does not make sense:

     

    Notice: Undefined index: password in /var/www/html/kvcms/ww.incs/basics.php on line 9

     

    Is this index not defined in the file config.php?  which looks like this:

    <?php
    
    $DBVARS = array(
        'username' => 'cmsuser',
        'password ' => 'cmspass',
        'hostname' => 'localhost',
        'db_name' => 'cmsdb'
    );
    

     

    Your help is really appreciated. I want to learn this

  2. I am a newbie

     

    I am trying to build a CMS as described in a book. There are 5 files so far and I am sure I copied the code exactly from the book as is but I get the following error when I ru the script:
    Fatal error: Class 'Page' not found in /var/www/html/kvcms/index.php on line 11

     

    So there are the handler index.php

     

    <?php
    
    // { common variables and functions
    include_once('vv.inc/common.inc');
    $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : '';
    $id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
    // }
    // { get current page id
    if (!$id) {
        if ($page) { // load by name
            $r = Page::getInstanceByName($page);
            if ($r && isset($r->id))
                $id = $r->id;
            unset($r);
        }
        if (!$id) { // else load by special
            $special = 1;
            if (!$page) {
                $r = Page::getInstanceBySpecial($special);
                if ($r && isset($r->id))
                    $id = $r->id;
                unset($r);
            }
        }
    }
    // }
    // { load page data
    if ($id) {
        $PAGEDATA = (isset($r) && $r) ? $r : Page::getInstance($id);
    } else {
        echo '404 thing goes here';
        exit;
    }
    // }
    echo $PAGEDATA->body;
    

     

    then you have the common include file that just calls the basics.php file and that is supposed to load the page.php file and reference the Page object. Which is clearly not happening. The book tells me that you can call and object (or load it) without including the file if you use an autoload function which is included here.

     

    Where is the mistake please?

     

    The basics.php file

     

    <?php
    
    session_start();
    function _autoload($name) {
        require $name . '.php';
    }
    
    function dbInit() {
        if(isset($GLOBALS['db'])) return $GLOBALS['db'];
    global $DBVARS;
    $db = new PDO('mysql:host='. $DBVARS['hostname'] . '; dbname='. $DBVARS['db_name'].
            $DBVARS['username'], $DBVARS['password']);
    $db->query('set name utf8');
    $db->num_queries = 0;
    $GLOBALS['db'] = $db;
    return $db;
    }
    function dbQuery($query) {
        $db=dbinit();
        $q=$db->query($query);
        $db->num_queries++;
        return $q;
    }
    function dbRow($query) {
        $q = dbQuery($query);
        return $q->fetch(PDO::FETCH_ASSOC);
    }
    define('SCRIPTBASE', $_SERVER['DOCUMENT_ROOT'] . '/');
    require SCRIPTBASE . '.private/config.php';
    if(!defined('CONFIG_FILE'))
        define('CONFIG_FILE', SCRIPTBASE. '.private/config.php');
    set_include_path(SCRIPTBASE. 'vv.php_classes' . PATH_SEPARATOR.get_include_path());
    
    ?>

     

    the page.php file

     

    <?php
    
    class Page {
    
        static $instances = array();
        static $instancesByName = array();
        static $instancesBySpecial = array();
    
        function __construct($v, $byField = 0, $fromRow = 0, $pvq = 0) {
    # byField: 0=ID; 1=Name; 3=special
            if (!$byField && is_numeric($v)) { // by ID
                $r = $fromRow ?
                        $fromRow :
                        ($v ?
                                dbRow("select * from pages where id=$v limit 1") :
                                array()
                        );
            } else if ($byField == 1) { // by name
                $name = strtolower(str_replace('-', '_', $v));
                $fname = 'page_by_name_' . md5($name);
                $r = dbRow("select * from pages where name like '"
                        . addslashes($name) . "' limit 1");
            } else if ($byField == 3 && is_numeric($v)) { // by special
                $fname = 'page_by_special_' . $v;
                $r = dbRow(
                        "select * from pages where special&$v limit 1");
            }
            else
                return false;
            if (!count($r || !is_array($r)))
                return false;
            if (!isset($r['id']))
                $r['id'] = 0;
            if (!isset($r['type']))
                $r['type'] = 0;
            if (!isset($r['special']))
                $r['special'] = 0;
            if (!isset($r['name']))
                $r['name'] = 'NO NAME SUPPLIED';
            foreach ($r as $k => $v)
                $this->{$k} = $v;
            $this->urlname = $r['name'];
            $this->dbVals = $r;
            self::$instances[$this->id] = & $this;
            self::$instancesByName[preg_replace(
                            '/[^a-z0-9]/', '-', strtolower($this->urlname)
                    )] = & $this;
            self::$instancesBySpecial[$this->special] = & $this;
            if (!$this->vars)
                $this->vars = '{}';
            $this->vars = json_decode($this->vars);
        }
    
        function getInstance($id = 0, $fromRow = false, $pvq = false) {
            if (!is_numeric($id))
                return false;
            if (!@array_key_exists($id, self::$instances))
                self::$instances[$id] = new Page($id, 0, $fromRow, $pvq);
            return self::$instances[$id];
        }
    
        function getInstanceByName($name = '') {
            $name = strtolower($name);
            $nameIndex = preg_replace('#[^a-z0-9/]#', '-', $name);
            if (@array_key_exists($nameIndex, self::$instancesByName))
                return self::$instancesByName[$nameIndex];
            self::$instancesByName[$nameIndex] = new Page($name, 1);
            return self::$instancesByName[$nameIndex];
        }
    
        function getInstanceBySpecial($sp = 0) {
            if (!is_numeric($sp))
                return false;
            if (!@array_key_exists($sp, $instancesBySpecial))
                $instancesBySpecial[$sp] = new Page($sp, 3);
            return $instancesBySpecial[$sp];
        }
    
    }
    

     

    Any help is appreciated

     

     

  3. Hi I am new to this

     

    When you have a form with various fields in them and you have given names to each field. If you submit that form then the values go sit in the array $_REQUEST am I correct? Then why do I get a index undefined?

     

    Here is the code for the form:

    <code>

    <h2>
        Sign my guestbook
    </h2>
    <form method="post" action="entry.php">
        <b>Name:</b>
        <input type="text" size="40" name="name">
        <br><br>
        <b>Location:</b>
        <input type="text" size="40" name="location">
            <br><br>
        <b>Email:</b>
        <input type="text" size="40" name="email">
            <br><br>
        <b>Homepage:</b>
        <input type="text" size="40" name="url">
            <br><br>
        <b>Comments:</b>
        <textarea name=”comments” cols=”40” rows=”4”
        wrap=”virtualv></textarea>

        <br><br>
    <input type="submit" name="submit" value="Sign!"> <input type="reset" name="reset" value="Start over">

        </form>
    </form>

    </code>

     

    Here is the code for entry.php

     

    <code>

    <?php

    include 'includes/dbconnect.php';
    if($_REQUEST['submit'] == "Sign!")
    {
        $query = "insert into guestbook (name, location, email, url, comments) values ('".$_REQUEST['name']."',
            '".$_REQUEST['location']."', '".$_REQUEST['email']."', '".$_REQUEST['url']."', '".$_REQUEST['comments']."')";
        mysql_query($query);

    ?>
    <h2>Thanks</h2>
    <h3><a href="view.php">View my guesbook</a></h2>
    <?php
    }
    else
    {
        include 'sign.php';
    }
    ?>

    </code>

     

    I need to understand what happend when a form is submitted where fields have names and the method used it post.

     

    Any help is greatly appreciated Thanks

  4. I also get the error and here is my code:

     

    <html>

    <head>

    </head>

    <body>

    <?php

     

    //Open adatabse connetcion

    $connect = mysql_connect("localhost", "root");

     

    //Select db

    mysql_select_db("winestore",$connection);

     

    $result = mysql_query ("select * from wine", $connection);

     

    while ($row = mysql_fetch_array($result, MYSQL_NUM))

    {

    forech ($row as $attribute)

    print "{$attribute}";

    print /n;

    }

    ?>

    </body>

    </html>

     

    Please could you tell me what the T_AS means?

  5. The following code gives me the error at the bottom of the page. What's wrong and how do I fix it? The book does not explain properly. Does anyone know what is the best book for learning PHP MySQL and Javascript for the web?

     

    The code:

     

    <html>

    <head>

    <title>

    </title>

    </head>

    <body>

    <pre>

    <?php

    $connection = mysql_connect("localhost", "corne", "c");

     

    mysql_select_db("winestore", $connection);

     

    $result = mysql_query("select winery_name, phone, fax from winery limit 20", $connection);

     

    while($row = mysql_fetch_array($result))

    {

    print "The {$row["Winery Name"]} Winery's fax is {$row["fax"]} Their phone is {$row["phone"]}. \n";

    }

    ?>

    </pre>

    </body>

    </html>

     

     

    The Error:

     

    Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /var/www/html/testphp/database.php on line 15

     

    Also what is MYSQ_NUM?

  6. I get the following error  Cannot redeclare count() in /var/www/html/testphp/1.php on line 14 with the code looking like this:

    <html>

    <head>

    <title>This whole file will be used to learn PHP and within two months.</title>

    </head>

     

    <body>

    <?php

     

    function count()

    {

    static $count = 0;

    $count++;

    return $count;

    }

     

    print count();

    print count();

     

    ?>

    </body>

    </html>

     

    What am I doing wrong or is the book wrong?

×
×
  • 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.