Thread: [PHP] Using a switch statement for pages and listing created accounts [PHP]

Page 1 of 2 12 LastLast
Results 1 to 10 of 14
  1. #1 [PHP] Using a switch statement for pages and listing created accounts [PHP] 
    puts "Hi "+name+
    Ruby's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    346
    Thanks given
    42
    Thanks received
    135
    Rep Power
    66
    Hello, Rune-Server.

    This stuff is fairly simple, but it can make your site smoother and look a little more professional.

    First, we'll start with the switch statement when using pages.

    What this will do is, when you go to some websites you'll see that a lot of their site revolves around index.php. This means your browser isn't actually loading another page as such, it's just loading a different section of code from the same file. This will look like "http://www.yourdomain.com/index.php?page=something". Lets get to work!
    Code:
    <?
    $switch = $_GET['page'];
    switch($switch){
    
    case 'vote':
    /**
    This will display the index.php?page=vote page.
    */
    break;
    
    default:
    /**
    This will display your index.php page.
    */
    break;
    
    }
    ?>
    I'll explain the code above, obviously <? ?> are the opening and closing tags of PHP. Your next line is then creating the variable of $switch and starting off the $_GET method, you then have the extension of your page. I have this set as ['page']; but it'd work as something such as ['page'] it'd just mean your link would be [Only registered and activated users can see links. ] or whatever.

    Your next line is then "switch($switch){" this is initiating the switch statement and then storing the values in the variable we created earlier, $switch.

    You're now into the switch statement itself. "case 'vote':" is just telling your browser that this is the extension you're using. If you had it set to "case 'mopar':" it'd be [Only registered and activated users can see links. ]. As a foot note on here, don't forget your "break;" otherwise your browser won't recognize the end of your statement. You can replicate as many of these as you need, therefore as many pages as you need. Understand? Good.

    You then get on to the "default:" value. This is the code that shows on the page "http://www.yourdomain.com/index.php" or what will most likely be your homepage.

    [hr]

    To our next part of the tutorial!
    This here is only for users who have a login/register system loading through MySQL.

    This more or less just displays how many accounts you have registered on your system, I do it on my RSPS site, it's pretty simple to do, anyone with a bare knowledge of MySQL can do it. But here we go anyways;

    Code:
    <?
    $username = "youruser";
    $password = "yourpass";
    $database = "yourdatabase";
    $host = "localhost"; //99% of the time it's localhost
    
    $handle = mysql_connect($host, $username, $password);
    
    $found = mysql_select_db($database, $handle);
    
    if ($found) {
    
                $SQL = "SELECT `ID` FROM `characters` ORDER BY `ID` DESC LIMIT 1";
                $result = mysql_query($SQL);
                $field = mysql_fetch_assoc($result);
    
                echo "<h5><center>There are currently <b>" . $field['ID'] . "</b> accounts created!</center></h5>";
    
    mysql_close($handle);
    }
    else {
    echo "Database wasn't found, check your variables.";
    }
    ?>
    So once again, <? ?> are your opening and closing tags, you're then declaring your database connection details and which database you'll be using. You're then creating the variable $handle, which is the connection variable, this connects to your MySQL. You've then got your $found variable, this is more or less the database and it's connecting to it. You then have an if statement that exists if your database was found and connected to. You then have another variable $SQL which is declaring your sql query, you then have your $result variable which is actually doing the work and executing the query. You then have another variable, $field which is retrieving the data that was just executed through the query. We're now onto echoing the data we've retrieved. Your next line of code is destroying the connection. Now, remember the if statement from earlier? Here's the else part to it. This is only printing a line of code to tell you that your connection couldn't be created.

    I hope these help you.

    Best Regards,
    Ruby.
    Professional website developer
    Almost a decade experience
    Bespoke website templates
    CMS & forum themes
    Future proof & secure scripts
    We should chat...
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Jul 2009
    Posts
    597
    Thanks given
    158
    Thanks received
    95
    Rep Power
    632
    if you must use PHP, then you should use [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. #3  
    puts "Hi "+name+
    Ruby's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    346
    Thanks given
    42
    Thanks received
    135
    Rep Power
    66
    Quote Originally Posted by No Dice View Post
    if you must use PHP, then you should use [Only registered and activated users can see links. ]

    I've been using mysql for the past x amount of years, until it's mandatory I'll stick to using it. It's only recommended to use mysqli instead atm.
    Professional website developer
    Almost a decade experience
    Bespoke website templates
    CMS & forum themes
    Future proof & secure scripts
    We should chat...
    Reply With Quote  
     

  4. #4  
    Registered Member
    whac's Avatar
    Join Date
    Nov 2011
    Posts
    176
    Thanks given
    35
    Thanks received
    84
    Rep Power
    245
    Thank you for your tutorials, Cammy. :-)
    Reply With Quote  
     

  5. Thankful user:


  6. #5  
    puts "Hi "+name+
    Ruby's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    346
    Thanks given
    42
    Thanks received
    135
    Rep Power
    66
    No problem, Justin.
    Professional website developer
    Almost a decade experience
    Bespoke website templates
    CMS & forum themes
    Future proof & secure scripts
    We should chat...
    Reply With Quote  
     

  7. #6  
    Registered Member dragonxtreme's Avatar
    Join Date
    Jan 2012
    Posts
    330
    Thanks given
    27
    Thanks received
    22
    Rep Power
    0
    that is a horrible way of counting users tbh, if a user is deleted it wont change, eg if there is 3 users id 1, 2, 3, 4 and 2 and 3 are deleted 4 will still exist and if i a new user is added it will be 5

    so use
    Code:
    $count = mysql_num_rows($result);
    
    echo $count;
    Reply With Quote  
     

  8. #7  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!


    S Quare Quxx's Avatar
    Join Date
    Jun 2007
    Posts
    8,868
    Thanks given
    1,859
    Thanks received
    4,677
    Rep Power
    5000
    Quote Originally Posted by No Dice View Post
    if you must use PHP, then you should use [Only registered and activated users can see links. ]
    or PDO



    Reply With Quote  
     

  9. Thankful user:


  10. #8  
    puts "Hi "+name+
    Ruby's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    346
    Thanks given
    42
    Thanks received
    135
    Rep Power
    66
    Quote Originally Posted by S Quare Quxx View Post
    or PDO
    Since I've came to this community I've repeated myself so many times, mysqli is better than PDO. However, mysqli is still in more or less a beta stage, it's only suggested to be used so that PHP can iron out the bugs. Until the switch is necessary, I'd be using mysql.
    Professional website developer
    Almost a decade experience
    Bespoke website templates
    CMS & forum themes
    Future proof & secure scripts
    We should chat...
    Reply With Quote  
     

  11. #9  
    Registered Member
    Anthony`'s Avatar
    Join Date
    Sep 2008
    Age
    23
    Posts
    770
    Thanks given
    75
    Thanks received
    164
    Rep Power
    204
    Quote Originally Posted by ` Ruby View Post
    Since I've came to this community I've repeated myself so many times, mysqli is better than PDO. However, mysqli is still in more or less a beta stage, it's only suggested to be used so that PHP can iron out the bugs. Until the switch is necessary, I'd be using mysql.
    What happens when you have a project that doesn't/can't restrict itself to MySQL? In that case, MySQLi isn't better.
    Reply With Quote  
     

  12. #10  
    puts "Hi "+name+
    Ruby's Avatar
    Join Date
    Apr 2012
    Age
    25
    Posts
    346
    Thanks given
    42
    Thanks received
    135
    Rep Power
    66
    Quote Originally Posted by Anthony` View Post
    What happens when you have a project that doesn't/can't restrict itself to MySQL? In that case, MySQLi isn't better.
    For this, yes; it is useful. However, MySQL should be used where possible over PDO due to it's functionality.
    Professional website developer
    Almost a decade experience
    Bespoke website templates
    CMS & forum themes
    Future proof & secure scripts
    We should chat...
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 6
    Last Post: 12-18-2013, 10:27 AM
  2. Replies: 4
    Last Post: 03-10-2011, 10:19 AM
  3. Whats the PHP dynamic pages code?
    By Voltimolt in forum Website Development
    Replies: 4
    Last Post: 01-17-2011, 03:31 AM
  4. Loading PHP pages through get requests
    By PHPLS in forum Website Development
    Replies: 9
    Last Post: 01-05-2009, 07:02 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •