Author Topic: Coding some 'new' items...  (Read 3867 times)

Offline gondie

Coding some 'new' items...
« on: March 08, 2007, 03:05:23 am »
Hey... We'll I havent been working with PHP all that long, so I was wondering if anyone could help me out with these? Honestly, I didnt put all that much Work into these considering all the snipplets I found and used.

Anyways, Here is the first one I tried (yesterday) and failed so horribly in doing so...

This item is similar to the other Group items, but I made some changes. I plan on adding different instances of this item but with a different value for $item_info[2] so that they can purchase the item for a longer period of time.

(As you can see, Im not using any code to automatically remove them from the group once the time has ran up, I am having a PM sent to my account on the forums and I will take note of the date they used the item and remove the membership when it runs out)

Any ideas? Tips? Suggestions? Fixes?

<?php

if (!defined('SMF'))
	
die(
'Hacking attempt...');

class 
item_ChangeMembergroupLimited extends itemTemplate
{
	
function 
getItemDetails()
	
{
	
	
$this->authorName 'Gondie';
	
	
$this->authorWeb 'http://mizzro.inceptiongs.com';
	
	
$this->authorEmail 'thegondie@gmail.com';

	
	
$this->name 'Premium Membership (xxx days)';
	
	
$this->desc 'Become a Premium Member for xxx days!';
	
	
$this->price 4500;

	
	
$this->require_input false;
	
	
$this->can_use_item true;
	
}


	
function 
getAddInput()
	
	
	
{

	
	
global 
$item_info$groupname$db_prefix;
	
	

	
$selectBox '<select name="info1">';

	
$result db_query("SELECT ID_GROUP, groupName
	
	
	
	
	
	
	
FROM 
{$db_prefix}membergroups
	
	
	
	
	
	
	
WHERE minPosts = -1"
,
	
	
	
	
	
	
	
	
__FILE____LINE__);
	

	
while (
$row mysql_fetch_assoc($result)) {
	
	
$selectBox .= "<option value='{$row['ID_GROUP']}'>{$row['groupName']}</option>";
	
}

	
$selectBox .= "</select>";
        return 
"Membergroup: ".$selectBox;
	
	

	
	
if (
$item_info[2] == 0$item_info[2] = 30;
	
	
'Time to stay Changed: <input type="text" name="info2" value="' $item_info[2] . '" />';
	
	
	
}


	
function 
onUse()
	
	
	
{

	
	
global 
$db_prefix$memberName$ID_MEMBER$ID_GROUP;

	
	
db_query("
	
	
	
UPDATE 
{$db_prefix}members
	
	
	
SET ID_GROUP = 
{$item_info[2]}
	
	
	
WHERE ID_MEMBER = 
{$ID_MEMBER}"__FILE____LINE__);
	
	
return 
'Upgraded to "  for ' $item_info[1] . ' days!';

	
$result db_query("SELECT money, memberName, realName
	
	
	
FROM 
{$db_prefix}members
	
	
	
WHERE ID_MEMBER = 
{$ID_MEMBER}
	
	
	
LIMIT 1"
__FILE____LINE__);
	
	
	

	
$row mysql_fetch_array($resultMYSQL_ASSOC);
	
$membersUserName $row['memberName'];
	
$membersName $row['realName'];

	
$pmfrom = array(
	
	
'id' => $ID_MEMBER,
	
	
'name' => $membersName,
	
	
'username' => $membersUserName
	
);

	
$pmto = array(
	
	
'to' => array(1),
	
	
'bcc' => array()
	
);

	
$subject "Item Log: Prem Member Upgrade";
	
$message "from has Upgraded to group for time days.";
	
$vreplacekeys = array('from''group''time');
	
$vreplaceto = array($pmfrom$item_info[2], $item_info[1]
	
);
	
$message  str_replace($vreplacekeys $vreplaceto $message);
	
sendpm($pmto$subject$message0$pmfrom);
	
	
	
}
}

?>


(Just so you know, I just got this mod yesterday as well)

And I do believe I am missing the GetUseInput function in that one...

-------------------------------------------------------------------------------------------------

Here is the one I started working on a little bit ago...

This one is somewhat similar to the above one but still rather different. This one will allow them to request a custom Graphic or Web Design for points. I have a textbox for the type they want (graphic or web) and a text area so they can give me details on the project.

I dont really know whats going on with this one.. When I try to load the Add/Edit/Delete page in the admin cp I get this error...

Quote
ERROR: Could not create instance of 'GetDesign' item!

Any ideas? Tips? Suggestions? Fixes?

<?php

if (!defined('SMF'))
	
die(
'Hacking attempt...');

class 
item_RequestDesign extends itemTemplate
{

	
function 
getItemDetails()
	
{
	
	
$this->authorName 'Gondie';
	
	
$this->authorWeb 'http://mizzro.inceptiongs.com';
	
	
$this->authorEmail 'thegondie@gmail.com';

	
	
$this->name 'Custom Design';
	
	
$this->desc 'Get a custom Graphic or Web Design for gPoints';
	
	
$this->price 1000;
	
	

	
	
$this->require_input true;
	
	
$this->can_use_item true;
	
	
$this->addInput_editable true;
	
}

	
function 
getAddInput()
	
{
	
	
global 
$item_info;
	
	
if (
$item_info[1] == ''$item_info[1] = 'Type of Project: <input type="text" name="type" /> (Graphic or Design)';
	
	
if (
$item_info[2] == ''$item_info[2] = 'Please give some details for this. Dimensions, colors, text, etc.';
	
	

	
	
	
	
return 
'Type of Project: <input type="text" name="info2" size="50" value="' $item_info[2] . '" /> (Graphic or Design)<br />
	
	
	
	
Additional fields needed:<br />
	
	
	
	
	
<textarea name="info2" rows="6" cols="40">
$item_info[2] . '
	
	
	
	
	
</textarea><br />'
;
	
}

	
function 
getUseInput()
	
{
	
	
global 
$item_info;
	
	
// The 'additional fields needed' entered during item setup
	
	
return 
$item_info[1];
	
	
return 
$item_info[2];
	
}

	
function 
onUse()
	
{
	
	
$result db_query("SELECT money, memberName, realName
	
	
	
	
	
FROM 
{$db_prefix}members
	
	
	
	
	
WHERE ID_MEMBER = 
{$ID_MEMBER}
	
	
	
	
	
LIMIT 1"
__FILE____LINE__);

	
	
$row mysql_fetch_array($resultMYSQL_ASSOC);
	
	
$membersUserName $row['memberName'];
	
	
$membersName $row['realName'];

	
	
$pmfrom = array(
	
	
	
	
'id' => $ID_MEMBER,
	
	
	
	
'name' => $membersName,
	
	
	
	
'username' => $membersUserName
);
	
	

	
	
$pmto = array(
	
	
	
	
'to' => array(1),
	
	
	
	
'bcc' => array()
);
	
	

	
	
$subject "[name] requests [type]";
	
	
$vreplacekeys = array('[name''type');
	
	
$vreplaceto = array($username$item_info[1], '
'
);

	
	
$subject  str_replace($vreplacekeys $vreplaceto $subject);
	
	
$message "[desc]";
	
	
$vreplacekeys = array('[name');
	
	
$vreplaceto = array($item_info[2], '
'
);
	
	
$message str_replace($vreplacekeys$vreplaceto$message);
	
	
sendpm($pmto$subject$message0$pmfrom);
	
}
}

?>



Say as you wish of me and my horrid coding, It wont bother me. Just trying to learn what I can.

Thanks for your time.

Offline Basil Beard

Re: Coding some 'new' items...
« Reply #1 on: March 08, 2007, 03:12:31 am »
With the first item, how does it expire after the time is up? Do you have to do that manually, or did I miss that important part of the code?
Arrrrr!

Offline gondie

Re: Coding some 'new' items...
« Reply #2 on: March 08, 2007, 03:15:50 am »
It says just before the PHP code...

Quote
(As you can see, Im not using any code to automatically remove them from the group once the time has ran up, I am having a PM sent to my account on the forums and I will take note of the date they used the item and remove the membership when it runs out)

Offline feeble

Re: Coding some 'new' items...
« Reply #3 on: March 08, 2007, 04:27:03 am »
bit more automation would be cool

could store the date in a table, then have a cronjob check if once a day or every hour etc.

Offline gondie

Re: Coding some 'new' items...
« Reply #4 on: March 08, 2007, 04:34:59 am »
I will probably do something along those lines, but I need to get them working first :-P

Didnt know that the shop logs showed up in the forum logs... That helped me to try and fix this...

I got all but one area in the first one fixed, for some reason I am getting an incorrect syntax?

Quote
Apply Filter: Only show the errors with the same message
Database Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID_MEMBER = 4' at line 3
*************************/ChangeMembergroupLimited.php
Line: 54

Here is what is messing up...

(I have altered the code since the first post, but not this section...)

	
	
$result db_query("UPDATE {$db_prefix}members
	
	
	
SET ID_GROUP = 
{$item_info[2]}
	
	
	
WHERE ID_MEMBER = 
{$ID_MEMBER}"__FILE____LINE__);
	
	
return 
'Upgraded to ' $item_info[2] . '  for ' $item_info[1] . ' days!';
« Last Edit: March 08, 2007, 09:43:45 am by gondie »

Offline Daniel15

Re: Coding some 'new' items...
« Reply #5 on: March 08, 2007, 04:12:02 pm »
As far as I know, you can only call 'return' once. So, code like:
Code: (php) [Select]
return $item_info[1];
return $item_info[2];

Should instead be something like:
Code: (php) [Select]
return $item_info[1] . $item_info[2];
(all on one line), or:
Code: (php) [Select]
return $item_info[1] . '
' . $item_info[2];
(across multiple lines).

Basically, as soon as return is called, PHP exits your function (and hence, any code underneath the first return line is not executed). I could be wrong, don't quote me on this :P

Quote
        return "Membergroup: ".$selectBox;
      
      if ($item_info[2] == 0) $item_info[2] = 30;
      'Time to stay Changed: <input type="text" name="info2" value="' . $item_info[2] . '" />';
         }
I'd perhaps do something like:
Code: (php) [Select]
$output = 'Membergroup: ' . $selectBox;

if ($item_info[2] == 0) $item_info[2] = 30;
$output .= 'Time to stay Changed: <input type="text" name="info2" value="' . $item_info[2] . '" />';
return $output;
}
(basically, put all the stuff to output in a $output variable, and then just return that variable. It makes it a lot neater :)

Quote
I dont really know whats going on with this one.. When I try to load the Add/Edit/Delete page in the admin cp I get this error...

Quote
ERROR: Could not create instance of 'GetDesign' item!
Your item's class needs to have the same name as the file itself. So, either
Code: (php) [Select]
class item_RequestDesign extends itemTemplate

Should be changed to
Code: (php) [Select]
class item_GetDesign extends itemTemplate
, or the file itself should be renamed.

Quote
Say as you wish of me and my horrid coding, It wont bother me. Just trying to learn what I can.
It looks a bit better than my first PHP scripts ;).
One suggestion: Fix your indentation. Each level of curly brackets { should have one extra tab character at the start of the line.  So, basically,
Code: (php) [Select]
function getAddInput()
{

global $item_info, $groupname, $db_prefix;

$selectBox = '<select name="info1">';

$result = db_query("SELECT ID_GROUP, groupName
FROM {$db_prefix}membergroups
WHERE minPosts = -1",
__FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)) {
$selectBox .= "<option value='{$row['ID_GROUP']}'>{$row['groupName']}</option>";
}

$selectBox .= "</select>";
        return "Membergroup: ".$selectBox;

if ($item_info[2] == 0) $item_info[2] = 30;
'Time to stay Changed: <input type="text" name="info2" value="' . $item_info[2] . '" />';
}
Should look like:
Code: (php) [Select]
function getAddInput()
{

global $item_info, $groupname, $db_prefix;

$selectBox = '<select name="info1">';

$result = db_query("SELECT ID_GROUP, groupName
FROM {$db_prefix}membergroups
WHERE minPosts = -1", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($result)) {
$selectBox .= "<option value='{$row['ID_GROUP']}'>{$row['groupName']}</option>";
}

$selectBox .= "</select>";
return "Membergroup: ".$selectBox;

if ($item_info[2] == 0) $item_info[2] = 30;
'Time to stay Changed: <input type="text" name="info2" value="' . $item_info[2] . '" />';
}
While this doesn't really affect the code that much, it adds a level of readability to it :).

Quote
Here is what is messing up...
It looks like $item_info[2] may not be set correctly. Try changing that bit of code to
Code: (php) [Select]
echo "UPDATE {$db_prefix}members
SET ID_GROUP = {$item_info[2]}
WHERE ID_MEMBER = {$ID_MEMBER}"
die();
And see if the correct data is being output.

Offline feeble

Re: Coding some 'new' items...
« Reply #6 on: March 08, 2007, 05:55:02 pm »
Quote from: daniel15
Basically, as soon as return is called, PHP exits your function (and hence, any code underneath the first return line is not executed). I could be wrong, don't quote me on this Tongue

you can quote him on this.

Offline gondie

Re: Coding some 'new' items...
« Reply #7 on: March 09, 2007, 05:37:00 am »
Well I have most of the errors worked out of the Change Usergroup item...

Now the only problem is is that it doesnt place it in the usergroup its supposed to.. It removes all the permissions for that users (When I used the item on my testing account I think used a bad usergroup and that is why, it listed the time as the usergroup and the usergroup as the time), and the PM code isnt working either. I will work with this some more...

Thanks for your help, but do you want to know the sad part about a lot of the errors you guys found? I put a lot of them there trying to fix other errors...

Edit: I have fixed it! I will post some more info after I test it out a bit more.
« Last Edit: March 09, 2007, 06:00:33 am by gondie »