Jump to content

Display Script Need Help!


coldfiretech

Recommended Posts

Hello, i recent migrated from a system that my partner had purchased to run a subscription website and whatnot... Problem is this, i am trying to migrate the entire system over to php just becuase i didnt like all the extra garbage that came along with the other site.. Now they were using cgi and what not which i have no clue about..  But  on all of his html pages  he could just type %%field_name%% and when the page was displayed it just showed the data that was in the field...

 

Is there a way i can do this in php?  If so could someone kindly show me an example?  Thanks guys!

-Matt

Link to comment
Share on other sites

Maybe it's the case of a template engine, which separates presentation from code. If that's the case, %%field_name%% is parsed and is replaced with the appropriate content. There are full blown template engines for doing this (ie. Smarty), but just to give you an idea:

 

header.tpl

<h1>This is the page header</h1>
<p>This is the slogan</p>

 

template.php

%%page_header%%
<h2>This is some sub header</h2>
<p>Content</p>

 

index.php

<?php
$header = file_get_contents('header.tpl'); //get the contents of the header file; all the html code
$template = file_get_contents('template.php'); //get the contents of template.php
$page = str_replace('%%page_header%%', $header, $template); //replace %%page_header%% with the actual header code
echo $page; //print the html
?>

Link to comment
Share on other sites

Thanks,  that is the case.. But what i am looking for is to pull data from fields in the database..

It is a profile site.  On all the display and modify pages he's got %%myname%%  and like %%mybirthday%%  and when the page is viewed, it search the database using the session id and display those fields where the %%myname%% text is at..

 

Sorry if i am repeating myself, new to php

Link to comment
Share on other sites

okay so i think i might have came up with a solution to my problem just dont know how to code it in php.    So here is my idea

 

create a varible for each row of data in the database and then just use the str_replace function like

echo str_replace('%%firstname%%, $firstname)

 

I dont really know. Just had an idea.. If anyone thinks this will work and would be an alright idea let me know.. 

Link to comment
Share on other sites

Okay so here is what ive got.. Problem is this, it is displaying the contents of the page twice.. becuase i dont know how to properly code this.  Can you replace multiple strings at once?  If so can someone show me or point me in the direction of a tutorial on this.  Thanks..

 

-Matt

 

Template.php

<html>
<head>
</head>
<body>
%%first_name%%<br />
%%middle_name%%<br />
%%last_name%%<br />
%%suffix%%<br />
</body>
</html>

 

Index.php

<?php
$fname = "Matthew";
$lname = "Stacks";
$template = file_get_contents('template.php'); 
$one = str_replace('%%first_name%%', $fname, $template); 
$two = str_replace('%%last_name%%', $lname, $template);
echo $one;
echo $two;
?>

Link to comment
Share on other sites

It's because you are running str_replace() twice. Try this:

 

<?php
$replace= array("Matthew", "Stacks");
$search = array("%%first_name%%", "%%last_name%%");
$page = file_get_contents('template.tpl');
$page = str_replace($search, $replace, $page);
echo $page;
?>

 

I run str_replace() only once by using arrays to search multiple strings. Hope this helps.

Link to comment
Share on other sites

Okay so there has got to be an easier way to do this..  I have been sitting pounding my head for hours trying to figure this out and i just cant do it.. So i am going to move on. I am in need of some serious insight on this... Here goes...

 

I am trying to build a multi-level subscription website where users can enter in information about them selfs and what not.. Now there are alot of fields..  Like i said in my first post we recently tried to migrate our old system into php.. Now were talking about a serveral pages with like 300 fields being displayed and updated by  %%field_name%%.  So I figured out how to connect to the db and make a variable for each row in the db then search in the file and replace it but this just seems like i am going way down the wrong path.. I want to do this right and if i have to rebuild so be it but i am in some real need of direction of how to design this site and call these fields easily. This site has full blown css and tons of tables and graphics so it hasnt been an easy migration.. 

 

Any advice would be greatly appreciated..

~Matt   

coldfiretech@yahoo.com Please Email Me!!!

 

 

Link to comment
Share on other sites

The templating code i suggested is far from being complex and for a real site, you've got to write your own [very well designed] template system. Believe me, it's not an easy task and as you're a beginner you can rethink about the template engine. At least use an existing one (like smarty) but it also has it's learning curve and for now you're in need to appropriately learn php itself.

 

When I started coding php, I had no idea on application design, objects, good coding practices, database design, etc, so I just went on writing stuff as i thought it was best. Surely I'm not an expert now, but i can write an application way better than i did before. That's what you can do too. If you need to have it online quick, write it as you can and if your aim is to learn php development, increase your skills and knowledge by reading and coding. Then you can rewrite the site using what you've learned.

 

If the aim of learning php is just to finish this site and your career path is completely different, then its completely another matter. Hire a freelance coder and have the work done.

 

Anyway, you can write more technically how's the site going to work, so someone can suggest a good way to design it.

Link to comment
Share on other sites

Thank you,  I am actually really interested in learning php...  I have been doing alot of reading and learning from trial and error..

I appreciate you reply,  i am going to redesign my database it need some normalisation in a bad way.  I think im going to just sit down and draw out the entire thing and then go from there... I will report back once i have some of the basic's acomplished..

 

 

Thanks again!

-Matt

Lead Programmer

Cold Fire Technology LLC.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.