Jump to content

Drop-down Lists and Textfields


FireFrenzy

Recommended Posts

I've found this is a great place to learn, so hopefully such can happen with the following:

Basically, I'm trying to develop a world market for a text-based game wherein what is available is listed inside a drop-down list, followed by an input box where the user can enter the amount to purchase (up to the quantity amount available) and submit.

 

Likewise, I've got a drop-down list populated via a MySQL query.  Altogether, the page looks like this:

marketbuy.gif

 

My question is, how does one associate the input box with the drop-down list, assuming it's possible (which I would assume)?  Currently, it displays correctly, however when one submits via the "Purcase Goods" button, nothing occurs.  I'm not quite sure how to integrate everything coherently; my knowledge is limited in the area of PHP/HTML integration, so any explanation would be kindly appreciated.

 

I know long streams of code are often unhelpful in pinpointing a problem (or problems), but in the interest of not excluding an element, here's the code of the file in question:

<?php
include_once("header.php");
include("lib/marketcron.php");

if ($lockdb)
TheEnd("Public market currently disabled!");

if(!defined('CLAN'))
define('CLAN', 0);
if(!defined('SCRIPT'))
define('SCRIPT', 'betamarket'); //this is the current script
if(!defined('SCRIPT2'))
define('SCRIPT2', 'pubmarketsell');

$trooplst = array();
foreach($config[troop] as $num => $mktcost)
$trooplst[] = "troop$num";
$trooplst[] = 'food';
$trooplst[] = 'runes';
foreach($config[troop] as $num => $mktcost)
$users["troop$num"] = $users[troop][$num];

function buyUnits ($type) {
global $playerdb, $marketdb, $users, $uera, $buy, $buyprice, $datetime, $time, $max, $config;
$amount = $buy[$type];
$price = $buyprice[$type];
fixInputNum($price);
fixInputNum($amount);

$amount = invfactor($amount);

sqlQuotes($type);
fixInputNum($price);
$result = mysql_safe_query("SELECT * FROM $marketdb WHERE type='$type' AND seller!=$users[num] AND time<=$time AND clan=".CLAN." ORDER BY price ASC, time ASC");

// printing the list box select command
echo "<select name=$marketdb value=''></option>";

//Array or records stored in $market
while($market=mysql_fetch_array($result)){

$countryname = mysql_fetch_array(mysql_safe_query("SELECT empire FROM $playerdb WHERE num='$market[seller]'"));

//Option values are added by looping through the array
echo "<option value=$market[id]>$market[amount] at $$market[price] from $countryname[empire]</option>";
}

// Closing of list box
echo "</select>";

if (!$market[amount])
	return;
if(isset($max[$type]))
	$amount = floor($users[cash] / $market[price]);
if ($amount > $market[amount])
	$amount = $market[amount];

if ($amount == 0) // did I specify a value?
	return;
if ($amount < 0)
	print "Cannot purchase a negative amount of $uera[$type]!<br>\n";
elseif ($amount * $price > $users[cash])
	print "You don't have enough money to buy that many $uera[$type]!<br>\n";
else {
	$enemy = loadUser($market[seller]);
	$spent = $amount * $price;
	$sales = round($spent * .95);
	$users[cash] -= $spent;
	$enemy[savings] += $sales;
	$users[$type] += $amount;
	fixInputNum($amount);
	@mysql_safe_query("UPDATE $marketdb SET amount=(amount-$amount) WHERE id=$market[id] AND clan=".CLAN.";");
	@mysql_safe_query("DELETE FROM $marketdb WHERE amount=0;");
	print commas(gamefactor($amount)) . " $uera[$type] purchased from the market for $" . commas(gamefactor($spent)) . ".<br>\n";
	if($type == 'food') {
		addNews(100, array(id1=>$enemy[num], id2=>$users[num], food1=>$amount, cash1=>$sales));
	} else if($type == 'runes') {
		addNews(100, array(id1=>$enemy[num], id2=>$users[num], runes1=>$amount, cash1=>$sales));
	} else {
		$troopsell = '';
		foreach($config[troop] as $num => $mktcost) {
			if($type == "troop$num")
				$troopsell .= $amount.'|';
			else
				$troopsell .= '0|';
		}
		$troopsell = substr($troopsell, 0, -1);
		addNews(100, array(id1=>$enemy[num], id2=>$users[num], troops1=>$troopsell, cash1=>$sales));
	} 
	saveUserData($enemy, "networth savings");
} 
}

function getCosts ($type)
{
global $marketdb, $users, $costs, $avail, $canbuy, $time;
sqlQuotes($type);
$market = mysql_fetch_array(mysql_safe_query("SELECT * FROM $marketdb WHERE type='$type' AND seller!=$users[num] AND time<=$time AND clan=".CLAN." 
	ORDER BY price ASC, time ASC LIMIT 1;"));
if ($market[id]) {
	$costs[$type] = $market[price];
	$avail[$type] = $market[amount];
	$canbuy[$type] = floor($users[cash] / $market[price]);
	if ($canbuy[$type] > $market[amount])
		$canbuy[$type] = $market[amount];
} else $costs[$type] = $avail[$type] = $canbuy[$type] = 0;
} 

function printRow ($type)
{
global $users, $uera, $costs, $avail, $canbuy;
?>
    <!-- centered items in this table; buyable goods on world market -->
<tr><td><?=$uera[$type]?></td>
    <td class="acenter"><?=commas(gamefactor($users[$type]))?></td>
    <td class="acenter"><?=commas(gamefactor($avail[$type]))?></td>
    <td class="acenter"><?=buyUnits ($type)?></td>
    <td class="acenter"><?=commas(gamefactor($canbuy[$type]))?></td>
    <td class="aright"><input type="text" name="buy[<?=$market[id]?>]" size="6" value="0"></td>
    <td class="aright"><input type="checkbox" name="max[<?=$market[id]?>]"></td></tr>
<?php
} 

if ($users[turnsused] <= $config[protection])
TheEnd("Cannot trade on the public market while under protection!");

foreach($trooplst as $num => $entry)
getCosts($trooplst[$num]);

if ($do_buy) {
foreach($market[id] as $num => $entry)
	buyUnits($market[id]);
foreach($config[troop] as $num => $mktcost)
	$users[troop][$num] = $users["troop$num"];
saveUserData($users, "networth cash troops food runes");
} 
foreach($trooplst as $num => $entry)
getCosts($trooplst[$num]);

if(defined('CLANMKT')) echo"<big><b>Clan Market - Buy</big></b><br><br>";
else echo"<big><b>Public Market - Buy</big></b><br><br>";
?>
Also see: <a href="?guide&section=military&era=<?=$users[era]?><?=$authstr?>"><?=$gamename?> Guide: Military</a><br>
<script language="JavaScript">
function checkAll (check){
        var path = document.pvmb;
        for (var i=0;i<path.elements.length;i++) {
       e = path.elements[i];
       checkname = "maxall";
       if(check==2) checkname = "maxall2";
       if( (e.name!=checkname)  && (e.type=="checkbox") ) {
	       e.checked = path.maxall.checked;
	       if(check==2) e.checked = path.maxall2.checked;
       }
}
}
</script>

<form method="post" action="?<?=SCRIPT?><?=$authstr?>" name="pvmb">
<!-- goods available from world market header; also, added table sytle to expand table width -->
<table class="inputtable">
<table style="width:550px" border="0">
<tr><td colspan="3" class="acenter"><a href="?<?=SCRIPT?><?=$authstr?>">Buy Goods</a></td>
    <td colspan="4" class="acenter"><a href="?<?=SCRIPT2?><?=$authstr?>">Sell Goods</a></td></tr>
<tr class="buysell" height="30px"><th class="aleft">Unit</th>
    <th class="acenter">Owned</th>
    <th class="acenter">Avail</th>
    <th class="acenter">Cost</th>
    <th class="acenter">Afford</th>
    <th class="acenter">Buy</th>
    <th class="aright">Max <input type="checkbox" name="maxall" onClick="checkAll(1)"></th></tr>
<?php
foreach($trooplst as $num => $entry)
printRow($entry);

?>
<tr><td colspan="6" class="acenter"><input type="submit" name="do_buy" value="Purchase Goods"></td></tr>
</table>
</form>
<?php
TheEnd("");

?>

Link to comment
https://forums.phpfreaks.com/topic/61661-drop-down-lists-and-textfields/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.