Page 1 of 1

Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 8:17 pm
by newbieprogger
So, I was trying to do some OOP (note that i am a real noob :oops: ) but apparently the php refuses to let it fire... can anyone take a look at it? It might be <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> up and all but, constructive criticism is always appreciated.

Code: Select all

 
class pagebuilder{
// tf = table foundation
var $tf = array();
var $posts;
    $tf[1] = "<table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dtl'></td>
                    <td class='dth'></td>
                    <td class='dtr'></td>
                </tr>
                <tr align='center'>
                    <td class='dlv'></td>
                    <td>
                    <table class='alltables' style='border: 1px solid #88b9e2;' cellspacing='0' cellpadding='0'>
                        <tr>
                            <td class='dbgc' style='width: 200px; border-right: 1px solid #88b9e2; height: 100px;' rowspan='3' align='center' >             <table cellspacing='0' cellpadding='0'>
                                <tr>
                                    <td align='center'>"; //Name after this line
    $tf[2] = "</td>
                                </tr>
                                <tr>
                                    <td align='center'><img style='border: 1px solid #88b9e2;' src='"; //image source after this line
    $tf[3] = "'></td>
                                </tr>
                                <tr>
                                    <td align='center'>"; //rep title and rep points after this line
                                    
    $tf[4] = "</td>
                                </tr>
                            </table>
                            </td>
                            <td class='dbgc' style='border-bottom: 1px solid #88b9e2;'>"; //title after this line, if no title make colspan = 2
    $tf[5] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc' align='left' style='border-bottom: 1px solid #88b9e2;'>"; // text after this line
    $tf[6] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc'>"; //signature after this line
    $tf[7] = "</td>
                        </tr>
                    </table>
                    </td>
                    <td class='drv'></td>
                </tr>
            </table>
            
            <table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dbl'></td>
                    <td class='dbh'></td>
                    <td class='dbr'></td>
                </tr>
            </table>"; //that should do it
    function dbrow() {
    var $genstring = "";
        require("privatefile :P"); // the file connects to a db
        $query = "SELECT * FROM sometable";
        $r = mysql_query($query);
        while($row = mysql_fetch_array($r)){
        $genstring = $genstring.$tf[1].$row['name'].$tf[2].$row['img_src'].$tf[3].$row['reptitel']."<br/>".$row['rep'].$tf[4].$row['text_titel'].$tf[5].$row['text'].$tf[6].$row['signatur'].$tf[7];
        }
            $this->posts = $genstring;
            return $this->posts;
            mysql_close($link); 
    }
    
}
 
EDIT:
Oh, don't know if it makes a difference but here is the code for using the object, maybe there's something wrong with it..
Yeah, I removed the extra ?> but it still wont fire

Code: Select all

$pager = new pagebuilder();
        print $pager->dbrow();

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 8:22 pm
by novice4eva

Code: Select all

 
$this->posts = $genstring;
            return $this->posts;
            mysql_close($link); 
        ?>//REMOVE THIS 
        mysql_close($link);
 
also i don't see $posts variable anywhere!! but php might forgive that..

EDIT: to check for errors try enabling error reporting with these

Code: Select all

 
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
 

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 8:27 pm
by newbieprogger
Aight, I added a variable named var $posts but it still does nothing... (Updating teh script)

EDIT: when I added the error things you suggested it gave me this:

"
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in (filelocation of my class) on line 50
"

