Jump to content

Question from a beginner


magicmoose

Recommended Posts

Hi, I am just getting into php and am struggling to get it to do anything useful.

I have a drop down box on one page, with 4 possible options:  title, author, artist and publisher.

When the user clicks submit, the following script is run.  I just want it to sort the results based on the field the user selects from the drop down box, but at the moment it sorts by title no matter what they choose.  Can anyone help me work out where I'm going wrong?

Thanks in advance. 

Here's the code.

 

<?

   

    $dbServer=mysql_connect("localhost","","");

    if (!$dbServer) {echo "Failed to connect to MySQL"; exit; }

     

   

    mysql_select_db("database",$dbServer);

   

    if ($_POST['Comics']= "Title"){

    $sql ="SELECT * FROM comics ORDER BY title";

    }

    elseif ($_POST['Comics']= "Author") {

    $sql ="SELECT * FROM comics ORDER BY author";

    }

    elseif ($_POST['Comics']= "Artist") {

    $sql ="SELECT * FROM comics ORDER BY artist";

    }

    elseif ($_POST['Comics']= "Publisher") {

    $sql ="SELECT * FROM comics ORDER BY publisher";

    }

     $queryResult=mysql_query($sql);

   

    if (mysql_error())

    {

      echo "Problem with Query<BR>";

      echo "The following error message was returned from MySQL:<BR>";

      echo mysql_error();

      exit;

    }

   

    if (mysql_num_rows($queryResult)==0)

    {

      echo "No results that match your enquiry";

    }

    else

    {

      while ($dbRecord=mysql_fetch_array($queryResult))

      {

        echo "found: ".$dbRecord["title"].", ".$dbRecord["author"].", ".$dbRecord["artist"].", ".$dbRecord["publisher"]."<BR>";

      }

    }

   

   

   

   

?>

 

 

 

EDIT:  Here's the HTML

 

<form action="includes/dispbytitle.php" method="POST">

<select name="Comics">

<option value="Title">Title</option>

<option value="Author">Author</option>

<option value="Publisher">Publisher</option>

<option value="Artist">Artist</option>

</select>

<INPUT type="submit" value="Search">

</form>

Link to comment
Share on other sites

It works for two fields, but I'm not sure how to extend this to sort by other fields.

I think I may have misunderstood the way drop down menus work with php.

Is it based simply on this line "<select name="Comics">" rather than the values of that are selected?

 

EDIT:  Ignore the above, it does work perfectly, just took me way longer than it should have to see what was happening ::)

Thanks!

Link to comment
Share on other sites

Okay, lets see if I can explain this.

 

You have this code:

<select name="Comics">

<option value="Title">Title</option>

<option value="Author">Author</option>

<option value="Publisher">Publisher</option>

<option value="Artist">Artist</option>

</select>

 

Lets take this line for example:

<option value="Author">Author</option>

 

the value of $_POST['Comics'] is going to be "Author" because you have the option VALUE set to "Author". If you didn't put a value it would be whatever was inbetween the <option>'s..which in this case would be the same thing.

 

You should probably change all the values you set to lowercase since the cols in your table are lowercase:

 

<select name="Comics">

<option value="title">Title</option>

<option value="author">Author</option>

<option value="publisher">Publisher</option>

<option value="artist">Artist</option>

</select>

 

So when I gave you this chunk of code:

 

<?php

$comics = $_POST['Comics'];

if (isset($comics)){

$sql ="SELECT * FROM comics ORDER BY $comics"; 

} else {

$sql ="SELECT * FROM comics ORDER BY title";

}

?>

 

ORDER BY $comics is going to be different each time depending on what option they chose. If they chose "authors" that line is going to read ORDER BY authors since the value of $comics is the POST value.

 

Does that make sense at all?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.