Jump to content

php drop down sub menu


skyhighweb

Recommended Posts

hi programmers how u doing? i need help creating a sub menu but with a twist

MENU
Submenu- team1 on click will display dropdown list of team2 lists
team2 on click will display dropdown list of team1 lists

if a user select (team1) it should drop down list of [team2] and if the user select (team2) it should drop list of [team1].

the code below is only a menu without submenu need ur assistance, thanks alot

 

// team select
		$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}

Tables : auctions

columns: id(int), team1, team2
3 chelsea ajax

Tables : bids
columns: auction(int) bidder willwin
3 9 chelsea
3 5 ajax
3 7 chelsea
3 2 ajax
3 6 chelsea
3 4 chelsea

Link to comment
Share on other sites

if a user select (team1) it should drop down list of [team2] and if the user select (team2) it should drop list of [team1].

I don't know what that means.

If you're trying to make dropdowns which depend on other dropdowns, the basic way to do that is to have the form submit when you pick from the parent list, PHP gets the submitted value for that list, and writes out the child list based on the parent value.  You could also use ajax to submit the value to the server and have it return the options for the child list, then update the page with that.

Link to comment
Share on other sites

6 minutes ago, justsomeguy said:

 

 

I don't know what that means.

If you're trying to make dropdowns which depend on other dropdowns, the basic way to do that is to have the form submit when you pick from the parent list, PHP gets the submitted value for that list, and writes out the child list based on the parent value.  You could also use ajax to submit the value to the server and have it return the options for the child list, then update the page with that.

yeah am trying to make a dropdown that depends on other drop down but no idea how to go about it

Link to comment
Share on other sites

Well, there's 2 options I mentioned.  Either have a normal form submit where you get the parent value and use it to build the child dropdown with PHP, or get the parent value with Javascript and send it to the server using ajax to get the list of options for the child dropdown.

Link to comment
Share on other sites

19 minutes ago, justsomeguy said:

Well, there's 2 options I mentioned.  Either have a normal form submit where you get the parent value and use it to build the child dropdown with PHP, or get the parent value with Javascript and send it to the server using ajax to get the list of options for the child dropdown.

i totally understand wwhat u saying my friend but i just dont know how to start about it thats y i posted the menu code am using, can u plz take a look at it for me n help out, will b grate ful

Link to comment
Share on other sites

Do you know how to get form data?  That would be the first step to getting the options for the second dropdown.  If you don't know how to do that, I would suggest starting with some of the beginning PHP tutorials.  Getting data from a form is one of the more basic things you should understand.

Link to comment
Share on other sites

11 hours ago, justsomeguy said:

Do you know how to get form data?  That would be the first step to getting the options for the second dropdown.  If you don't know how to do that, I would suggest starting with some of the beginning PHP tutorials.  Getting data from a form is one of the more basic things you should understand.

hi jsg thanks for ur reply, have been over it again n again cant even seem to get it to work, please help me out with it, i will b glad n have a good nite sleep today, thanks

Link to comment
Share on other sites

You need to start with something.  I'm here to help people learn, not write code for them.  Go look at the PHP form tutorials and start there.  Start with a form that has your dropdown in it where you can submit it and get the value that was selected.

Link to comment
Share on other sites

So get the submitted value, and use it to look up whatever database records correspond to that, and build the other dropdown.  I'm not sure what you're missing.  If you know how to process a form, and you know how to use the database, and you know how to build a dropdown, which part are you having problems with?

Link to comment
Share on other sites

4 hours ago, justsomeguy said:

So get the submitted value, and use it to look up whatever database records correspond to that, and build the other dropdown.  I'm not sure what you're missing.  If you know how to process a form, and you know how to use the database, and you know how to build a dropdown, which part are you having problems with?

hi am having problem on the drop down part if i knew what to do i would have stopped posting questions n do it

Link to comment
Share on other sites

I'm not sure what else to say.  You have code to create a dropdown already based on information in the database.  You've said that you know how to get data from a form.  So, get the data from the form for the parent dropdown, and use it to get the data from the database and build the child dropdown almost exactly like the code you showed in the first post.

