Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Creating a site completely reliant on php
Old 07-19-2009, 08:12 PM Creating a site completely reliant on php
konetch's Avatar
Ultra Talker

Posts: 258
Trades: 0
I'm still extremely new to php, and am trying to become a developer instead of a designer. Right now I'm trying to conquer a huge project for my skills. I basically want to create a site where users will be able to write articles on there own local sports. I found a couple scripts on the internet and am dissecting them to figure out how they work.

I want the pages on the site to be dynamically created. (i.e. http://mysitename.com/index.php?post=1&user=sportsman )

I've found a helpful script that creates a post on the site, and to create an article you click on a link that looks like http://localhost/display.php?admin=1

I saw where they got this in the code, but when I tried to recreate with my own code, it didn't work.

Here's the files:

newarticle.php
PHP Code:
<?php

class simpleCMS {

  var 
$host;
  var 
$username;
  var 
$password;
  var 
$table;

  public function 
display_public() {
    
$q "SELECT * FROM testDB ORDER BY created DESC LIMIT 3";
    
$r mysql_query($q);

    if ( 
$r !== false && mysql_num_rows($r) > ) {
      while ( 
$a mysql_fetch_assoc($r) ) {
        
$title stripslashes($a['title']);
        
$bodytext stripslashes($a['bodytext']);

        
$entry_display .= <<<ENTRY_DISPLAY

    <div class="post">
        <h2>
            
$title
        </h2>
        <p>
          
$bodytext
        </p>
    </div>

ENTRY_DISPLAY;
      }
    } else {
      
$entry_display = <<<ENTRY_DISPLAY

    <h2> This Page Is Under Construction </h2>
    <p>
      No entries have been made on this page. 
      Please check back soon, or click the
      link below to add an entry!
    </p>

ENTRY_DISPLAY;
    }
    
$entry_display .= <<<ADMIN_OPTION

    <p class="admin_link">
      <a href="
{$_SERVER['PHP_SELF']}?admin=1">Add a New Entry</a>
    </p>

ADMIN_OPTION;

    return 
$entry_display;
  }

  public function 
display_admin() {
    return <<<ADMIN_FORM

    <form action="
{$_SERVER['PHP_SELF']}" method="post">
    
      <label for="title">Title:</label><br />
      <input name="title" id="title" type="text" maxlength="150" />
      <div class="clear"></div>
     
      <label for="bodytext">Body Text:</label><br />
      <textarea name="bodytext" id="bodytext"></textarea>
      <div class="clear"></div>
      
      <input type="submit" value="Create This Entry!" />
    </form>
    
    <br />
    
    <a href="display.php">Back to Home</a>

ADMIN_FORM;
  }

  public function 
write($p) {
    if ( 
$_POST['title'] )
      
$title mysql_real_escape_string($_POST['title']);
    if ( 
$_POST['bodytext'])
      
$bodytext mysql_real_escape_string($_POST['bodytext']);
    if ( 
$title && $bodytext ) {
      
$created time();
      
$sql "INSERT INTO testDB VALUES('$title','$bodytext','$created')";
      return 
mysql_query($sql);
    } else {
      return 
false;
    }
  }

  public function 
connect() {
    
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " mysql_error());
    
mysql_select_db($this->table) or die("Could not select database. " mysql_error());

    return 
$this->buildDB();
  }

  private function 
buildDB() {
    
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS testDB (
title        VARCHAR(150),
bodytext    TEXT,
created        VARCHAR(100)
)
MySQL_QUERY;

    return 
mysql_query($sql);
  }

}

?>
display.php
PHP Code:
 <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    
    <title>Simple CMS with PHP</title>
    
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>

  <body>
      <div id="page-wrap">
    <?php
    
      
include_once('newarticle.php');
      
$obj = new simpleCMS();

      
/* CHANGE THESE SETTINGS FOR YOUR OWN DATABASE */
      
$obj->host 'localhost';
      
$obj->username 'username';
      
$obj->password 'db_password';
      
$obj->table 'database_name';
      
$obj->connect();
    
      if ( 
$_POST )
        
$obj->write($_POST);
    
      echo ( 
$_GET['admin'] == ) ? $obj->display_admin() : $obj->display_public();
    
    
?>
    </div>
  </body>

</html>
I was hoping someone with a little more experience could help me out. I'm not asking for a full page of code, because I really want to learn how this works. If you could just point me in the right direction it would be great.

Thanks
__________________
Alex
konetch is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-20-2009, 02:42 AM Re: Creating a site completely reliant on php
Lashtal's Avatar
wherenomanhasgonebefore

Posts: 679
Name: Lashtal
Trades: 0
so that is their script, and what did you try that didn't work?
__________________
Currently Reading:
Please login or register to view this content. Registration is FREE
Lashtal is offline
Reply With Quote
View Public Profile
 
Old 07-20-2009, 10:18 AM Re: Creating a site completely reliant on php
Junior Talker

Posts: 4
Name: Dai Williams
Location: Derby, UK
Trades: 0
Firstly pick a different script, that one one is not going to help you learn - simple example it has a variable called table which is actually the database name, not the database table at all

Secondly as Lashtal said if you want us to debug your code, post your code, that looks like their code? Also tell us what error message you get etc. - "it didn't work" is not very helpful
DaiWelsh is offline
Reply With Quote
View Public Profile Visit DaiWelsh's homepage!
 
Old 07-20-2009, 12:37 PM Re: Creating a site completely reliant on php
konetch's Avatar
Ultra Talker

Posts: 258
Trades: 0
Thanks, I guess I should have included my own script as well. I basically tried to copy the one off the internet as much as I could, but took out the unnecessary code. I have two file newlink.php and index.php

newlink.php
PHP Code:
<?

class newlink {

  public function 
displayLink () {

    
$link  .= <<<PAGE_LINK

    <a href="
{$_SERVER['PHP_SELF']}?post=1">Read my first page!!!</a>

  }

  public function displayPage () {
    <h2>Hello World</h2>
    <p>This is a test post</p>
  }
}
?>
index.php
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Home</title>
  </head>

  <body>
<?

  
require_once("newlink.php");

    
$obj = new newlink ();

    echo ( 
$_GET['post'] == ) ? $obj->displayLink() : $obj->displayPage();

?>
  </body>
</html>
I get a parse error on line 18 of newlink.php

Thanks
__________________
Alex

Last edited by konetch; 07-20-2009 at 12:38 PM..
konetch is offline
Reply With Quote
View Public Profile
 
Old 07-21-2009, 07:39 PM Re: Creating a site completely reliant on php
konetch's Avatar
Ultra Talker

Posts: 258
Trades: 0
can anyone help?
__________________
Alex
konetch is offline
Reply With Quote
View Public Profile
 
Old 07-21-2009, 07:54 PM Re: Creating a site completely reliant on php
NullPointer's Avatar
Will Code for Food

Posts: 2,786
Name: Matt
Location: Irvine, CA
Trades: 0
The parse error occurs because you forget to close your heredoc string. Just so you are aware, the displayLink function has no effect. To begin with you can't append a string to $link because it is not defined within the scope of that function.

If you are trying to define it, use = not .=

If $link is a member variable of your class you will have to access it explicitly:
PHP Code:
$this->link .= <<<PAGE_LINK

    <a href="
{$_SERVER['PHP_SELF']}?post=1">Read my first page!!!</a>
PAGE_LINK; 
If $link is not a member variable, assigning it a value has no effect beyond the scope of that function.

If you provide more info on what you expect that class to do I can provide a better answer. Also, this is somewhat unrelated, but I noticed you are using short tags (ie <? instead of <?php ). Don't. It is a bad habit. Doing so reduces the portability of your application and can cause problems if you are trying to use PHP in combination with XML.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 07-21-2009 at 08:22 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-21-2009, 08:37 PM Re: Creating a site completely reliant on php
konetch's Avatar
Ultra Talker

Posts: 258
Trades: 0
okay thanks nullpointer for the help I'm now getting a parse error for line 20

PHP Code:
<?php

class newlink {

  public function 
displayLink () {

$this->link .= <<<PAGE_LINK

    <a href="
{$_SERVER['PHP_SELF']}?post=1">Read my first page!!!</a>
PAGE_LINK;  

  }

  public function displayPage () {
    <h2>Hello World</h2>
    <p>This is a test post</p>
  }
}

?>
Sorry for asking so many question, I'm still trying to learn and this part of php is still new. I'm trying to get as much info off google.
__________________
Alex
konetch is offline
Reply With Quote
View Public Profile
 
Old 07-22-2009, 01:24 AM Re: Creating a site completely reliant on php
NullPointer's Avatar
Will Code for Food

Posts: 2,786
Name: Matt
Location: Irvine, CA
Trades: 0
I overlooked it before, but your syntax is invalid in in the displayPage function. HTML and PHP are distinct, in your code you start writing HTML code in the middle of your PHP function. Try:
PHP Code:
  public function displayPage () {
?>
    <h2>Hello World</h2>
    <p>This is a test post</p>
<?php
  
}
}
Closing the PHP tags tells PHP that it no longer needs to parse the code and it assumes all of the code, until an opening tag, is to be sent to the browser as output. Alternatively you can just store the HTML in a string and then output manually.

Also, I wasn't clear on what you expect displayLink to do. If all it needs to do is output the link code you can do it like this:
PHP Code:
public function displayLink () {
?>
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?post=1">Read my first page!!!</a>
<?php
}
This is somewhat unrelated, but personally I am not a fan of the heredoc syntax. If I have a large string that I need to store I prefer to use an output buffer. Not for any particular reason, I just don't like heredoc.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 07-22-2009, 01:32 AM Re: Creating a site completely reliant on php
konetch's Avatar
Ultra Talker

Posts: 258
Trades: 0
Any other way you would suggest getting the link I'm looking for. Maybe not a full string of code but just a tutorial?

Thanks. You're right I need to look at my PHP syntax better.

EDIT: I got it working. If anyone ever runs accross this post in the future this is how you do it

index.php
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Home</title>
  </head>

  <body>
<?php

  
require_once("newlink.php");

    
$obj = new newlink();
if (isset(
$_GET["post"]) && $_GET["post"] == 1) {
    echo 
$obj->displayPage();
} else {
    echo  
$obj->displayLink();
}
?>
  </body>
</html>
newlink.php
PHP Code:
<?php

class newlink {

  public function 
displayLink () {

?>
    <a href="<?php echo $_SERVER['PHP_SELF']; ?>?post=1">Read my first page!!!</a>
<?php
  
}

  public function 
displayPage () {
?>
    <h2>Hello World</h2>
    <p>This is a test post</p>
<?php
  
}
}
?>
Thanks everyone for the help
__________________
Alex

Last edited by konetch; 07-22-2009 at 04:33 PM..
konetch is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Creating a site completely reliant on php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44955 seconds with 12 queries