mysql related question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michel

    mysql related question

    Hi group,

    just wondering:

    I have a database, containing the following fields:

    name
    address
    zip
    email

    and a template for my page, containing the following tags:

    [[name]] [[address]] [[zip]] [[email]]

    Would there be a quick way to replace those tags with the data read from the
    database without doing this:

    $line=mysql_get _row($res) ; // provided we already have connection and data
    selected
    $html_to_show=p reg_replace("/\[\[name]]/",$line[0],$html_to_show) ;
    $html_to_show=p reg_replace("/\[\[address]]/",$line[1],$html_to_show) ;
    $html_to_show=p reg_replace("/\[\[zip]]/",$line[2],$html_to_show) ;
    $html_to_show=p reg_replace("/\[\[email]]/",$line[3],$html_to_show) ;

    (never mind the preg here or any typo's. this is a quick question)

    Reason for me to ask, though code above works:
    I need to create something dynamic. I would like to scan and replace every
    instance in my template, looking like a tag ( [[some_tag_name]] ) by
    the value of it's corresponding fieldname from my table.
    I am really wondering how, but there's got to be an easier way to do this.
    Currently I have 61 fields in one table and that kinda works towards RSI if
    I have to code it all like this.
    Anyone with a good suggestion, apart from reading the manual? (I am already
    doing that)

    Thank you very much in advance,

    Michel

    PS: sorry for double posting this.


  • Andrzej Bednarczyk

    #2
    Re: mysql related question

    maybe instead of building your own template system (based on preg_replace)
    try to use some simple (!) existing one: try Xipe
    (PEAR::HTML_tem nplate_Xipe). It works this way that you don't operate on a
    bunch of $template->setValue($some Value), but simply auto-input the values
    of the template-variables with existsing-ones in php. This means that you
    can use php-arrays as well in the template.

    i.e. in the template file you can have:

    (...SOME HTML...)<strong ><{$_myArray['name']}</strong>(...SOME HTML...)

    and on the php code BEFORE compiling this template you have to create an
    array $_myArray with at last one value $_myArray['name']. This can be done
    ie with:

    while ($row = mysql_fetch_row s($result)) {

    $_myArray['name'] = $row[0];
    // etc.
    }

    cheers
    andrew
    [color=blue]
    > Hi group,
    >
    > just wondering:
    >
    > I have a database, containing the following fields:
    >
    > name
    > address
    > zip
    > email
    >
    > and a template for my page, containing the following tags:
    >
    > [[name]] [[address]] [[zip]] [[email]]
    >
    > Would there be a quick way to replace those tags with the data read from
    > the database without doing this:
    >
    > $line=mysql_get _row($res) ; // provided we already have connection and
    > data selected
    > $html_to_show=p reg_replace("/\[\[name]]/",$line[0],$html_to_show) ;
    > $html_to_show=p reg_replace("/\[\[address]]/",$line[1],$html_to_show) ;
    > $html_to_show=p reg_replace("/\[\[zip]]/",$line[2],$html_to_show) ;
    > $html_to_show=p reg_replace("/\[\[email]]/",$line[3],$html_to_show) ;
    >
    > (never mind the preg here or any typo's. this is a quick question)
    >
    > Reason for me to ask, though code above works:
    > I need to create something dynamic. I would like to scan and replace every
    > instance in my template, looking like a tag ( [[some_tag_name]] ) by
    > the value of it's corresponding fieldname from my table.
    > I am really wondering how, but there's got to be an easier way to do this.
    > Currently I have 61 fields in one table and that kinda works towards RSI
    > if I have to code it all like this.
    > Anyone with a good suggestion, apart from reading the manual? (I am
    > already doing that)
    >
    > Thank you very much in advance,
    >
    > Michel
    >
    > PS: sorry for double posting this.[/color]

    Comment

    • michel

      #3
      Re: mysql related question

      Hi dude, thanks for responding.
      trick though is that the time I need to start using these classes, is as
      good as the time it takes to do this myself, probably, with some info.
      Latter, though sticks in my mem and I would have actually learned a deal.

      Thanks for pointing me to this solution though. never hurts to see more
      resources.

      Mich

      "Andrzej Bednarczyk" <andrzej@kreo9. NOSPAM.com> wrote in message
      news:40d2c6f1@n ews.home.net.pl ...[color=blue]
      > maybe instead of building your own template system (based on preg_replace)
      > try to use some simple (!) existing one: try Xipe
      > (PEAR::HTML_tem nplate_Xipe). It works this way that you don't operate on a
      > bunch of $template->setValue($some Value), but simply auto-input the values
      > of the template-variables with existsing-ones in php. This means that you
      > can use php-arrays as well in the template.
      >
      > i.e. in the template file you can have:
      >
      > (...SOME HTML...)<strong ><{$_myArray['name']}</strong>(...SOME HTML...)
      >
      > and on the php code BEFORE compiling this template you have to create an
      > array $_myArray with at last one value $_myArray['name']. This can be done
      > ie with:
      >
      > while ($row = mysql_fetch_row s($result)) {
      >
      > $_myArray['name'] = $row[0];
      > // etc.
      > }
      >
      > cheers
      > andrew
      >[color=green]
      > > Hi group,
      > >
      > > just wondering:
      > >
      > > I have a database, containing the following fields:
      > >
      > > name
      > > address
      > > zip
      > > email
      > >
      > > and a template for my page, containing the following tags:
      > >
      > > [[name]] [[address]] [[zip]] [[email]]
      > >
      > > Would there be a quick way to replace those tags with the data read from
      > > the database without doing this:
      > >
      > > $line=mysql_get _row($res) ; // provided we already have connection and
      > > data selected
      > > $html_to_show=p reg_replace("/\[\[name]]/",$line[0],$html_to_show) ;
      > > $html_to_show=p reg_replace("/\[\[address]]/",$line[1],$html_to_show) ;
      > > $html_to_show=p reg_replace("/\[\[zip]]/",$line[2],$html_to_show) ;
      > > $html_to_show=p reg_replace("/\[\[email]]/",$line[3],$html_to_show) ;
      > >
      > > (never mind the preg here or any typo's. this is a quick question)
      > >
      > > Reason for me to ask, though code above works:
      > > I need to create something dynamic. I would like to scan and replace[/color][/color]
      every[color=blue][color=green]
      > > instance in my template, looking like a tag ( [[some_tag_name]] )[/color][/color]
      by[color=blue][color=green]
      > > the value of it's corresponding fieldname from my table.
      > > I am really wondering how, but there's got to be an easier way to do[/color][/color]
      this.[color=blue][color=green]
      > > Currently I have 61 fields in one table and that kinda works towards RSI
      > > if I have to code it all like this.
      > > Anyone with a good suggestion, apart from reading the manual? (I am
      > > already doing that)
      > >
      > > Thank you very much in advance,
      > >
      > > Michel
      > >
      > > PS: sorry for double posting this.[/color]
      >[/color]


      Comment

      • Brad Kent

        #4
        Re: mysql related question

        I use something like the following:


        $pageoutput = preg_replace('/\[\[\s*(.*?)\s*\]\]>/ie',"get_replac ement('\\1')",
        $template);
        // I think the "]" needs escaped? ie "\]" above?
        // you just had "]"... I could be wrong


        function get_replacement ($what)
        {
        /*
        obviously here is where you would lookup "what"
        in your database or whatever
        */
        return $what;
        }

        Comment

        • michel

          #5
          Re: mysql related question

          Aye Brad,

          Thanks for your input. The brackets " ] " do not need to be pre-ed by \,
          only the left brackets do " [ " needs to be "\[".....

          I will have a look at what you're suggesting and get back with results.....

          Mich

          "Brad Kent" <bkfake-google@yahoo.co m> wrote in message
          news:7ad3d45b.0 406180628.3e585 a6@posting.goog le.com...[color=blue]
          > I use something like the following:
          >
          >
          > $pageoutput =[/color]
          preg_replace('/\[\[\s*(.*?)\s*\]\]>/ie',"get_replac ement('\\1')",[color=blue]
          > $template);
          > // I think the "]" needs escaped? ie "\]" above?
          > // you just had "]"... I could be wrong
          >
          >
          > function get_replacement ($what)
          > {
          > /*
          > obviously here is where you would lookup "what"
          > in your database or whatever
          > */
          > return $what;
          > }[/color]


          Comment

          • Chung Leong

            #6
            Re: mysql related question


            "michel" <no@spam.please > wrote in message
            news:cau7cf$6kb $1@news.cistron .nl...[color=blue]
            > Hi group,
            >
            > just wondering:
            >
            > I have a database, containing the following fields:
            >
            > name
            > address
            > zip
            > email
            >
            > and a template for my page, containing the following tags:
            >
            > [[name]] [[address]] [[zip]] [[email]]
            >
            > Would there be a quick way to replace those tags with the data read from[/color]
            the[color=blue]
            > database without doing this:
            >
            > $line=mysql_get _row($res) ; // provided we already have connection and[/color]
            data[color=blue]
            > selected
            > $html_to_show=p reg_replace("/\[\[name]]/",$line[0],$html_to_show) ;
            > $html_to_show=p reg_replace("/\[\[address]]/",$line[1],$html_to_show) ;
            > $html_to_show=p reg_replace("/\[\[zip]]/",$line[2],$html_to_show) ;
            > $html_to_show=p reg_replace("/\[\[email]]/",$line[3],$html_to_show) ;
            >
            > (never mind the preg here or any typo's. this is a quick question)
            >
            > Reason for me to ask, though code above works:
            > I need to create something dynamic. I would like to scan and replace every
            > instance in my template, looking like a tag ( [[some_tag_name]] ) by
            > the value of it's corresponding fieldname from my table.
            > I am really wondering how, but there's got to be an easier way to do this.
            > Currently I have 61 fields in one table and that kinda works towards RSI[/color]
            if[color=blue]
            > I have to code it all like this.
            > Anyone with a good suggestion, apart from reading the manual? (I am[/color]
            already[color=blue]
            > doing that)[/color]

            strtr() is probably the best function to use in this case.

            $row = mysql_fetch_ass oc($res);
            foreach($row as $name => $value) {
            $tr["[[{$name}]]" = htmlspecialchar s($value);
            }
            $html_to_show = strtr($html_to_ show, $tr);


            Comment

            Working...