Page 1 of 1

Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 8:35 pm
by Payton
Hi.

I've been having problems getting this script of mine to work, but before I dive into the script, let me explain the theory and idea behind it first. There is a file named config.php which holds variables for everything called inside the main script (index.php) in alphabetical order; i.e. $a is the MySQL connection, and when you get further into the script $h subtracts the total number of records in my database from the number I want to show per page ($h = $d - $b;). At the beginning of index.php of course, config.php is included.

Here is the error I get when I go to localhost/phpquickjournal/index.php:
Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\xampplite\htdocs\phpquickjournal\config.php on line 12

Warning: Division by zero in C:\xampp\xampplite\htdocs\phpquickjournal\config.php on line 13

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\xampplite\htdocs\phpquickjournal\config.php on line 15

Warning: mysql_connect() expects parameter 1 to be string, resource given in C:\xampp\xampplite\htdocs\phpquickjournal\index.php on line 30
Here is index.php:

Code: Select all

<?php
 
/* PHP Quick Journal RC1
* Developed by Payton Bice
* http://paytonbice.com/phpquickjournal/
* PHP Quick Journal ((C) 2009) is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.
* Under this license, you are allowed to:
*   >> To Share: To copy, distribute and transmit the work
*   >> To Remix: To adapt the work
* Under the following conditions:
*   >> Attribution — 
*   You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
*   >> Share Alike - 
*   If you alter, transform, or build upon this work, you may distribute the resulting work only under the same, similar or a compatible license. 
* With the understanding that:
*   >> Waiver - 
*   Any of the above conditions can be waived if you get permission from the copyright holder. 
*   >> Other Rights - 
*   In no way are any of the following rights affected by this license: 
*       # Your fair dealing or fair use rights;
*       # Apart from the remix rights granted under this license, the author's moral rights;
*       # Rights other persons may have either in the work itself or in how the work is used, such as publicity or privacy rights.
*   NOTICE:
*   For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.
* Further license information can be found at http://paytonbice.com.
*/ 
 
include 'config.php';
 
mysql_connect($a);
 
if (!$a) //Display a MySQL error if we couldn't connect. 
    { 
    die(mysql_error());
    }
else //If we connected to the database successfully, you will move forward.
    {
    //Move on into the HTML.
?>
 
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $title; //The title of the page. ?></title>
    <link rel="stylesheet" type="text/css" href="styles/default/theme.css" />
    </head>
    
    <body>
    
    <div id="container">
        <div id="header" class="header">
            <img src="styles/default/header.png" alt="Header" />
        </div>
        
        <div class="bodycontainer">
            <?php 
            //Now we dive into the fun stuff. Goody goody.  
            
            mysql_select_db($l); //Select the database.
            
            if (!$c) //Pages = 0.
                $c = 0;
            
            mysql_query($f); //Selecting from the database. More information is in the config.php file.
            
                    //And now time to output data from the database onto the page. 
                    echo $g['title'] . " - " . $g['date'] . " - " . "Posted by:" . "  " . $g['author'] . " " . "<br /> <br />"; //Title, date, posted by: author. Two line breaks.
                    echo $g['content'] . " " . "<br /> <br />"; //Shows the post, then adds two line breaks.
                    echo "Comments."; //A placeholder for the comments section, which is being worked on.
                    echo "<hr />"; //A horizontal line to repeat the process of blog posts.     
            
            if (!($c<=0)) //If the page is less than or equal to 0, let us navigate back some pages.
                echo "<a href='index.php?page=$h'>Previous</a> ";
 
         if ($c!=$k) //If 'page' is not equal to $k...
            echo " <a href='index.php?page=$k'>$j</a> "; //Echo the first page. 
         else
            echo " <a href='index.php?page=$k'><b>$j</b></a> "; 
         $j++;
 
        if (!($c>=$d-$b)) //If $c is greater than or equal to $d minus $b...
               echo " <a href='index.php?page=$i'>Next</a>"; //Show the next link.
               
               echo "<hr />"; //Horizontal rule.
            
            //Blog information.
            echo "<br />";
            echo "Blog created by " . " " . $author; //Blog created by $author as defined in the config.php file. 
            echo "&nbsp; - "; //Space.
            echo "Click here to read more about the author."; //Currently this is a placeholder for the about page.
            
            
            //We're done. Time to close the MySQL connection.
        mysql_close($a);
        }
        ?>
    </div>
    
    </div>
    
    </body>
    
    </html>
And here is config.php:

Code: Select all

<?php
 
$title = "Test Journal"; //The site's title.
$author = "Payton Bice"; //The author of the blog.
 
