 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
04-22-2011, 03:55 PM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Problem with template class
Well i have created a template class and i works fine with normal content but gives a problem with database content.
the code is like this:
PHP Code:
class temp{
function get($file){
$this->page = get_file_contents($file);
return $this->page;
}
function vars($temp_vars){
foreach($temp_vars as $keys => $values){
$this->page = str_replace('{'.$keys.'}', $values, $this->page);
}
return This->page;
}
function show(){
echo $this->page;
}
}
$temp = new temp();
$temp->get('file.html');
// mysql database connection
while($row = mysql_get_num($data)){
$temp->vars(array('var',$row));
}
$temp->show();
now what this does is gets the file.html converts into a single string and add it to a variable then in the second function it changes the keys from the html file to original values from the database but it loops only only once and if i echo this->page; in the var() function then it shows the same data multiple times. can anyone help??
|
|
|
|
04-22-2011, 09:37 PM
|
#2 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
I am guessing you have a get_file_contents function in your code because php has file_get_contents.
The other thing is that in the var function your last line is return This->page; which should be return $this->page;
Another thing is that you are passing an array of values when you pass $row so in essence this line: $temp->vars(array('var',$row)); is really $temp->vars(array('var',array('field1'=>'value1','field2 '=>'value2', ...)));.
which in itself in the vars function when you call str_replace you are trying to replace a string for an array which is not possible. I don't know why it didn't give you an error.
That is my take without running the code. In a gist, you might want to take a look at the values you pass around.
|
|
|
|
04-23-2011, 04:09 PM
|
#3 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Ya sorry actually i didn't copied the codes i just made it so i remember it so i missed a few part you corrected well how do i convert it from
$temp->var(array('var',$row));
to
$temp->vars(array('var',array('field1'=>'value1','fiel d2 '=>'value2', ...)));
i mean i have only this $row should i add it to field or value and if in field then what should be in value and if in value then what should be in fields sorry i am new to php development.
|
|
|
|
04-23-2011, 08:05 PM
|
#4 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
It seems that you want to show a list of rows, but they show the same data all over, because of the 'var' key. You might want to take a look at at using a combination of extract and to the output buffer functions.
Kohana has a simple enough class that you can take a look to learn more about it
|
|
|
|
05-09-2011, 05:27 AM
|
#5 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Ok now i have changed a few codes and this is the new script and i learned that its overwriting $this->p so i changed a bit and now its is giving a new problem it only shows the forum descriptions that is DES not the title that is TIT what is going wrong here??
PHP Code:
class temp{ var $p = array(); var $built = array(); function getdata($file){ $this->p = $file; return $this->p; } function vars($data){ foreach($data as $k => $v){ $this->built = str_replace("{".$k."}", $v, $this->p); } echo $this->built; } }
$temp = new temp();
$temp->getdata("<h1>{TIT}</h1><BR />{DES}");
mysql_select_db($database_config, $config); $query_bbforum = "SELECT * FROM forum"; $bbforum = mysql_query($query_bbforum, $config) or die(mysql_error()); while($row_bbforum = mysql_fetch_array($bbforum)){
$temp->vars(array("TIT" => $row_bbforum['title'], "DES" => $row_bbforum['description'])); }
Last edited by Keshav : 05-09-2011 at 05:40 PM.
|
|
|
|
05-10-2011, 03:06 PM
|
#6 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
That is because you are always replacing the keys in $this->p which is never changing with the previous replaces, so the only one that you see is the last replace.
You are never appending previous changes. try chaning = to .= in your vars function.
I created this class based on you, using the syntax of PHP5 objects. Maybe you are using PHP4 and that is why you are using that syntax. But I will advice if you can to at least upgrade to PHP5, there are a lot of improvements to objects in that version. Here is my take on the class:
php Code:
<?phpclass temp { private $p; private $built; public function __construct($file = null) { $this-> p = is_null($file) ? '' : $file; $this-> built = ''; } public function getdata ($file){ $this-> p = $file; return $this-> p; } public function vars ($data){ foreach($data as $k => $v){ $this-> built .= str_replace('{'. $k. '}', $v, $this-> p); } return $this; } public function output () { echo $this-> built; }}$temp = new temp (); $temp-> getdata("<h1>{TIT}</h1><br />{DES}\n"); // or $temp = new temp('<h1>{TIT}</h1> <p>{DES}</p>'.PHP_EOL);$input = array(); foreach(range(1, 20) as $n) { $input[] = array('title' => 'TITLE '. $n, 'description' => 'desc'. $n); }//usign input as dummy textforeach($input as $vars) { $temp-> vars(array('TIT' => $vars[ 'title'], 'DES' => $vars[ 'description'] )); }echo $temp-> output();
|
|
|
|
05-11-2011, 07:14 AM
|
#7 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
But why is it still showing like
TITLE 1
{DES}
{TIT}
desc1
i mean the
{DES}
{TIT}
did you got what i mean sorry for my bad english..
|
|
|
|
05-11-2011, 09:29 PM
|
#8 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
I see what you mean.
There is a problem in the vars function you where right with the = instead of concatenating it with .=
But there was another problem in the logic of multiples in a template.
The template class can only handle 1 template and can only replace 1 instance, but you want multiple copies of that template with different values. So I made some changes.
php Code:
class temp { private $p; private $built; public function __construct($file = null) { $this-> getdata(is_null($file)? '': $file); } public function getdata ($file){ $this-> built = $this-> p = $file; return $this; } public function reset() { $this-> built = $this-> p; return $this; } public function vars ($data){ foreach($data as $k => $v){ $this-> built = str_replace('{'. $k. '}', $v, $this-> built); } return $this; } public function output () { return $this-> built; }}$temp = new temp ("<h1>{TIT}</h1><br />{DES}\n"); $input = array(); foreach(range(1, 20) as $n) { $input[] = array('title' => 'TITLE '. $n, 'description' => 'desc'. $n); }//usign input as dummy textforeach($input as $vars) { echo $temp-> reset() -> vars(array('TIT' => $vars[ 'title'], 'DES' => $vars[ 'description'] )) -> output(); }
You can see that in the loop that we feed the data to the template, it is just reseting the template, to start with the vars. And also that it is echoing because the temp class can't accumulate multiple copies.
|
|
|
|
|
The Following User Says Thank You to tony For This Useful Post:
|
|
05-12-2011, 06:17 AM
|
#9 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Wohh really thanks a million really i don't know how can i replay you back with this... and if you ever thought of creating a forums script them please do let me know..
|
|
|
|
05-24-2011, 11:34 AM
|
#10 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Ok i did my homework and worked hard on finding out this problem now i have two tables category and sub category and i want to loop is and display all the rows like:
CATEGORY
- SUBCATEGORY
- SUBCATEGORY
- SUBCATEGORY
hope you understood now there are many categories and there subcategories how do i loop it like that?? i use while loop but still no good...
EDIT: It is working good with the same key names but not with different ones...
Last edited by Keshav : 05-24-2011 at 12:03 PM.
|
|
|
|
05-24-2011, 01:21 PM
|
#11 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
My guess is that subcategory has a Foreing Key linking to category making it a 1 to many relationship. The simple solution would be a loop within a loop.
something like
PHP Code:
<?php$cats = mysql_query('SELECT * FROM `categories`'); while ($cat = mysql_fetch_assoc($cats)) { echo $cat[ 'name']. '<br />'; $subcats = mysql_query('SELECT * FROM `subcategories` WHERE `category_idfk` = '. $cat[ 'id'] ); while ($subcat = mysql_fetch_assoc($subcats)) { echo '- '. $subcat[ 'name']. '<br />'; } mysql_free_results ($subcats); }mysql_free_results ($cats); ?>
This is a very simple rudimentary example. But it gives you the idea of how you might go with it.
|
|
|
|
05-25-2011, 05:54 AM
|
#12 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
No i know that but i want it work with the template class like:
CAT
-SUBCAT
and the CAT will replace and display the category data from the database and SUBCAT will display the subcategory data from the database. i want to make it database driven template class how do i do that? with the same template class only because it doesn't work properly if i call it in different arrays and if i call it from the same array then it won't loop.
|
|
|
|
05-25-2011, 01:47 PM
|
#13 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
Then, you would need to collect the data and then make the loops again. It depends also of how your template file is marked-up.
The template system that you have now, can't handle loops that replaces variables values (for example kohana's views or smarty) instead of replacing plain text. So you would have to make the loops yourself. something probably like this:
PHP Code:
<?php$cats_res = mysql_query('SELECT * FROM `categories`'); $items = ''; $subitems = ''$list = new temp ('<ul>{ITEMS}</ul>'); while ($cat = mysql_fetch_assoc($cats_res)) { $items .= '<li>'. $cat[ 'name'] $subcats_res = mysql_query('SELECT * FROM `subcategories` WHERE `category_idfk` = '. $cat[ 'id'] ); if(mysql_num_rows($subcats_res) > 0) { while ($subcat = mysql_fetch_assoc($subcats_res)) { $tmp .= '<li>'. $subcat[ 'name']. '</li>'.PHP_EOL; } $subitems = $list-> reset() -> vars(array('ITEMS' => $tmp))-> output(); $items .= PHP_EOL. $subitems; } $items .= '</li>'; mysql_free_results ($subcats); }mysql_free_results ($cats); ?>echo $list-> reset()-> vars(array('ITEMS'=> $items))-> output();
I haven't tested this code, but it is something like this. As you can see it doesn't help to separate the concerns of html and processing code.
|
|
|
|
05-26-2011, 01:10 PM
|
#14 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
Yeah, free scripts would help you. and even if you are not allowed to use them and tweak them (for whatever reason) you can learn from them too.
|
|
|
|
05-26-2011, 05:24 PM
|
#15 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Ya i will try but is there a way to make a template engine that works with str_replace?
|
|
|
|
05-26-2011, 06:09 PM
|
#16 (permalink)
|
|
The Addict
Join Date: Aug 2008
Posts: 312
Thanks: 8
|
There is like the one that we kind of come up in here. but it will be of limited functionality when it comes to looping content.
|
|
|
|
05-27-2011, 06:06 AM
|
#17 (permalink)
|
|
The Wanderer
Join Date: Apr 2011
Posts: 10
Thanks: 1
|
Still could you please tell me
EDIT:
Ok i made this but its displaying the main category every time before the subcategory like this:
CATEGORY:
-SUBCATEGORY1
CATEGORY:
-SUBCATEGORY2
CATEGORY:
-SUBCATEGORY3
But i want it like:
CATEGORY:
-SUBCATEGORY1
-SUBCATEGORY2
-SUBCATEGORY3
and its also looping the whole template data every time a new variable is added this is the code:
PHP Code:
class temp{
var $p;
var $b;
var $val = array();
function get_temp($file){
$this->b = file_get_contents($file);
return $this->b;
}
function reset(){
$this->p = $this->b;
return $this->p;
}
function vars($key, $value){
$this->reset();
$this->val[$key] = $value;
foreach($this->val as $key => $value){
$this->p = str_replace("{".$key."}", $value, $this->p);
}
return $this->p;
}
function show(){
echo $this->p;
}
}
$temp = new temp();
$temp->get_temp("temp.html");
mysql_select_db($database_config, $config);
$query_bbcat = "SELECT * FROM category";
$bbcat = mysql_query($query_bbcat, $config) or die(mysql_error());
while($row_bbcat = mysql_fetch_assoc($bbcat)){
$temp->vars("TIT", $row_bbcat['title']);
$temp->vars("DSC", $row_bbcat['description']);
mysql_select_db($database_config, $config);
$query_bbsubcat = "SELECT * FROM fsubcategory WHERE cid={$row_bbcat['id']}";
$bbsubcat = mysql_query($query_bbsubcat, $config) or die(mysql_error());
while($row_bbsubcat = mysql_fetch_assoc($bbsubcat)){
$temp->vars("FTIT", $row_bbsubcat['title']);
$temp->vars("FDSC", $row_bbsubcat['description']);
$temp->show();
}
}
Last edited by Keshav : 05-27-2011 at 06:47 PM.
|
|
|
|
08-11-2011, 06:52 AM
|
#18 (permalink)
|
|
The Visitor
Join Date: Aug 2011
Posts: 2
Thanks: 0
|
It seems that you want to show a list of rows, but they show the same data all over, because of the 'var' key. You might want to take a look at at using a combination of extract and to the output buffer functions.
_________________
MBA consultants in India
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|