PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,035 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,274 people online right now. Registration is fast and FREE... Join Now!




Dynamic page generation

 

Dynamic page generation, load x content into x divs depending on page title

ApacheSoft

4 Nov, 2009 - 06:59 AM
Post #1

New D.I.C Head
*

Joined: 20 Sep, 2009
Posts: 13


My Contributions
Hi everyone... I'm working on a new site at the moment, and I'm exploring the idea of generating everything from a database. I come to you today with (I think) 2 problems...

I think my main problem is with the database design. I just can't seem to get my head around it!

Firstly.. I have generated this code for my menu and submenu. I havent yet come up with a way to generate a different submenu, depending on what main menu page im on.

Here is the code:

CODE

<?php
        
        $username = "xxxxxx";
        $password = "xxxxxx";
        $database = "xxxxxx";

        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die("Unable to select database");
        
        $query = "SELECT * FROM menu_table WHERE menu_id = 'mainmenu'";
        $result = mysql_query($query) or die(mysql_error() . ' in ' . $query);
        $num = mysql_numrows($result) or die(mysql_error() . ' in ' . $result);

        $query2 = "SELECT * FROM menu_table WHERE menu_id = 'subNav'";
        $result2 = mysql_query($query2) or die(mysql_error() . ' in ' . $query2);
        $num2 = mysql_numrows($result2) or die(mysql_error() . ' in ' . $result2);


        mysql_close();

    ?>


    <div id="navigation-bg">
        <div id="navigation">
            <ul class="mainMenu">

                <?php
                    $i=0;
                    while ($i < $num)
                    {


                        $Class = mysql_result($result,$i,"menu_Class");
                        $Title = mysql_result($result,$i,"menu_Title");
                        $Link = mysql_result($result,$i,"menu_Link");?>

                        <li><a href="<?php echo $Link ?>" class="<?php echo $Class ?>" title="<?php echo $Title ?>"><?php echo $Title ?></a></li>
                        

                        <?php
                        $i++;

                    }?>

            </ul>

            <br class="spacer">

            <ul class="subNav">

                <?php


                    $i=0;
                    while ($i < $num)
                    {
                    
                        if ( $i == "0" )
                        {    

                            $Title = mysql_result($result2,$i,"menu_Title");
                            $Link = mysql_result($result2,$i,"menu_Link");
                ?>

                            <li class="noBg"><a href="<?php echo $Link ?>" title="<?php echo $Title ?>"><?php echo $Title ?></a></li>
                        <?php
                        }
                        else
                        {

                            $Title = mysql_result($result2,$i,"menu_Title");
                            $Link = mysql_result($result2,$i,"menu_Link");?>

                            <li><a href="<?php echo $Link ?>" class="<?php echo $Class ?>" title="<?php echo $Title ?>"><?php echo $Title ?></a></li>
                            <?php
                        }

                    $i++;

                }?>
            

            </ul>        
        </div>
    </div>


Now... I've managed to get the main menu loading perfectly. I just have to add in an if statement later to get my css working, but thats not really important right now. What I'm having trouble doing, is getting the submenu to load a different set of values, depending on what link is clicked in the main menu. Where am I going wrong? What am I missing?

My next problem, is.. can i store the entire contents of my page in a database (will be 100% plain text, all images are handled with CSS), and load them into the correct divs the exact same way as i have done with the menus here?

Will i need to create a table with each page say as the UID, and then... I don't know... I fail at databases! anime2.gif

Any help is greatly appreciated, and thanks smile.gif

User is offlineProfile CardPM
+Quote Post


Fratyr

RE: Dynamic Page Generation

6 Nov, 2009 - 04:22 AM
Post #2

New D.I.C Head
*

Joined: 10 Apr, 2008
Posts: 25


My Contributions
Create a table for main menu titles, with ID, name and probably hard coded link (it's good enough for a start)

Create a table for sub menu links/titles, with ID, main menu title ID they belong to, name, and hard coded link

Create table for pages, with ID, main menu title ID and submenu link ID they belong to, page name, page title (html title), content, NO link and many more.

So what do we have here:
submenu links is your bottom line, the pages itself, like:

Main Menu Title ID #1: About Us
Sub Menu Title ID #1: Company Goals, Belongs to Main Menu Title ID #1
Page Company Goals ID #1, Belongs to Main Menu Title ID 1 and Submenu Link ID 1, content etc...

Off topic advice:

1) Create class for mySQL to operate will mysql functions directly, without typing them every time again and again.

2) Creating a site that generates all data from database is a bad and vulnerable idea.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 08:38AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month