/*Below are variables that have been set up that we call all throughout the script.
For more information please visit the wiki. */
 
$a = mysql_connect("localhost","root","password"); //Set up our connection under the variable $a.
$b = 1; //How many journal entries we're going to show per page. 
$c = $_GET['page']; //The suffix that takes us to new pages. Example: index.php?page=3
$d = mysql_num_rows($f); //Count how many records are in our database.
$e = $b / $d; //$e is our maximum number of pages, calculated by dividing $b and $d. 
$f = mysql_query("SELECT * FROM phpqj_articles ORDER BY date DESC LIMIT $c, $b");
$g = mysql_fetch_array($f); //$g fetches the array $f. 
$h = $d - $b; //The total number of records minus how many we display on a page.
$i = $c + $b; //'page' plus however many records we display on a page. 
$j = 1; //Set the variable for the first page. 
$k = 0;$k<$d;$k=$k+$b; //$k is equal to 0; but if $k is less than or equal to $d, then $k is however many blog posts we want on a page ($b).
$l = mysql_select_db("journal",$a); //Select the database.
 
 
?>
I have tried other methods however those are no longer with me, because I overwrote the files.

Can anyone offer some support? I've been working on this for quite a while now. :(

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 8:50 pm
by AlanG
In the config.php file, on line 12 you are using the mysql_num_rows function, but the resource you are passing into it has not been defined yet. Move it down below the mysql_query function.

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 8:56 pm
by Payton
AlanG wrote:In the config.php file, on line 12 you are using the mysql_num_rows function, but the resource you are passing into it has not been defined yet. Move it down below the mysql_query function.
Thanks, I'll try that. I figured it would be something along those lines, but alas, I'm not too experienced in PHP yet, so I didn't know. :mrgreen:

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 8:57 pm
by daedalus__
payton your naming scheme is really bothering me. how can you read it like that?

plus what about when you want to document your code and you have to explain alphabet soup?

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 9:01 pm
by Payton
daedalus__ wrote:payton your naming scheme is really bothering me. how can you read it like that?

plus what about when you want to document your code and you have to explain alphabet soup?
I don't know, but I've always understood the code more clearly when it was something like A, B, C, D, etc...

Also, I'm going to have a pretty in depth documentation section on my site for the variables, or as you call it, alphabet soup ( :lol: ), that explains what each variable does and why it's there. Of course this is only the first development stage of my project so anything can change from now to the release date, which is still to be decided.

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 9:06 pm
by AlanG
daedalus__ wrote:payton your naming scheme is really bothering me. how can you read it like that?

plus what about when you want to document your code and you have to explain alphabet soup?
I second that. If your code isn't readable, it's not worth writing as it can't be expanded on in the future. We're not talking about other developers either. If you write something, its unlikely your going to even remember it 6 to 12 months down the line.

Try something like this:

Code: Select all

 
$db_connection = mysql_connect("localhost","root","password"); //Set up our connection under the variable $a.
$max_entries = 1; //How many journal entries we're going to show per page.
$page_suffix = $_GET['page']; //The suffix that takes us to new pages. Example: index.php?page=3
$num_records = mysql_num_rows($f); //Count how many records are in our database.
 
Something along those lines. There are various coding standards you could use.

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 9:09 pm
by Payton
AlanG wrote:
daedalus__ wrote:payton your naming scheme is really bothering me. how can you read it like that?

plus what about when you want to document your code and you have to explain alphabet soup?
I second that. If your code isn't readable, it's not worth writing as it can't be expanded on in the future. We're not talking about other developers either. If you write something, its unlikely your going to even remember it 6 to 12 months down the line.

Try something like this:

Code: Select all

 
$db_connection = mysql_connect("localhost","root","password"); //Set up our connection under the variable $a.
$max_entries = 1; //How many journal entries we're going to show per page.
$page_suffix = $_GET['page']; //The suffix that takes us to new pages. Example: index.php?page=3
$num_records = mysql_num_rows($f); //Count how many records are in our database.
 
Something along those lines. There are various coding standards you could use.
That's what I used to go by, but as I said, this is more experimental. I'll probably have two different versions, the experimental one and the stable release one.

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 9:36 pm
by daedalus__
Payton wrote:
AlanG wrote:
daedalus__ wrote:That's what I used to go by, but as I said, this is more experimental. I'll probably have two different versions, the experimental one and the stable release one.
perfect :)

Re: Help with PHP project I'm working on.

Posted: Mon Dec 07, 2009 9:37 pm
by Payton
Well with that said, I suppose this topic has been taken care of. :P

Request to be locked.
(Unless you don't do lock requests, which is fine. I just come from a forum where people do that often)