SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deadly Combo Of Header and Switch Together

    Hi!!,Can any one solve my problem?? I am having a drop down menu and i am sending some variables through anchor tag like "<a href=index.php?a=1>" and so on.When i catch the variables in next page, i want to display the data from database which associates to the variable.like
    "case $menu==1:header('Location:main.php?a=website');"
    and the website data must be shown.Dont know why its not working with the code

    $menu=$_GET['a'];
    $query=("SELECT * FROM menu");
    while($row=mysql_fetch_array($query))
    {
    echo $row[$_GET['a']];
    }

    is there any better alternative?? or i can change the code for an output??Donno if i have explained correctly what i want to say..

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Here's the problem.

    Code:
    $query=("SELECT * FROM menu");
    A couple of problems exist here.

    1. The $menu variable is not used
    2. No query is sent to the database

    See example #2 at the mysql_query documentation page for the way to do this.

    First, figure out what query you're going to make of the database.
    The $menu variable contains "website" so how are you going to use it in the query. Are you going to restrict the results to where the field called item is equal to "website"?

    Code mysql:
    SELECT *
    FROM menu
    WHERE item = "website"

    Here is how you would create that query

    Code php:
    $sql= sprintf(
        'SELECT * FROM menu WHERE item = "%s"',
        mysql_real_escape_string($menu)
    );

    Once you have your query you need to pass it to your database and get a result

    Code php:
    $result = mysql_query($sql);

    Finally after that, you can use your while and mysql_fetch_array to retrieve the rows

    Code php:
    while ($row = mysql_fetch_array($result)) {
        ...
    }

    It can be preferable though to remove the assignment from inside the condition, and to use a one-and-a-half loop instead:

    Code php:
    while (true) {
        $row = mysql_fetch_array($result);
        if (!$row) {
            break;
        }
        ...
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •