Jump to content

Recommended Posts

ok so basically this is the thing

 

i have this page

 

question.gif

 

now so far what i usually done is copied and pasted index.php  3 times and changed it to about, contact and news . php  and i had my 4 page site.

 

now i wanna make it easier for me when i wanna call for a page to load.

i kinda understand the idea if and else.. but what about in the case of clicking the LINKs and than loading it in a different place (maincontent)

 

so thats kinda my question..

 

any pointers to tutorials, or feel free to explain here

thanks in advantage.

Link to comment
https://forums.phpfreaks.com/topic/73887-if-clicked-load-this-page-noob-alert/
Share on other sites

Well, you could use 4 different pages?

 

Or look into using the GET method and putting the page into the url. For example, the links would be formatted like this:

index.php?pg=about

 

And it would pull the variable and try to load an about.php page with include() or some other function like that. If you do it that way, you need to have some type of check to make sure that the page exists and if not return a 404. That's pretty easy. You would just loop through all the files on that directory level and see if one exists that is called whatever variable you're looking for.

 

Hope that helps. I've done it that way on 2 different sites, as I'm sure many people here have, so if you get stuck just come back and show us what code is giving you the trouble.

You have three links and some processing code....

 

<a href="?page='one'>LINK ONE</a>
<a href="?page='two'>LINK TWO</a>
<a href="?page='three'>LINK THREE</a>
<?php

 if (isset($_GET['page'])) {
   switch ($_GET['page']) {
     case 'one':
       echo "this is one";
       break;
     case 'two':
       echo "this is two";
       break;
     case 'three':
       echo "this is three";
       break;
   }
 }

?>

I might modify that to use include() so the code doesn't become crowded. But I like the way you think thorpe ;)

 

<a href="?page='one'>LINK ONE</a>
<a href="?page='two'>LINK TWO</a>
<a href="?page='three'>LINK THREE</a>
<?php

  if (isset($_GET['page'])) {
    switch ($_GET['page']) {
      case 'one':
        include("one.php");
        break;
      case 'two':
        include("two.php");
        break;
      case 'three':
        include("three.php");
        break;
    }
  }

?>

Ah see, if your going to use include you best make sure its safe to include the files. Otherwsie people can do vicious things to your site. You can also get rid of the switch all together.

 

<a href="?page=one">LINK ONE</a>
<a href="?page=two">LINK TWO</a>
<a href="?page=three">LINK THREE</a>
<?php

  $valid = array('one','two','three');
  if (isset($_GET['page'])) {
    if (in_array($_GET['page'],$valid)) {
      include $_GET['page'].'.php';
    }
  }

?>

oh lol

wow thats fast responses here

 

http://kurruptsystem.com/media/random/question/

 

thats what i made up

 

the test.php has the "coding" in it

 

<body>
<div align="center" style="padding: 2px;">
<div align="center" style=" width:600px; height:100px; background-color:#999999;">Banner </div>
<div align="center" style=" width:600px; height:20px; background-color: #CCCCCC;">
<a href="?page=one">LINK ONE</a>
<a href="?page=two">LINK TWO</a>
<a href="?page=three">LINK THREE</a>
</div>
<div align="center" style=" width:600px; height:50px; background-color:#999999;">
  <p>Div dedicated to the people that helped me on that thread<br />
    Thanks!</p>
  </div>
<div align="center" style=" width:600px; height:auto; background-color: #333333; color:#FFFFFF;">
<?php
  if (isset($_GET['page'])) {
    switch ($_GET['page']) {
      case 'one':
        include("one.php");
        break;
      case 'two':
        include("two.php");
        break;
      case 'three':
        include("three.php");
        break;
    }
  }
  ?>
</div>
</div>
</body>

 

and it actually worked.. ok time to try the array thing..

can u define the "people can do things to your site" ?

 

**

edit

 

 

copied and pasted the array

 

worked nicely..

u guys make php easy.. lol

I found a tutorial on this website about doing kinda what you want. but alas i am unable to find said tutorial right now but what it had done was create 1 giant table with the with your design and what you did was look at the table code till you find the section where you want your pages to go, and make this a file called header.php then take the rest of it and call it footer.php. thats what i did.

 

now on my files i just had the top line include 'header.php'; and when the page was done, include footer.php

 

then build your page the way you want. it was something like frames in html.

 

I did notice you have a fix or sorts already but this is something i have done, it was fun

yeah

well thats how i code my templates

 

banner.php

maincontent.php

footer.php

navigation.php

sidecategory1.php 

 

etc etc.

 

and than include them in

 

index.php

 

 