If you're just copying and pasting code without understanding how it works, then that's the problem.  If that's the case you need to start with the PHP tutorials to learn how this stuff works before expecting that you'll be able to change or add to it.

Link to comment
Share on other sites

13 hours ago, justsomeguy said:

I'm not sure what else to say.  You have code to create a dropdown already based on information in the database.  You've said that you know how to get data from a form.  So, get the data from the form for the parent dropdown, and use it to get the data from the database and build the child dropdown almost exactly like the code you showed in the first post.

If you're just copying and pasting code without understanding how it works, then that's the problem.  If that's the case you need to start with the PHP tutorials to learn how this stuff works before expecting that you'll be able to change or add to it.

i am trying something hopefully that works also posted a issue about it

Link to comment
Share on other sites

hello programmers again really need your help with this, i Need a menu linked to a sub menu

i have a menu code and a submenu code but they arent linked, both i select manually, so i need help linking both of them.
the menu consist of two values team1 and team2 i want when i select team1 it should drop down a menu of users who selected team1 and vice versa.

for the MENU we got two tables they are

auctions: with column: id             team1      team2  
bids:       with column: auction     bidder      willwin


and for the sub menu we also have two tables
bids     with:  column: auction     bidder      willwin
users  with:  column:  nick

 

MENU CODE

// MENU team select
		$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin, b.willlose FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

		$TPL_team_list = '<select name="willwin" class="form-control">' . "\n";
		while ($row = $db->fetch())
		{
			$TPL_team_list .= "\t" . '
			<option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
			<option class="team1" value="' . $row['team1'] . '" ' . $selected . '>' . $row['team1'] . '</option>
			<option class="team2" value="' . $row['team2'] . '" ' . $selected . '>' . $row['team2'] . '</option>
			' . "\n";
		}
		
		$TPL_team_list .= '</select>' . "\n";

{
	$template->assign_block_vars('tag_bidder', array(
	'TEAM' => $TPL_team_list,

			));
	$i++;
}

SUBMENU CODE   

// SUBMENU user name
        $query = "SELECT b.*, u.nick, u.rate_sum FROM " . $DBPrefix . "bids b
LEFT JOIN " . $DBPrefix . "users u ON (u.id = b.bidder)
WHERE b.bidder NOT IN ('b.tagged') and b.tagged IN ('b.bidder') and b.auction = :auc_id order by b.willwin";
$params = array();
$params[] = array(':auc_id', $id, 'int');
$db->query($query, $params);

        $TPL_team_list = '<select name="tagged" class="form-control">' . "\n";
        while ($row = $db->fetch())
        {
            $TPL_team_list .= "\t" . '
            <option value="' . $row[''] . '" ' . $selected . '>' . $row[''] . '</option>
            <option value="' . $row['bidder'] . '" ' . $selected . '>' . $row['nick'] . '...' . $row['willwin'] . '</option>
            ' . "\n";
        }
        
        $TPL_team_list .= '</select>' . "\n";

{
    $template->assign_block_vars('tag_bidder', array(
    'NAME' => $TPL_team_list,

            ));
    $i++;
}


really looking forward to ur help thanks.

Link to comment
Share on other sites

Let's keep the discussion about the same issue in a single thread.

The explanation for how to do this is still the same, so what's different?  You still need to get the value from the first dropdown, and use to build the second dropdown, however they relate?  Which part are you stuck on?

Link to comment
Share on other sites

Do you know how to get the value of the first dropdown?  You can either submit it regularly with a form, or get it with Javascript and send it with ajax.  I've already asked you these questions though, you seemed to indicate that you know how to get the value from the form.  If you don't, you need to hit the PHP tutorials to learn the basics.

Link to comment
Share on other sites

13 minutes ago, justsomeguy said:

