Forum Moderators: coopster

Message Too Old, No Replies

Problem with Classes

         

stormshield

1:38 pm on Oct 21, 2006 (gmt 0)

10+ Year Member



Hi,

I'm pretty new to OOP programming and I have this very irritating problem with classes.

class table{

function load(){

$num =2;
for($i = 0;$i < 5; $i ++){

echo $this->text;
$num++;
}
}
}

$table1 = new table();
$table1->text="I have $num cars";
$table1 ->load();


I'd like it to output 2,3,4,5 and so on.

Any suggestions appreciated.

Storm

dreamcatcher

2:43 pm on Oct 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there stormshield,

Well, it depends on how you want to use the data and what are trying to accomplish to determine whats best. For a simple message, you could do:


class table{

function load($start,$end){

$num = array();

for($i = $start;$i < $end; $i ++){
$num[] = $i;
}

return implode(",",$num);

}
}

$table1 = new table();
echo "I have $table1->load(2,6) cars.";

Lots of different ways, but like I say depends on what you want if for.

dc

henry0

4:58 pm on Oct 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dreamcatcher offered indeed a good solution.

It is important to look at one aspect of its code
nothing is echoed within the function()
It is not the best coding practice to echo something directly within a () but returning something like 1 or calling another () such as many from the PHP existing () or other of your own () is the way to go.

stormshield

5:25 pm on Oct 21, 2006 (gmt 0)

10+ Year Member



Hi dreamcatcher,

I expressed myself very unclearly, sorry. What I want to achieve is "tell" the function inside the class what variable it should use inside itself... let me bring the whole script:

class table {
function table(){
$this->id_here = 1;
}

function display_table2(){
include("includes/connect_db.php");
include("global_values.php");
$page= $this->page;
$adres_start = $this->adres_start;
$table = $this->table;
$condition = $this->condition;
$num_cols = count($this->column);

$page2 = ($page-1) *10;


$mysql_query="SELECT * FROM $table $condition ORDER BY id DESC LIMIT $page2,10";
$select = mysql_query($mysql_query);

$id_here = $page2;
while($dane = mysql_fetch_array($select)){
$id_here++;
$this->id_here++;

echo " <tr>";

for($i=1;$i <= $num_cols; $i++){
echo "<td align=\"center\">";
echo $this->column[$i];
echo "</td>";
}

echo " </tr>";
}
}
}

In other php file:


include("classes/table_below_class.php");

$table1 = new table();

$table1->page=$page;
$table1->adres_start=$adres_start;
$table1->table=$table;
$table1->condition=$condition;
$table1->column[1]="<strong>$table1->id_here</strong>";

$table1->display_table2();

So, provided there are 3 records in the database, in this example I'd like it to produce:
<tr>
<td align="center"><strong>1</strong></td>
</tr>
<tr>
<td align="center"><strong>2</strong></td>
</tr>
<tr>
<td align="center"><strong>3</strong></td>
</tr>

But I get 1,1,1... it only sees the $this->id_here = 1; specified in the constructor.

Maybe I'm doing it in a totally wrong way?

Thanks
Storm

henry0

7:23 pm on Oct 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Short example
class table
{ // declare class member variable
var $stuff;
var $stuff2;

/* constructor function, note constructor and class must = same name */
function table($stuff, $stuffs2)
// give value to member var
{
$this->id_here = 1;
}
// entering the meat of the building process

The bunch of "1" are showing the object id, therefore what's generated is not the result but the object itself.

stormshield

9:15 pm on Oct 21, 2006 (gmt 0)

10+ Year Member



henry0 thanks for reply, unfortunetely, it's still not what I wanted. I'll try to explain it in a different way:

I've got multiple tables on my website, many of which have different values in its rows and columns. E.g.:

id ¦ something1 ¦ something2
[below values]

or

something3 ¦ id ¦ something5
[below values]

In order to be able to easily new ones or change its properties, I came up with an idea of making a class so that I would be able to set up new tables in a way similiar to this:

$table1->new table();
// some properties

$table1->colum[1] = "<bold>$name</bold>"; // the way all <td>'s are displayed in the first column.
$table1->colum[2] = "<a href=\"$url\">$name</a>";

It's no problem at all to pass value such as "<bold>", however the problems began when it comes to the variables ($name, $url) because are replaced with its values (e.g. nil)...

As I stated earlier, I need to somehow tell the class what variable it should use inside itself while executing the function ($id_here - in case of the first column).

I hope I made it a bit clearer.
Storm

henry0

10:42 pm on Oct 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using OOP the best way to deal with this is to create a template or templates

<? A CLASS EXAMPLE?>
<? // an object is an instance of the class created by "Instantiation" once instantiated you may call
// the methods (Functions) defined in the class. instantiation is done through "new"Keyword
// as for example in "index" $page = new tpl_class
?>
<?php # tpl_class.php
// This class reads in a template, sets the different values, and sends it to the browser.

class tpl_class {

// Set the attributes.(functions or others..) Keyword var declares variable in class
var $template;
var $html; // var could also be var $html='..somethng..';
var $parameters = array();// note how using an array();
var $parse;

function tpl_class ($template) { // This function sets which template will be used
// (see: CreatePage() where str replace replace with template name
// as per the file using the class).
// This function is a constructor it returns auto the object once setup
// the construtor accepts all vars needed in process
// we assign value to vars via $this->
$this->template = $template;
$this->html = implode ("", (file($this->template))); // Read the template into an array, implode creates a string
// representing all the array elements.
// $ this points to the object in which the function (Method) runs
// -> points to a function (Method) named within the object
}

function SetParameter ($variable, $value) { // This function sets the particular values. Parameters was
// set w/class attributes as var $parameters = array();
$this->parameters[$variable] = $value;
}

function CreatePage () { // This function title speaks by itself.

foreach ($this->parameters as $key => $value) { // Loop through all the parameters and set the variables to values.
// key in arrays is a pointer or index that returns the value of the first item placed in the array
// => is an operator that is placed after Key where the lpointer starts looping.
$template_name = '{' . $key . '}';
$this->html = str_replace ($template_name, $value, $this->html);
}
echo $this->html;
}

}
?>

<? MAIN FILE CALLING A TEMPLATE?>
<?php # tpl_category_dex.php
session_start(); // if needed
//////////////////////////////////////////////////////////////////////////////
// THIS FILE CALLS YOUR TEMPLATE IT COULD BE THE INDEX.PHP
/////////////////////////////////////////////////////////////////////////////
require_once($_SERVER['DOCUMENT_ROOT']."/conn_stuffs.php");
require_once ("../classes/tpl_class.php"); // Include the class

//error_reporting (E_ALL);// This is the index page that uses the tpl_class.

$conn=db_connect(); // function to DB conn

$page = new tpl_class ("../templates/main_template_tpl_category.inc.php"); // Create an instance of your template.


///////////////////////////////////////////
// THE FOLLOWING ARE CHUNKS OF DATA - UP TO YOU -
// PULLED FROM A DB - NOTE THAT IT COULD BE HARD CODED
/////////////////////////////////////////
$content_page_title .= '';
include "../includes/page_title.inc.php";
$page->SetParameter("PAGE_TITLE",$content_page_title );

$content_intro .= '';
include "../includes/intro.inc.php";
$page->SetParameter("INTRO", $content_intro);

$content_page_footer_html .= '';
include "../includes/page_footer_html.inc.php";
$page->SetParameter("PAGE_FOOTER_HTML", $content_page_footer_html);

$footer = "Last modified " . (date("l, F j, Y", filemtime("../modified.txt")));
$page->SetParameter("PAGE_FOOTER", $footer);

$page->CreatePage(); // Send the page to the browser.
unset ($page);
?>

<? A TEMPLATE EXAMPLE?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<head>

</head>

<title>{PAGE_TITLE}</title>

<body>
<table width="765" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2">

<div id="top"></div>
</td></tr>
<tr>
<td colspan="2">

<div class="content_nav">

</div>
</td></tr>

<tr>
<td width="185" valign="top">
<div id="leftnav">
<div class="nav"><br>
{URL_DIRECTORY}
</div></td>

<td width="565" valign="top" height="100%">
<div id="content">
<div id="img_lft">
<div class="intro">
{INTRO}

</div>

<!-- end of displaying rows -->

</td><!-- ends div content -->

<tr>
<td colspan="2" valign="bottom">
<div id="footer">
{PAGE_FOOTER_HTML}
{PAGE_FOOTER}
</div> <!-- footer -->

</body>
</html>

<? HOW DO WE CALL INPUT FROM DB?>
// FOR EXAMPLE WE HAVE " {INTRO} "

<?
$conn = db_connect();

$sql= "select intro from #*$!XX WHERE AA='BBBB' ";
$result = mysql_query($sql,$conn);
while ($new_content=mysql_fetch_array($result) ){
$content_intro.= $new_content[intro];
}
?>

stormshield

1:22 pm on Oct 22, 2006 (gmt 0)

10+ Year Member



I've tested it and it works like a dream, now I have to implement it within my code.

Henry, tell me one thing, there is something called "Smarty" - does it work in a similiar way as the class you've created?

Thanks,
Storm

henry0

2:28 pm on Oct 22, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working!

Smarty is very well known, very respected and used by many well known sites.
It’s not a very light system, since it is also templates based it requires from you an effort to learn how it works. (if I am correct its manual could be as big as a hundred + pages)

My $0.10, first get accustomed with OOP and templating.
Do your own as a great training, or because it fits exactly your needs, further doing your own will allow it to perform up to your expectations and you will for sure know its “inside and out”.

Good luck

stormshield

4:43 pm on Oct 22, 2006 (gmt 0)

10+ Year Member



Thanks for your advice!