Results 1 to 6 of 6

Thread: Variable in the URL

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default Variable in the URL

    Hi All,

    I'm trying to make a members page, which has personalized content, depending on what's in the URL: members.php?username_here

    Just a simple IF statement would suffice...

    Thanks,
    X96
    Last edited by X96 Web Design; 04-22-2009 at 03:27 AM. Reason: solveded
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    This won't be a simple IF statment I don't think. Unless you're planning on only having 2 - 3 members. You'll need to query a database to find if said member ID exists, then display relevant data depending on the records returned from the query...

    If however you do only plan to have a couple of members then it's pretty simple:

    PHP Code:
    if(isset($_GET['member'])) {

    if(
    $_GET['member'] == 1) {
    // All relevant code for this member here, example:

    echo "<h1>Welcome to {$membername}'s page!</h1>";
    }

    elseif(
    $_GET['member'] == 2) {
    // All relevant code for this member
    }

    // And so on...

    else {
    echo 
    "Member does not exist.";
    }

    This will be static, and mean that if you want to update the information on a members page you'll need to go and hardcode it in again. =/

    Sorry about the formatting, you know how it is when trying to indent online >.<

  3. #3
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    I have a working registration page and log in set up, so all the database stuff is there, I just need to pull in different content from different tables depending on what the $_GET is set to...

    Something like in Wordpress, where it has /index.php?page_id=53 and it displays a page depending on the page_id variable.

    Sorry if I'm not making it very clear...

    Thanks,
    X96
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Oh I see, you do have a database...

    Well then it would be a bit like this:

    PHP Code:
    <?php
    if(isset($_GET['member'])) {

        
    $member mysql_real_escape_string($_GET['member']); // Make safe for query

        // I'm going to assume you have a table called members

        
    $memberq mysql_query("SELECT * FROM members WHERE `id` = '$member'");

        if(
    mysql_num_rows($memberq)) { // If any rows are returned
            
            
    $row mysql_fetch_row($memberq); // Since Id will be / is a primary key, only one record will be returned
            /* So we use mysql_fetch_row as we don't need a while loop to go through multiple records
            Only drawback is that the row comes back as a numerical array so we have to use [0] to address columns
             We'll say the setup is like this:
            
            [0] = id (Unique identifier)
            [1] = name
            [2] = bio (biography)
            [3] = location
            [4] = email
            
            Obviously these are just test columns and you can add  different ones if you want
            I just don't know what data you'll want on the member's page
            Break out of PHP to output HTML
            */
            
    ?>
            
            <div id="memberheader"><?php echo $row[1] . '\'s profile.'?></div>
            <div id="memberinfo">
            <h3>About <?php echo $row[1]; ?></h3>
            Biography: <?php echo $row[2]; ?>
            <p>Location: <?php echo $row[3]; ?></p>
            <p>Email: <?php echo $row[4]; ?></p>
            </div>
            
        
        <?php
        
    }
        
        else {
            echo 
    "The member you're looking for doesn't exist.";
        }

    }
    ?>
    That's the structure of what I think you're looking for, of course you can build on this with more advanced functions and you'll need to make it look presentable

    Hope this helps you out though.

  5. The Following User Says Thank You to Schmoopy For This Useful Post:

    X96 Web Design (04-22-2009)

  6. #5
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Thank you so much Schmoopy! It works exactly like I wanted it to! I've been trying to make something like that for hours - I'm not that great at writing PHP yet...

    Cheers,
    X96 WD
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  7. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    No problem, now you've got the structure there it's pretty simple to add more columns, so have fun with it

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •