www.webdeveloper.com
Results 1 to 2 of 2

Thread: dynamic php that changes by option selected

  1. #1
    Join Date
    Jan 2012
    Posts
    14

    dynamic php that changes by option selected

    Well I came up with an idea...not saying its a good one, but one none-the-less. Now I do know that this can easily be done by creating multiple pages that have the proper code I was wondering if it would be
    possible to have a single php file that would be able to change on the fly by the press of a menu button.

    For example:

    MySQL database

    Table = Books
    Field1 = Title
    Field2 = Genre


    PHP file

    Menu with two options: Fiction and Non-Fiction

    When fiction is selected the page will display all rows from the database that has fiction in the genre field.

    Then when non-fiction is selected the page will then display only the rows that have non-fiction in the genre field.

    I know that the following sections in the file would need to change, just not sure how to make it work.

    PHP Code:
    <?php

    include'config.php';
    mysql_connect("$host""$username""$password") or die(mysql_error());
    mysql_select_db("$db_name") or die(mysql_error());

    // Change the LIKE '% %' when menu button is selected
    $result mysql_query("SELECT * FROM books WHERE genre LIKE '%fiction or non-fiction%' ORDER BY title ASC");

    ?>


    <html>
    <head>
    <!--Change when menu option is selected-->
    <title>Fiction or Non-Fiction</title>
    </head>

    <!--Change when menu option is selected-->
    <body id="fiction or non-fiction">

    <div id="menu">
            <!-- Not sure how to make this work-->
        <ul>
            </li><a href="????" target="_parent">Fiction</a></li>
            </li><a href="????" target="_parent">Non-Fiction</a></li>
        </ul>
    </div

    <?php
    while($row=mysql_fetch_array($result))
    {
    echo 
    "".$row['title']."";
    }
    ?>

    </body>
    </html>

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    First of all, if you can upgrade from mysql_connect to mysqli_connect, do it. I think mysql_connect is outdated.

    Secondly, you don't have to change the PHP code of the page, itself, in order to do what you want. You can use either links (as you have) or a SELECT for the Fiction/Non-Fiction, and use an AJaX function to make a call to a .php page that runs the query with the value of whatever is clicked/selected, and dynamically refresh the data without reloading the page.
    Last edited by WolfShade; 02-03-2012 at 04:18 PM.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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



Recent Articles