Jump to content
MakeWebGames

Function Help


War_Hero

Recommended Posts

Hi all. I'm still quite new to PHP so I get stuck on a few things.

I'm creating a new mod called House Items. I'm not sure if it's been posted before, but what it does is allows the user to buy items to go with their house, for example an LCD TV, which increases their max will.

Now, I've created the table in the database and created the staff options, in which, there are a few minor errors, but my main concern is with the actual purchasing of items. My table selects all the items from the house items table I made and displays them, but when I try and purchase the item, it doesn't come up with anything. It's just a blank screen.

I'll post all of my code here, the staff_houseitems.php and houseitems.php, along with my SQL table.

My staff_houseitems.php:

<?php
include "sglobals.php";
if($ir['user_level'] != 2)
{
die("403");
}

//This contains House Items//

switch($_GET['action'])
{
case 'addhitems': add_hitems(); break;
case 'edithitems': edit_hitems(); break;
case 'delhitems': del_hitems(); break;
default: print "Error. This script requires an action."; break;
}


function add_hitems()
{
global $db,$ir,$c,$h,$userid;

$name = $_POST['name'];
$price = abs((int) $_POST['price']);
$will = abs((int) $_POST['will']);

if($name AND $price AND $will)
{
	$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsNAME` = '%d'",
							$r['hitemsNAME']));

	if($db->num_rows($q))
	{
		print "Sorry, you can not have two house items with the same name.";

		$h->endpage();
		exit;
	}

$db->query(sprintf("INSERT INTO `houseitems` VALUES(NOT NULL, '%d', '%d', '%d')",
				   $name, $price, $will));

print "House item $name added to the game.";

stafflog_add("Created House Item $name");

}
else
{
	print "<h3>Add House Item</h3> <hr />

	<form action = 'staff_houseitems.php?action=addhitems' method = 'post' />
	House Item Name: <input type = 'text' name = 'name' /> 

	House Item Price: <input type = 'text' name = 'price' /> 

	House Item Will: <input type = 'text' name = 'will' /> 


	<input type = 'submit' value = 'Add House Item' /> </form>";
}
}


function edit_hitems()
{
global $db,$ir,$c,$h,$userid;

switch($_POST['edit'])
{
	case "2":

	$name = $_POST['name'];
	$price = abs((int) $_POST['price']);
	$will = abs((int) $_POST['will']);

	$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsNAME` = '%d' AND `hitemsID` != '%d'",
							$name, $_POST['id']));

	if($db->num_rows($q))
	{
		print "You can not have two house items with the same name.";

	$h->endpage();
	exit;
	}

$name = $_POST['name'];

$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsID` = '%d'",
						$_POST['id']));

$old = $db->fetch_row($q);

$db->query(sprintf("UPDATE `houseitems` SET `hitemsNAME` = '%d',
				   `hitemsPRICE` = '%d',
				   `hitemsWILL` = '%d'
				   WHERE `hitemsID` = '%u'",
				   $name, $price, $will, $_POST['id']));

print "House item $name edited successfully.";

stafflog_add("Edited House Item $name");

break;

case "1":

$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsID` = '%d'",
						$_POST['hitem']));

$old = $db->fetch_row($q);

print "<h3 />Editing a House Item</h3> <hr />

<form action = 'staff_houseitems.php?action=edithitems' method = 'post' />
<input type = 'hidden' name = 'edit' value = '2' />
<input type = 'hidden' name = 'id' value = '{$_POST['hitem']}' />

House Item Name: <input type = 'text' name = 'name' value = '{$old['hitemsNAME']}' /> 

House Item Price: <input type = 'text' name = 'price' value = '{$old['hitemsPRICE']}' /> 

House Item Will: <input type = 'text' name = 'will' value = '{$old['hitemsWILL']}' /> 


<input type = 'submit' value = 'Edit House Item' /> </form>";

break;

default:

print "<h3 />Editing a House Item</h3> <hr />

<form action = 'staff_houseitems.php?action=edithitems' method = 'post' />
<input type = 'hidden' name = 'edit' value = '1' />
House Item: ".location_dropdown($c, "hitem")." 


<input type = 'submit' value = 'Edit House Item' /> </form>";

break;

}
}


function del_hitems()
{
global $db,$ir,$c,$h,$userid;

if($_POST['hitem'])
{
	$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsID` = '%d'",
						$_POST['hitem']));

	$old = $db->fetch_row($q);

	if($old['hitemsID'] == 1)
	{
		die("This house item can not be deleted.");
	}

	$db->query(sprintf("DELETE FROM `houseitems` WHERE `hitemsID` = ('%u')",
					   $old['hitemsID']));

	print "House Item {$old['hitemsNAME']} deleted.";

	stafflog_add("Deleted House Item {$old['hitemsNAME']}");
}

else
{
	print "<h3 />Deleting a House Item</h3> <hr />

	Deleting a House Item is permanent, so be sure you want to delete before you do.

	<form action = 'staff_houseitems.php?action=delhitems' method = 'post' />
	House Item: ".location_dropdown($c, "hitem")." 


	<input type = 'submit' value = 'Delete House Item' /> </form>";
}
}


$h->endpage();
?>

My error with this script is that whenever I create an item, it creates it without a name, just a number. I've looked at my code and can't see what is wrong with it. But that might be because I'm still a newbie. Plus, at the part

House Item: ".location_dropdown($c, "hitem")."

I'm not sure what to put in front of the _dropdown bit to display my house items. :?

My houseitems.php:

<?php
include "globals.php";
if($ir['user_level'] != 2) 
{
die("Under Construction");
}
if($ir['jail'] or $ir['hospital'])
{
die("You can not buy items for your house when you're in jail or hospital.");
}

switch($_GET['action'])

{
	case 'itembuy': item_buy(); break;
	case 'purchase': purchase(); break;
	default: index(); break;
}


function index()
{
global $db,$ir,$userid;

print "<h1 />Furniture Shop</h1> 



Here, you can buy furniture for your home. Furniture ranges from carpets to LCD TVs. It's your choice what to buy, as long as you can afford it. 



<b />What would you like to do?[/b] 

> [url='houseitems.php?action=itembuy']Buy Furniture[/url] 
";
}


function item_buy()
{
global $db,$ir,$userid; 

print "<h2 />Buying Furniture</h2> 



Welcome to the shop. How may we help you? Please keep in mind that you can only have <b />ONE[/b] of each item. 

";

$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsID` = `hitemsID`"));

print "<table width='75%' cellspacing='1' class='table' />

<tr />
	<th />Furniture</th>
	<th />Price</th>
	<th />Max Will Increase</th>
	<th />Purchase?</th>
</tr>";

while($r=$db->fetch_row($q))
{
	print "
	<tr />
		<td />{$r['hitemsNAME']}</td>
		<td />{$r['hitemsPRICE']}</td>
		<td />{$r['hitemsWILL']}</td>
		<td />[url='houseitems.php?action=purchase&ID={$r[']Purchase[/url]</td>
	</tr>";
}
print "</table>";
}


function purchase()
{
global $db,$ir,$userid;

$hprice = ($r['hitemsPRICE']);
$hwill = ($r['hitemsWILL']);

if($_GET['purchase'])
{
	if($ir['money'] < $r['hitemsPRICE'])
{
	die("You don't have enough money to buy this item.");
}
else
{		                   
	$db->query(sprintf("UPDATE `users` SET `money` = `money` - '%d',
					   `maxwill` = `maxwill` + '%d'
					   WHERE `userid` = ('%u')",
					   $hprice, $hwill, $userid));

print "You have bought a(n) {$r['hitemsNAME']}. You feel happier already. 



Click [url='houseitems.php?action=itembuy']HERE[/url] to buy more items.";
}
}
}


$h->endpage();
?>

With this script, my main problem is the purchasing of items. As said above, the items are displayed, but when I click on purchase, a blank page appears. Again, I have tried numerous things but haven't been able to fix it.

My SQL table:

Name: houseitems

Fields:

1) hitemsID

2) hitemsNAME

3) hitemsPRICE

4) hitemsWILL

I appreciate any help given to me. Your help will also help me understand where I went wrong and help me improve my PHP skills.

I'll update this post if I find any more bugs/errors.

Thank you,

War_Hero :)

Link to comment
Share on other sites

Re: Function Help

I also am not an experienced coder in the least but this might help, i am not sure....

In staff_houseitems.php

in the add hitems function!

		<form action = 'staff_houseitems.php?action=addhitems' method = 'post' />
	House Item Name: <input type = 'text' name = 'name' /> 

	House Item Price: <input type = 'text' name = 'price' /> 

	House Item Will: <input type = 'text' name = 'will' /> 


	<input type = 'submit' value = 'Add House Item' /> </form>";

I think you put the $name, $price and $will there so like this

 

		<form action = 'staff_houseitems.php?action=addhitems' method = 'post' />
	House Item Name: <input type = 'text' name = '$name' /> 

	House Item Price: <input type = 'text' name = '$price' /> 

	House Item Will: <input type = 'text' name = '$will' /> 


	<input type = 'submit' value = 'Add House Item' /> </form>";

 

I am probably very wrong but yeah...might help

Now for the other code!

 

$q = $db->query(sprintf("SELECT * FROM `houseitems` WHERE `hitemsID` = `hitemsID`"));

 

I believe you my need a, $_GET['hitemsID'] or something like that to replace the hitemsID in

= `hitemsID`"

 

Again, i may be wrong but just trying to help!

Link to comment
Share on other sites

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.   Paste as plain text instead

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