guys i'm new to php and learning templating. the scenerio is i'm passing a customers name in an array to a class. in my template file i have a variable (or shall i call it a variale) name #customer. my problem is how to populate a select dropdown in the design.htm as the customer name has to be looped i have a index.php (used to fetch data from the database) myclass.php ( used to merge the class with the design) design.htm (the layout design) i'm nuts in this kindly help
search.php <?php class srch{ public $template; public function srch($template){ $this->template=file_get_contents($template); } public function output(){ echo $this->template; } public function replace($tags){ if (sizeof($tags) > 0){ foreach ($tags as $tag => $data) { $this->template = eregi_replace("#" . $tag , $data, $this->template); } } } } ?> index.php <?php require "include/config.inc.php"; // just have a database connection require "classes/search.php"; $template="theme/carsearch.htm"; $sear = new srch($template); $row=mysql_query("select brand from new_specs"); $res=mysql_fetch_array(); $brand= array($res); $sear->replace($brand); $sear->output(); ?> carsearch.htm <html> <body bgcolor=red> <form> Brand <select name=brnd> #brand </select> <br> </form> </body> </html>
First, you need to fix your class. It's not like C++ where the constructor function is the class name, you use __construct function. So, replace public function srch( ) with this: public function __construct($template) { $this->template = file_get_contents($template); } PHP: But still. It doesn't make any sense. Public function output( ) will just output your template's location ( 'theme/carsearch.htm' ), not the template itself. Use include( ) for that. Replace: $template="theme/carsearch.htm"; PHP: With this: include "theme/carsearch.htm"; PHP: So now, the constructor is unnecessary, take it off. Alternately, if you still want to use your constructor, you can do this: public function __construct($template) { include $template; } PHP: Public function output( ) and class member variable '$template' are now unnecessary, take those off. EDIT: woops, i didn't see file_get_contents( ).. It should still work with output( )
thanks for correcting me. but how do i make the results appear in select dropdown list created in the carsearch.htm i'm just able to display one value in the select option. what about the remain values.........!!!
That's because you're fetching only one row from the database: $res=mysql_fetch_array(); PHP: Put it in a loop and keep fetching row until the end of db: $result=mysqli_query("select brand from new_specs"); while($row = $result->fetch_array( )) { $res[] = array('brand' => $row['brand']); echo $row['brand'] . "<br />"; // output each row } PHP:
i'm able to store all the values in the array and able to pass it to the class. actually i want to keep my business logic and presentation logic on two different files and don't want to mix them. so actually i'm creating a <select> </select> control in the carsearch.htm (template file) and i'm fetching the values from database in index.php now where do i merge these two files and how so that my <select></select> control gets populated with the values passed from index.php
Depends on how much work you want to do with your template. You'll need language-like logic handlers to do what you want with a full separation, but then your designer is going to need to learn your template language. You want to be able to loop through your array and spit the values back wrapped in an <option>, but maybe you'll also want them in an <li> down the line but you don't want to use different code. You can do a semi-mix and bypass your own template logic by adjusting your index file's code in such a way that you can add a prefix and suffix to your 'brand' field output. You'll have to throw the code that gets each row into its own function and add a param to the function. You might decide to use a numeric value and name it $outputType as the parameter. Then you can do a simple if statement. If it's 0 (which could be the default if you'd like) then it wraps it in <option value="$field">$field</option>, 1 could be <li>$field</li>, and so forth. Edit: I just realized maybe this isn't as clear as it could be. You're basically forced to use PHP here in your presentation if you don't want to go absolutely crazy with your template code. The end result would be something along the lines of (in your view / presentation): <?php echo OutputArray(#brand); ?> [this would output it as an option of a select control if you use my idea from above] <?php echo OutputArray(#brand, 1); ?> [this would output it as a list item if you use my idea from above] Then index.php has a function called OutputArray (which is actually a bad spot for it, but I'm not writing your application, you are) which takes the incoming array and returns it with a specific prefix/suffix depending on argument #2 (which is the outputType this case) and returns it as a string.