Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Change a table name with PHP

    Hi,
    I'm kind of new to php mysql but was wondering if it's possible to change a tables' name.

    Let's say I have a News sections on my site that has posts each day there is new news. It echoes the contents out from a table that a user edits so that it displays date, title, content, etc. The name of the entire section and the table name is News. But let's say the user wants to change it from a News section to a Blog section, or whatever. Is there a way, on an edit page, to echo out the name of the table (news) in a text box and has the ability for the user to change it to something else, Blog for example, so that the table is now called blog, the section is now called blog, but the posts are all still there.

    I know that to echo out the table name is pretty easy if I hardcode it into the page...something like:

    PHP Code:
    <? 
    $tablename
    ="home";
    $query="SELECT * FROM $tablename";
    ?>
    But the user wouldn't have the ability to change the $tablename variable I don't think, right?

    Anyone know of a way to do that?
    Thanks, Matt

  • #2
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    yes you can do it that way -- it's unconventional -- but it's doable

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Posts
    120
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but the user can't change $tablename with this code, can they?

    what would be conventional?

    Thanks for the reply, Matt

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    just pass a GET or POST variable from a drop-down list or a hidden field etc..
    PHP Code:
    <?php
    if($_REQUEST['db']=='news'){
      
    $table 'news';
    }else{
      
    $table 'blog';
    }
    $sql="SELECT * FROM $table";
    ?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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