Do you know how to get the value of the first dropdown?  You can either submit it regularly with a form, or get it with Javascript and send it with ajax.  I've already asked you these questions though, you seemed to indicate that you know how to get the value from the form.  If you don't, you need to hit the PHP tutorials to learn the basics.

i can get the first value wwith a form but getting the sub menu is the issue for me else would have done that,  just look tru for me n assist me, that wwill b great thanks, either JavaScript or ajax cant seem to get it to connect.

Edited by skyhighweb
Link to comment
Share on other sites

When the form is submitted,OR the select dropdown triggers a onchange event that uses AJAX, it should pass the value of selection using $_POST[] or $_GET to the second dropdown.

It usually uses this value to filter and access the related records, but I currently do not see any fields that would possibly do that? All I see is filtering by 'auc_id', is this relative value passed through by team1 or team2?

Link to comment
Share on other sites

9 hours ago, dsonesuk said:

When the form is submitted,OR the select dropdown triggers a onchange event that uses AJAX, it should pass the value of selection using $_POST[] or $_GET to the second dropdown.

It usually uses this value to filter and access the related records, but I currently do not see any fields that would possibly do that? All I see is filtering by 'auc_id', is this relative value passed through by team1 or team2?

hi, i am no where near a submenu drop down linked to menu, all i have is  a field for the menu and a field for the submenu seperately, auc_id is used to display only data being viewed at the moment.

Link to comment
Share on other sites

The array is treated as tables, the value from first dropdown is used to show other related info from related array/table info.

The way the second dropdown is processed can be placed in another php file, which is called by using ajax.

This is a rough guide on how to send info from first to second dropdown, the principle is the same, I did not reproduce the effect using your code, as you seem to want us to do it for you, as you keep saying  you tried this and that! But! don't actually show us what code you tried, just the repeated showing of the same code.

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($_POST['willwin'])) {
            $teamRec = $_POST['willwin'];
        }


        $team_array = [];
        $team_array['Team1'] = ['Team #1 dropdown1', 'Team #1 dropdown2', 'Team #1 dropdown3', 'Team #1 dropdown4'];
        $team_array['Team2'] = ['Team #2 dropdown1', 'Team #2 dropdown2', 'Team #2 dropdown3', 'Team #2 dropdown4'];
        ?>

        <form id="myform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
            <select name="willwin" class="form-control">
                <option value="">Select Team </option>
                <?php
                $optioncount = 0;
                foreach ($team_array as $key => $value) {
                    $optioncount++;
                    if ($key === $teamRec) {
                        echo '<option class="team' . $optioncount . '" value="' . $key . '" selected="selected">Team #' . $optioncount . '</option>';
                    } else {
                        echo '<option class="team' . $optioncount . '" value="' . $key . '">Team #' . $optioncount . '</option>';
                    }
                }
                ?>


                <!--<option class="team1" value="Team1">Team #1</option>
                <option class="team2" value="Team2">Team #2</option>-->

            </select><br>
            <?php
            if (isset($_POST['willwin'])) {


                echo '<select name="tagged" class="form-control">';
                echo '<option value="">Select Team Info </option>';
                $team_array_item = $team_array[$teamRec];

                foreach ($team_array_item as $key => $value) {
                    echo '<option value="' . $value . '">' . $value . '</option>';
                }
                echo '</select><br>';
            }
            ?>


            <input type="submit">
        </form>


    </body>
</html>

 

Link to comment
Share on other sites

36 minutes ago, dsonesuk said:

The array is treated as tables, the value from first dropdown is used to show other related info from related array/table info.

The way the second dropdown is processed can be placed in another php file, which is called by using ajax.

This is a rough guide on how to send info from first to second dropdown, the principle is the same, I did not reproduce the effect using your code, as you seem to want us to do it for you, as you keep saying  you tried this and that! But! don't actually show us what code you tried, just the repeated showing of the same code.


