开发者

outputing a 2-column PHP/MySQL Dropdown menu request

开发者 https://www.devze.com 2023-04-02 07:54 出处:网络
Hope you can help me.... I have a dropdown menu that works fine when requesting from a single column as follows:

Hope you can help me....

I have a dropdown menu that works fine when requesting from a single column as follows:

error_reporting(E_ALL);
if (!isset($_POST['submit'])) {

echo "<form action=\"$t\" method=\"post\">";

    $res=mysql_query("SELECT DISTINCT y FROM $z") or die("Db temp. not available");

    echo "<select name=dropdown>";

    while($row=mysql_fetch_assoc($res)) {

    echo "<option value=\"".$hy."\">".$hy."</option>";

    }

    echo "</select>";

    echo "<input value=\"SORT\" name=\"submit\">";

    echo "</form>";

    }

    else {

    $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_real_escape_string($_POST['dropdown']);

    $d = "DATE_FORMAT( date,'%d/%m/%Y' )as date";

    $result = mysql_query("SELECT * , $d FROM $z WHERE y='$dropdown'  ORDER BY date DESC");

when I try to add a extra column (as below), using the same output methods, the request is not recognized---Zero errors are shown and all variables can be echoed at the appropriate places.

error_reporting(E_ALL);
if (!isset($_POST['submit'])) {

    echo "<form action=\"$t\" method=\"post\" >";


    $query="SELECT DISTINCT x, y FROM $z ORDER BY x";

    echo "<dd><select name=dropdown>";

    if ($result = mysql_query($query))
    {
    $numofrows = mysql_num_rows($result);   
    for($i = 0; $i < $numofrows; $i++) {

    $row = mysql_fetch_assoc($result); 


            if($row['y']=="HOTTEST"){
            echo "<option  value=\"" . $i . "\">" .$hy. "------------" .$hx. "</option>";    }      
            elseif($row['y']=="GRADUAL"){
            echo "<option value=\"" . $i . "\">" .$hy. "-----------" .$hx. "</option>";    }        
            elseif($row['y']=="T开发者_如何转开发OP-Seller"){
            echo "<option value=\"" . $i . "\">" .$hy. "-----------" .$hx. "</option>";    }        
            elseif($row['y']=="HOT-Seller"){
            echo "<option value=\"" . $i . "\">" .$hy. "-----------" .$hx. "</option>";    }
            else {
            echo "<option value=\"" . $i . "\">" .$hy. "----------" .$hx. "</option>";    } 

        }
    }
            else    {

        print "Could not retrieve the data because: 1' . mysql_error() . '";
    }
    echo "</select>";

    echo "<input  type=\"submit\" value=\"SORT\" name=\"submit\">";

    echo "</form>";
    }

    else     {


    $dropdown = empty($_POST['dropdown'])? die ("ERROR: Select from dropdown") : mysql_real_escape_string($_POST['dropdown']);

    $d = "DATE_FORMAT( date,'%d/%m/%Y' )as date";
    $result = mysql_query("SELECT * , $d FROM $z WHERE y='$dropdown'  ORDER BY date DESC");

I hope you can shed some light into the matter for me.

Thanks Sammy


Maybe this will work

if ($result = mysql_query($query)) {
    $numofrows = mysql_num_rows($result);   
    for($i = 0; $i < $numofrows; $i++) {
        if (!mysql_data_seek($result, $i))   //add this line
           continue; //add this line

        $row = mysql_fetch_assoc($result); 

        ........

    }
}


I think i understant your problem. try this:

     if($row['y']=="HOTTEST"){
        echo "<option  value=\"" . $row['y'] . "\">" .$hy. "------------" .$hx. "</option>";    }      
        elseif($row['y']=="GRADUAL"){
        echo "<option value=\"" . $row['y'] . "\">" .$hy. "-----------" .$hx. "</option>";    }        
        elseif($row['y']=="TOP-Seller"){
        echo "<option value=\"" . $row['y'] . "\">" .$hy. "-----------" .$hx. "</option>";    }        
        elseif($row['y']=="HOT-Seller"){
        echo "<option value=\"" . $row['y'] . "\">" .$hy. "-----------" .$hx. "</option>";    }
        else {
        echo "<option value=\"" . $row['y'] . "\">" .$hy. "----------" .$hx. "</option>";    } 


Well, it is very difficult to debug this, since I cannot create the same conditions as in your server. However, your code had alot of syntax issues. I cleared them up for you.. Either it starts working or at least some smarter person then me can debug it further.

CSS:

.dropdown option {
    height: 22px; 
    width: 255px;
    margin-bottom: 3px; 
    font-weight: bold; 
    border-bottom: 3px;
    background-color: #00FF00; /* This also is the default color */
}
.dropdown .hottest {background-color: #FF020D;}
.dropdown .gradual {background-color: #FFFFFF;}
.dropdown .top-seller {background-color: #FFFF00;}
.dropdown .hot-seller {background-color: orange;}

PHP:

// Lets get rid of the notices, as you dont need them for this script
// error_reporting(E_ALL);

// Understanding that this is a sorting dropdown, then we are going to display this all the time:
if ($results = mysql_query('SELECT * FROM EXAMPLE_TABLE ORDER BY COL_X')) {
    echo '<form action="" method="post"><select name="dropdown" class="dropdown">';
    while ($row = mysql_fetch_assoc($results)) {

        $row_y = htmlspecialchars_decode($row['COL_Y']);
        $row_x = htmlspecialchars_decode($row['COL_X']);

        if ($row['COL_Y'] == "HOTTEST") {
            echo '<option  value="' . $row['COL_Y'] . '-|-' . $row['COL_X'] . '" class="hottest">' . $row_y . '------------' . $row_x . '</option>';
        } elseif ($row['COL_Y'] == "GRADUAL") {
            echo '<option value="' . $row['COL_Y'] . '-|-' . $row['COL_X'] . '" class="gradual">' . $row_y . '------------' . $row_x . '</option>';
        } elseif ($row['COL_Y'] == "TOP-Seller") {
            echo '<option value="' . $row['COL_Y'] . '-|-' . $row['COL_X'] . '" class="top-seller">' . $row_y . '------------' . $row_x . '</option>';
        } elseif ($row['COL_Y'] == "HOT-Seller") {
            echo '<option value="' . $row['COL_Y'] . '-|-' . $row['COL_X'] . '" class="hot-seller">' . $row_y . '------------' . $row_x . '</option>';
        } else {
            echo '<option value="' . $row['COL_Y'] . '-|-' . $row['COL_X'] . '">' . $row_y . '------------' . $row_x . '</option>';
        }
    }
    mysql_free_result($results);
    echo '</select><input type="submit" value="SORT" name="submit"></form>';
} else {
    print 'Could not retrieve the data because: ' . mysql_error();
}

// And when the form has been submitted, then use this:
if ($_POST['submit']) {
    list($post_col_y, $post_col_x) = explode('-|-', $_POST['dropdown']);
    $results = mysql_query("SELECT *, DATE_FORMAT(date, '%d/%m/%Y' ) AS `date` FROM $w_table WHERE `COL_Y` = '$post_col_y' AND `COL_X` = '$post_col_x' ORDER BY `date` DESC"); 
} else {
    // However, you also need to show some data without any sorting:
    $results = mysql_query("SELECT *, DATE_FORMAT(date, '%d/%m/%Y' ) AS `date` FROM $w_table ORDER BY `date` DESC");
}

EDIT 6 Understanding that COL_Y is the rating and COL_X is the category, and that you want both of this criteria to be posted and then used in the query. This is script should do the trick now. However, there might come an issue. Because your COL_Y and COL_X values can have special characters..hence the htmlspecialchars(). However, you cant htmlspecialchars the raw data in the options value.. or it wont match in the query.

EDIT 5 I started realizing before, but I was waiting for your feedback. Now that I have seen the example of what you are trying to do, I updated the answer. I'm pretty confident that this will work. You just have to replace the terms with your own parameters (EXAMPLE_TABLE, COL_Y, COL_X). Also, if you know exactly what DISTINCT does in mysql, then you should probably add it back..

EDIT 4 Ok, as I checked your very first revision.. You have nerfed your code down so much, that it lost the original point and did not make any sense. I used your very first revision on this edit. I added the CSS part as well, so the code would make sense again.. And also, so you could see how CSS works.. I added the htmlspecialchars() parts back again, because otherwise your code makes no sense. Changed the mysql error printing location in relation to <select> starting and ending. I also changed the terms, because they didn't make much sense before. The terms are:

  • EXAMPLE_TABLE - Your only table in this code, from where all the data is being pulled.
  • COL_Y - Column inside the EXAMPLE_TABLE, it appears to be some sort of rating
  • COL_X - Second column in this hole code. The output of this column is unknown.

EDIT 3 I removed DISTINCT x, y and replaced with *. Based on the comments and debugging progress, this might be confusing things.

EDIT 2 I removed $hy and $hx, because if you are basing your scripts functionality on them. Then it cant possible work, as these variables don't get set anywhere in this script.

EDIT I edited the code slightly, as I started to realize what is going on (I'm very tired.) Basically, why did you use for() for mysql result loop? It made no sense what so ever, except that you wanted to use the $i count. I changed that part, it theory this should work..

You basically should give us the error messages, other variables, the mysql table scheme and any other information.. Why not explain, what for are you creating this script etc..

NOTES

  • Why are you quoting this string? echo "<input type=\"submit\" value=\"SORT\" name=\"submit\">"; You can actually use ': echo '<input type="submit" ..
  • The very very last else doesn't get closed! Neither does the isset($_POST['submit']
  • !isset($_POST['submit']) ?!? Did you mean empty($_POST['submit'])?
  • Also, the !isset($_POST['submit']) part doesn't make much sense. Usually, the posting script is before the form.. Anyways, I changed it alot.. Sorry, but this makes more sense in my head.
  • You don't need to end quotes like this, if you using double-quotes: echo "<div class=\"" . $class . "\" />"; You can do it like this: echo "<div class=\"$class\" />";
  • print "Could ... 1' . mysql_error() . '"; cannot possible work. You cannot end double quotes with single quotes...?!
  • Why are your variables names so short? $z, $hy and $hx etc.. You always have to code, with the idea at the back of your head.. that some other developer someday must ready your code.. Even you! Yeah, right now you remember, what is what.. But In 5 months, with many new projects behind you, you will not remember what $hx meant. Also, as you can see, it is very hard to help you right now..
0

精彩评论

暂无评论...
验证码 换一张
取 消