but my question was ( and it was answered)

instead of taking  index.php and copy paste 3 times and change name (for a 4 page site)

to have just one index.php  and to change the text needed to be changed by switching just to  about.php or w/e and in about.php i can simply have only the text.. no design required.

 

kinda know what i mean now ?

To do that you actually need only one page (index.php) and your content is stored within a database. This is the basic principle behind dynamic sites.

 

A quick example (you can build quite alot from here).

 

<?php

  // build the menu.
  $sql = "SELECT id,title FROM menu";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<a href=\"?id={$row['id']}\">{$row['title']}</a><br />";
      }
    }
  }

  // check to see if a link was checked, otherwise prepare a default id.
  if (isset($_GET['id'])) {
    $id = mysql_real_escape_string($_GET['id']);
  } else {
    $id = 1;
  }

  // build query to grab the content for this page.
  $sql = "SELECT data FROM content WHERE id = '$id' LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<p>{$row['data']}</p>";
    }
  }

?>

 

Of course this example is pretty stripped back, but there would be nothing stopping you copying and pasting that code, creating the correct tables / data and using it to dynamically generate a site with 10,000 pages with it.

Thorpe, would that then produce something like:

 

index.php

index.php?aboutus

index.php?blahblah

index.php?yadayada

 

Where it's basically dynamically generating the text in the content area depending upon what link is clicked? I've been wanting to learn this method.

Something like that. This part....

 

<?php

  // build the menu.
  $sql = "SELECT id,title FROM menu";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      while ($row = mysql_fetch_assoc($result)) {
        echo "<a href=\"?id={$row['id']}\">{$row['title']}</a><br />";
      }
    }
  }

?>

 

would generate something like (as an example)....

 

<a href="?id=1">home</a>
<a href="?id=2">about</a>
<a href="?id=3">something</a>

 

then this part....

 

<?php

  // build query to grab the content for this page.
  $sql = "SELECT data FROM content WHERE id = '$id' LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "<p>{$row['data']}</p>";
    }
  }

?>

 

if passed an id of 1 would produce the data related home.

 

Of course you could use named id's as in your example and in fact alot of the time this can be better. Using named id's allows you to use mod_rewrite rules which will clean your urls.

 

This really is the basics of how dynamic sites are built. Like I said, even that simple example I posted is capable of producing a site with 10000+ dynamic pages. its limitless really, though the menu would get pretty ugly.

alright

i kinda understand the code abit..

 

i have xampp

and i made a database..

and managed to connect my menu.php (trying to get a dynamic menu)

 

can u tell me how my table would look

i got too many options that i dont know what they mean..

 

Field

Type

Length

Attributes

Null

extra

comments..

etc. etc.

 

what exactly am i suppose to fill in those...

some are drop down menus with.. lots of more options.

 

thanks

How about use includes so you will have top.php leftmenu.php buttom.php

snd main_boddy1.php you can use CSS to do the layout you want.

 

page1.php will be inside this page top.php leftmenu.php buttom.php

snd main_boddy1.php

page2.php will be inside this top.php leftmenu.php buttom.php

snd main_boddy2.php

page3.php etc will be inside this top.php leftmenu.php buttom.php

snd main_boddy3.php

 

This way everyone of your pages will include the same template, so if you need to change your banner or add new links for your menues you just change it in one inlude page and all other pages will reflect the change.

 

If you wish you can put the pages in a database like thrope suggested.

 

Here is an example of CSS www.phsdl.net

 

The top left menu is the same on every page, right it is not an include, but if you going to build many pages it would be nice to have it as an inlude, so adding one new link will be reflected on all the Web pages.

 

 

thanks for the input.. but i know how to use include's

i've been using include function for a long time..

 

tho i would want to learn to get it more dynamic using databases.. since later on.. i might wanna learn how to make an easy edit page (CMS) that would edit the tables in the db.

 

i've looked at a couple of CMS tutorials.. but i dont wanna just copy and paste.. i actually wanna learn it.

IF you make one share the script with us. :)

 

Sounds like you are on the right track.

 

I have used a nuber of content managment softwaresand they all were badly build. The last one I used I could only link to internel pages in the menu.

 

Also if you can get some XML fancunality into it to move around manuales same as iGoogle homa page that would be very useful for person updating the pages.

 

Good luck

Ah see, if your going to use include you best make sure its safe to include the files. Otherwsie people can do vicious things to your site. You can also get rid of the switch all together.

 

But if you're using a switch, you could just include a default to include a 404 error page or something. That way, if it didn't match any of the conditions, it wouldn't include anything.

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.