<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        if (isset($_POST['willwin'])) {
            $teamRec = $_POST['willwin'];
        }


        $team_array = [];
        $team_array['Team1'] = ['Team #1 dropdown1', 'Team #1 dropdown2', 'Team #1 dropdown3', 'Team #1 dropdown4'];
        $team_array['Team2'] = ['Team #2 dropdown1', 'Team #2 dropdown2', 'Team #2 dropdown3', 'Team #2 dropdown4'];
        ?>

        <form id="myform" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
            <select name="willwin" class="form-control">
                <option value="">Select Team </option>
                <?php
                $optioncount = 0;
                foreach ($team_array as $key => $value) {
                    $optioncount++;
                    if ($key === $teamRec) {
                        echo '<option class="team' . $optioncount . '" value="' . $key . '" selected="selected">Team #' . $optioncount . '</option>';
                    } else {
                        echo '<option class="team' . $optioncount . '" value="' . $key . '">Team #' . $optioncount . '</option>';
                    }
                }
                ?>


                <!--<option class="team1" value="Team1">Team #1</option>
                <option class="team2" value="Team2">Team #2</option>-->

            </select><br>
            <?php
            if (isset($_POST['willwin'])) {


                echo '<select name="tagged" class="form-control">';
                echo '<option value="">Select Team Info </option>';
                $team_array_item = $team_array[$teamRec];

                foreach ($team_array_item as $key => $value) {
                    echo '<option value="' . $value . '">' . $value . '</option>';
                }
                echo '</select><br>';
            }
            ?>


            <input type="submit">
        </form>


    </body>
</html>

 

i have been trying, for example have been working on the below code it doing what i want but problem with it is it displays all data in a table instead of whats been viewed at the moment

<?php
require_once('conn.php');
ini_set('display_errors',0);

$query = "SELECT b.auction, b.bidder, b.tagged, b.willwin, b.willlose, a.id, a.team1, a.team2  FROM " . $DBPrefix . "vs_auctions a
LEFT JOIN " . $DBPrefix . "vs_bids b ON (b.auction = a.id) 
LEFT JOIN " . $DBPrefix . "vs_sports s ON (s.team_id = b.id) 
WHERE a.id = b.auction and a.id = a.id group by b.auction";
$country_result = $conn->query($query);

?>


<select name="country" id="vs_sports-list" onchange="changeSelect( this.value )">
<option value="1">Select Team</option>
<?php
if ($country_result->num_rows > 0) {
// output data of each row
while($row = $country_result->fetch_assoc()) {
?>
<option value="<?php echo $row["willwin"]; ?>"><?php echo $row["team1"]; ?></option>
<option value="<?php echo $row["willlose"]; ?>"><?php echo $row["team2"]; ?></option>
<?php
}
}
?>
</select>
</br></br></br><div id="subcats">
<select name="bidder" id="vs_bids-list">
<option value='1'>Select Number</option>

</select></div>
<?php

$willlose = mysql_real_escape_string($_POST['willlose']);
if($willlose!='')
{


$vs_bids_result = $conn->query('select b.auction, b.bidder, b.tagged, b.willwin, b.willlose from vs_bids b where willlose='.$willlose.' ');
$options = "<option value=''>Select Name</option>";
while($row = $vs_bids_result->fetch_assoc()) {
$options .= "<option value='".$row['id']."'>".$row['bidder']."</option>";
}
echo $options;
}

?>

<script src="js/dropdown/jquery-1.3.0.min.js"></script>

<script>
$('#vs_sports-list').on('change', function(){
var willlose = this.value;
$.ajax({
type: "POST",
data:'willlose='+willlose,
success: function(result){
$("#vs_bids-list").html(result);
}
});
});

</script>

in my existing code i am using this

$query = "SELECT a.id, a.team1, a.team2, b.auction, b.bidder, b.tagged, b.willwin FROM " . $DBPrefix . "auctions a
		LEFT JOIN " . $DBPrefix . "bids b ON (b.auction = a.id)
		WHERE a.id = :auc_id and a.id = a.id group by a.id";
		$params = array();
		$params[] = array(':auc_id', $id, 'int');
		$db->query($query, $params);

thats display only what been viewed at the moment

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...