[plug] mysql/associativearrays/objects/something wierdness.

Shayne O'Neill shayne at guild.murdoch.edu.au
Mon Sep 27 12:59:21 WST 2004


Ah. inserts it into a html template.

Heres the module that does that;-

class renderer{

        var $templatebody = "";
        var $templatefields = array();
        var $outtext = "";

        function retrieve_template($templatename)
        {
                global $rootdir;
                $filename = $rootdir.'templates/'.$templatename;
                $handle = fopen($filename,'r');
                $this->templatebody = fread($handle, filesize($filename));
                fclose($handle);
        }
        function apply_template()
        {
        $outp = $this->templatebody;
        while (list($key, $val) = each($this->templatefields))
                {
                        $outp = str_replace($key,$val,$outp);
                }
        $this->outtext = $outp;
       return $outp;
        }
        function apply_template_br_unslash()
        {
        $outp = $this->templatebody;
        while (list($key, $val) = each($this->templatefields))
                {
                        $outp =
		str_replace('['.$key.']',stripslashes($val),$outp);
                }
        $this->outtext = $outp;
        return $outp;
        }
}

The way it works is you feed it a html file with little placeholders like
this;-

<h1>[heading]</h1> This is field1:[field1]<br>This is field2:[field2]<br>

you feed those fields ins as follows

renderer->templatefields['heading']="heading!!!"
renderer->templatefields['field1']="heading!!!"
renderer->templatefields['field2']="heading!!!"

then you run render->apply_template_br() to get the template with the
fields filled in.

Its naive as hell, but it works well, and if you pass it an associative
row from a db with the correct placeholders, it automatically does the
magic well.

Except in this case with this one dam field :(

--
"Well, I think if you say you're going to do something and don't do
it, that's trustworthiness."
-- George Bush on CNN online chat, Aug.30, 2000
RIAA Copyright notice trap: http://guild.murdoch.edu.au/~shayne/

On Mon, 27 Sep 2004, James Devenish wrote:

> Hi Shayne,
>
> What do you mean it "inserts"? The code you showed only does a
> SELECT. Are you saying the ids in the database are correct but
> that mysql_fetch_assoc($records) seems to return the wrong thing?
>
>
> _______________________________________________
> PLUG discussion list: plug at plug.linux.org.au
> http://mail.plug.linux.org.au/cgi-bin/mailman/listinfo/plug
> Committee e-mail: committee at plug.linux.org.au
>




More information about the plug mailing list