and line 50 in my script starts with "
$tf[1] = "<table class='alltables' cellspacing='0' cellpadding='0'>.........."; ( note that I cut the code out, so the line number doesnt match with the one I got from home

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 8:47 pm
by novice4eva
OK create a constructor and assign the values to the array inside the constructor not where you declare variables.

EDIT: Though i am puzzled why we have to do that, must have do with array, other data types are no prob!!

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 8:56 pm
by newbieprogger
Could I get an example? I really need suggestions, personally I think the code looks hideous >.<
EDIT: Yeah, I'm sitting here playing a little with the arrays... suggestions more than welcome.. :)

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 9:03 pm
by novice4eva
newbieprogger wrote:Could I get an example? I really need suggestions, personally I think the code looks hideous >.<
That makes two of us :D

Code: Select all

 
class pagebuilder{
 
var $tf = array();
var $posts;
 
//rather than defining those $tf[1] array where we have defined them now we add those to constructor like this
function pagebuilder()
{
 $tf[1] = "<table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dtl'></td>
                    <td class='dth'></td>
                    <td class='dtr'></td>
                </tr>
                <tr align='center'>
                    <td class='dlv'></td>
                    <td>
                    <table class='alltables' style='border: 1px solid #88b9e2;' cellspacing='0' cellpadding='0'>
                        <tr>
                            <td class='dbgc' style='width: 200px; border-right: 1px solid #88b9e2; height: 100px;' rowspan='3' align='center' >             <table cellspacing='0' cellpadding='0'>
                                <tr>
                                    <td align='center'>"; //Name after this line
    $tf[2] = "</td>
                                </tr>
                                <tr>
                                    <td align='center'><img style='border: 1px solid #88b9e2;' src='"; //image source after this line
    $tf[3] = "'></td>
                                </tr>
                                <tr>
                                    <td align='center'>"; //rep title and rep points after this line
                                   
    $tf[4] = "</td>
                                </tr>
                            </table>
                            </td>
                            <td class='dbgc' style='border-bottom: 1px solid #88b9e2;'>"; //title after this line, if no title make colspan = 2
    $tf[5] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc' align='left' style='border-bottom: 1px solid #88b9e2;'>"; // text after this line
    $tf[6] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc'>"; //signature after this line
    $tf[7] = "</td>
                        </tr>
                    </table>
                    </td>
                    <td class='drv'></td>
                </tr>
            </table>
           
            <table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dbl'></td>
                    <td class='dbh'></td>
                    <td class='dbr'></td>
                </tr>
            </table>";
}
//CONTINUE WITH OTHER FUNCTIONS
...
...
 

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 12, 2008 9:08 pm
by newbieprogger
Breakthrough!
OK, so basicly, I'm not allowed to declare any variables inside a function, the instant I moved all the arrays inside, like you said and also had to put genstring outside the function it worked, although now I just have to remove the double mysql i have in the script.

Thanks! :D

EDIT:
A little side-note, I have a secured db connection file using .htaccess ( order deny,allow
deny from all ) so when I try to require from it it says no! but in any other case it says okie dokey, any good explanations why it does that? :?

EDIT: AAAAAH ERROR MESSAGES!! >.<

Bah, I don't like using my secure method anymore, anyways got it to work in some way, just need to sort these errors out

Ok, well I need some help with this as well, it gives me one error still, it says:
Notice: Undefined variable: genstring in (location where the class is) on line 107,
where line 107 is: $genstring = $genstring.$tf[1].$row['name'].$tf[2].$row['img_src'].$tf[3].$row['reptitel']."<br/>".$row['rep'].$tf[4].$row['text_titel'].$tf[5].$row['text'].$tf[6].$row['signatur'].$tf[7];

Posting new code below,

Code: Select all

 
class pagebuilder{
// tf = table foundation
var $tf = array();
var $posts;
var $genstring;
    function dbrow() {
        require("skyddad/condb.php");
        $query = "SELECT * FROM laxiasmeddelanden";
        $tf[1] = "<table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dtl'></td>
                    <td class='dth'></td>
                    <td class='dtr'></td>
                </tr>
                <tr align='center'>
                    <td class='dlv'></td>
                    <td>
                    <table class='alltables' style='border: 1px solid #88b9e2;' cellspacing='0' cellpadding='0'>
                        <tr>
                            <td class='dbgc' style='width: 200px; border-right: 1px solid #88b9e2; height: 100px;' rowspan='3' align='center' >             <table cellspacing='0' cellpadding='0'>
                                <tr>
                                    <td align='center'>"; //Name after this line
    $tf[2] = "</td>
                                </tr>
                                <tr>
                                    <td align='center'><img style='border: 1px solid #88b9e2;' src='"; //image source after this line
    $tf[3] = "'></td>
                                </tr>
                                <tr>
                                    <td align='center'>"; //rep title and rep points after this line
                                    
    $tf[4] = "</td>
                                </tr>
                            </table>
                            </td>
                            <td class='dbgc' style='border-bottom: 1px solid #88b9e2;'>"; //title after this line, if no title make colspan = 2
    $tf[5] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc' align='left' style='border-bottom: 1px solid #88b9e2;'>"; // text after this line
    $tf[6] = "</td>
                        </tr>
                        <tr>
                            <td class='dbgc'>"; //signature after this line
    $tf[7] = "</td>
                        </tr>
                    </table>
                    </td>
                    <td class='drv'></td>
                </tr>
            </table>
            
            <table class='alltables' cellspacing='0' cellpadding='0'>
                <tr align='center'>
                    <td class='dbl'></td>
                    <td class='dbh'></td>
                    <td class='dbr'></td>
                </tr>
            </table>"; //that should do it
        $r = mysql_query($query);
        while($row = mysql_fetch_array($r)){
        $genstring = $genstring.$tf[1].$row['name'].$tf[2].$row['img_src'].$tf[3].$row['reptitel']."<br/>".$row['rep'].$tf[4].$row['text_titel'].$tf[5].$row['text'].$tf[6].$row['signatur'].$tf[7];
        }
            $this->posts = $genstring;
            return $this->posts;
            mysql_close($link); 
    }
    
}
THE LAST EDIT:
Ok, so its finally done, complete, finito, weeeeee. The last problem I had was solved when I realised that I had to use $this->genstring to define the variable... anyways, thanks again!

Now I've trimmed it down a little... this is actually my first php function I've ever made... but It's done, and it works!

Code: Select all

class pagebuilder{
var $posts;
    function dbrow($t1,$t2,$t3,$t4,$t5,$t6,$t7) {   
        require("skyddad/condb.php");
        $query = "SELECT * FROM laxiasmeddelanden";
        $r = mysql_query($query);
        while($row = mysql_fetch_array($r)){
        $this->posts = $this->posts.$t1.$row['name'].$t2.$row['img_src'].$t3.$row['reptitel']."<br/>Rykte: ".$row['rep'].$t4.$row['text_titel'].$t5.$row['text_value'].$t6.$row['signatur'].$t7;
        }
            return $this->posts;
            mysql_close($link); 
    }
    
}
Tips and tricks on how to make a better one would be appreciated :)
BTW is there a way to give like.. reputation points or something on this forum?

Re: Hiya, I bet there is something wrong with this... help!

Posted: Fri Dec 19, 2008 1:29 pm
by novice4eva
OK only few tips, first would be to use access specifier(check them out in google), it's a very good habit

Code: Select all

 
var $posts;
//I would do
private $posts;
 
Next thing would be

Code: Select all

 
 while($row = mysql_fetch_array($r)){
        $this->posts.= $t1.$row['name'].$t2.$row['img_src'].$t3.$row['reptitel']."<br/>Rykte: ".$row['rep'].$t4.$row['text_titel'].$t5.$row['text_value'].$t6.$row['signatur'].$t7;
        }
 
Last one is regarding

Code: Select all

require("skyddad/condb.php");
Umm if you have many functions that needs querying database you should place that line of code at top of your class or even better in the php file where you will use these classes. The advantage being that there will be only one connection, while if you add the require line to each function , for each function call, it will attempt to make a new connection which is unnecessary.

and another thing if you want quicker replies you should stop editing, rather reply a new one coz see when you edit, it is not notified to us, but when you reply with new post they show up at my "view my post" tab AT TOP so then i know someone has replied....getting to this post was pure luck

umm that's it... congrats on you first PHP function :drunk: glad to be a help :D
best wishes