Jump to content

MYSQL Pulling Values


dsbpac
Go to solution Solved by dsbpac,

Recommended Posts

Hello guys, my code is a 3 teir auto populate form based on the selected option. The information is pulling from a mysql DB. Everything is working fine, now I'm wanting to expand to giving the options a value number from the MYSQL values.

Example:
MYSQL DB is setup as follows:

teir_one | teir_one_value | tier_two | teir_two_value | tier_three | tier_three_value
Honda            1           civic          10            blue           100

How can I pull tier_one_value from the DB and display it for data-cost= ?? Thanks in advance

function getTierOne()
{
    $result = mysql_query("SELECT DISTINCT tier_one FROM three_drops")
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result ))
 
 
        {
           echo '<option value="'.$tier['tier_one'].'" data-cost="'.$tier['tier_one_value'].'">'.$tier['tier_one'].'</option>';
        }

}

Here is my full code
 

<?php
//**************************************
//     Page load dropdown results     //
//**************************************
function getTierOne()
{
    $result = mysql_query("SELECT DISTINCT tier_one FROM three_drops")
    or die(mysql_error());

      while($tier = mysql_fetch_array( $result ))
 
 
        {
           echo '<option value="'.$tier['tier_one'].'" data-cost="1">'.$tier['tier_one'].'</option>';
        }

}

//**************************************
//     First selection results     //
//**************************************
if($_GET['func'] == "drop_1" && isset($_GET['func'])) {
   drop_1($_GET['drop_var']);
}

function drop_1($drop_var)
{  
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT tier_two FROM three_drops WHERE tier_one='$drop_var'")
    or die(mysql_error());
    
    echo '<select name="drop_2" id="drop_2">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

           while($drop_2 = mysql_fetch_array( $result ))
            {
              echo '<option value="'.$drop_2['tier_two'].'" data-cost="10">'.$drop_2['tier_two'].'</option>';
            }
    
    echo '</select>';
    echo "<script type=\"text/javascript\">
$('#wait_2').hide();
    $('#drop_2').change(function(){
      $('#wait_2').show();
      $('#result_2').hide();
      $.get(\"func.php\", {
        func: \"drop_2\",
        drop_var: $('#drop_2').val()
      }, function(response){
        $('#result_2').fadeOut();
        setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
      });
        return;
    });
</script>";
}


//**************************************
//     Second selection results     //
//**************************************
if($_GET['func'] == "drop_2" && isset($_GET['func'])) {
   drop_2($_GET['drop_var']);
}

function drop_2($drop_var)
{  
    include_once('db.php');
    $result = mysql_query("SELECT DISTINCT tier_three FROM three_drops WHERE tier_two='$drop_var'")
    or die(mysql_error());
    
    echo '<select name="drop_3" id="drop_3">
          <option value=" " disabled="disabled" selected="selected">Choose one</option>';

           while($drop_3 = mysql_fetch_array( $result ))
            {
              echo '<option value="'.$drop_3['tier_three'].'" data-cost="100">'.$drop_3['tier_three'].'</option>';
            }
    
    echo '</select> ';
}
?>


 

 

Link to comment
Share on other sites

I don't understand what your question is.

 

How can I pull tier_one_value from the DB and display it for data-cost= ??

 

That seems to be exactly what you are doing here:

echo '<option value="'.$tier['tier_one'].'" data-cost="'.$tier['tier_one_value'].'">'.$tier['tier_one'].'</option>';
Link to comment
Share on other sites

data-cost="'.$tier['tier_one_value'].'"

Is the finished code I want. How can I pull the tier_one_value from the mysql DB. When I try to do something like

    $result = mysql_query("SELECT DISTINCT tier_one, tier_one_value FROM three_drops")

it doesn't work.

Link to comment
Share on other sites

  • Solution

UPDATE: I figured out a solution.

 

I took out the DISTINCT and change it to GROUP BY. Thank you everyone for your time

    $result = mysql_query("SELECT tier_one, tier_one_value FROM three_drops GROUP BY tier_one")
    or die(mysql_error());
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.