Need a menu linked to a sub menu(explanation expanded)

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 submenu 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.

I don’t see any connection in submenu query related to willwin choice made in first select. I am sure you have noticed that because you are storing team names in the same table as auction you end up doing twice the work to get values from different field names whereas if you had a separate table for auction_teams that held the auction id and team id you wouldn’t need to use team1, team2 as fields.

In any case, in my example I am building menu date array based off a single query for teams and bidders. As it appears that “willwin” is a team name based off the table field willwin, I am using that as the primary KEY for the bidders array.

As we need a POST value from first team select to show any options available on users selection I added an ONCHANGE line to the select to POST the form.

We then check if $_POST[‘willwin’] is not empty and a bidder has selected this team. We then use foreach to loop through any users

// MENU team select
        $sql = "SELECT 
		a.id
		, a.team1
		, a.team2
		, b.auction
		, b.bidder
		, b.tagged
		, b.willwin
		, b.willlose
		, u.nick
		, u.rate_sum 
		FROM " . $DBPrefix . "auctions a
			LEFT JOIN " . $DBPrefix . "bids b 
				ON b.auction = a.id
			LEFT JOIN " . $DBPrefix . "users u 
				ON u.id = b.bidder
		
        WHERE a.id = :auc_id AND b.bidder NOT IN ('b.tagged') and b.tagged IN ('b.bidder') 
		order by b.willwin";
		
		
		$query = $db->prepare($sql);
		$query->bindParam(':auc_id', $id);
		$query->execute();

		
		$menu_data['teams'] = array();
		$menu_data['bidders'] = array();
		while($row = $query->fetch(PDO::FETCH_ASSOC)){
		
			if(!in_array($row['team1'],$menu_data['teams'])):
				$menu_data['teams'][] = $row['team1'];
			endif;
			if(!in_array($row['team2'],$menu_data['teams'])):
				$menu_data['teams'][] = $row['team2'];
			endif;	
			
			if(!empty($row['willwin'])):
				$menu_data['bidders'][$row['willwin']][$row['bidder']] = $row['nick'];
			endif;	
		
        }
        
        $TPL_team_list = '<select name="willwin" class="form-control" onchange="this.form.submit()">' . "\r";	
        $TPL_team_list .= '<option value="">Select Team</option>' . "\r";
		foreach($menu_data['teams'] as $team): 
			$selected_team = (isset($_POST['willwin']) && $_POST['willwin'] == $team ? ' selected="selected"' : '');
			$TPL_team_list .= '<option class="team1" value="' . $team . '" ' . $selected_team . '>' . $team . '</option>' . "\r";
		endforeach;
        $TPL_team_list .= '</select>' . "\r";
		
		//echo $TPL_team_list;		

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

            ));
    $i++;
}
	  
		
        $TPL_user_list = '<select name="tagged" class="form-control" onchange="this.form.submit()">' . "\r";	
        $TPL_user_list .= '<option value="">Select User</option>' . "\r";
		
		$search_team = (isset($_POST['willwin']) && in_array($_POST['willwin'],$menu_data['teams']) ? $_POST['willwin'] :'');

		if(!empty($search_team) && array_key_exists($search_team,$menu_data['bidders'])):
			foreach($menu_data['bidders'][$search_team] as $bidder => $nick):					
				$selected_user = (isset($_POST['tagged']) && $_POST['tagged'] == $bidder ? ' selected="selected"' : '');
				$TPL_user_list .= '<option value="' . $bidder . '" ' . $selected_user . '>' . $nick . '...' . $search_team . '</option>' . "\r";
			endforeach;
		endif;
        $TPL_user_list .= '</select>' . "\r";
		
		//echo $TPL_user_list;
		
{
    $template->assign_block_vars('tag_bidder', array(
    'NAME' => $TPL_user_list,

            ));
    $i++;
}

Anyway this is what I understand you are attempting to do.

hi am getting that

Fatal error: Call to a member function bindParam()

How are you connecting?
I used this for a test database connection.

$db="auctions";
$login="auction";
$dbpass="auction1";
$db = new PDO('mysql:host=localhost;dbname='.$db, $login, $dbpass);

As well as these values for testing.

$DBPrefix="tbl_";  
$id=1;	

I assume this is just the way it got posted to the forum and not the way you tested it.

$query = $db-&gt;prepare($sql);

i use this

<?php

$DB_host = "localhost";
$DB_user = "root";
$DB_pass = "pw";
$DB_name = "db";

try
{
    $DB_con = new PDO("mysql:host={$DB_host};dbname={$DB_name}",$DB_user,$DB_pass);
    $DB_con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
    $e->getMessage();
}

i chnage the query to this

// MENU team select
        $query = "SELECT s.team_id, s.teams AS teams1, ss.team_id, ss.teams AS teams2, 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)
        LEFT JOIN " . $DBPrefix . "sports s ON (s.team_id = a.team1)
        LEFT JOIN " . $DBPrefix . "sports ss ON (ss.team_id = a.team2)
        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);

but when i select a team it get submitted before i hit the submit button

onchange="this.form.submit()"

As quoted from my post we need team selection variable as that is the primary KEY used in showing the user array i.e. bidders array.

yeah but what am saying is when i select a value from the menu it automatically submit the form once i click the first drop down menu

Exactly, so when you make the first selection, the corresponding submenu is shown.

I don’t know what you have going when you “submit” the form but I would make sure your submit button has a distinct name and use that name to show records or whatever you are doing on submit.

Noticed and error in my example. In the select user section the search_team should use values from the first selection.

$search_team = (isset($_POST['willwin']) && in_array($_POST['willwin'],$menu_data['teams']) ? $_POST['willwin'] :'');

hi thats the form for my submit button

 <form class="form-horizontal" name="bid" id="bid" action="{SITEURL}bid.php" method="post" onSubmit="return checkForm(this);">

<input type="hidden" name="id" value="{ID}">
                        <div class="alert alert-success" role="alert">{AGREEMENT}</div>
                        <input type="hidden" name="action" value="bid">
                        <input type="submit" name="myButton" value="{L_5199}" class="btn btn-primary">

Well rather than rely on some JS validation I would use php to check POST values making sure they are all as expected triggered from the submit input name. Then make any DB table inserts or updates that are needed. Then if successful and you need some kind of popup alert add it to this post processing section.

<?php  
if (isset($_POST['myButton'])):
	//Do any POST validation, update tables etc.
	
	//If needed show success popup	
	echo '<script type="text/javascript">alert("You have successfully placed your bid")</script>'; 

endif;
?>

hi done that same result

So you have removed onSubmit from the form tag.

yeah

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.