darkeye Posted December 8, 2009 Share Posted December 8, 2009 Hello again, I have a question in regards to the IM bot I am building. I want it to have a 10 slot 'inventory,' where it is holding (at max) 10 items. Then, whenever someone says "/me gives bot [something]," the bot will delete the oldest item from its inventory, and put something in its place. Now I know I have to create a table (inventory) with two columns. (item, time). Then I have to put a timestamp on all insert statements (DATETIME, NOW()?) so that it will be able to decide on the oldest entry. Here is my current code: <?php $haystack=$_REQUEST['msg']; $needle='<reply>'; $username="admin"; $password="adminpassword"; $database="database"; $pos1=stripos($haystack, $needle); if($pos1!==false){ $pos2=$pos1+7; $condition=substr($haystack, 0, $pos1); $reply=substr($haystack, $pos2); $query="INSERT INTO responses VALUES ('".$condition."','".$reply."')"; $int=0; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $print='stuff'; $loop="yes"; while ($loop=="yes"){ $testquery="SELECT * FROM `responses` WHERE `condition`='".$condition.$int."'"; $test=mysql_query($testquery); if(mysql_num_rows($test) > 0){ $int=$int+1; } else{ $query="INSERT INTO responses VALUES ('".$condition.$int."','".$reply."')"; mysql_query($query); mysql_close(); echo "success if no error message."; $loop="no"; } } } else if('bucket: remember that' == $_REQUEST['msg']){ $value=$_POST['step']; $value=$value-2; $writestring=$_POST['value'.$value]; $writestring=$writestring."\n\n"; echo 'Okay, Remembering: '; echo $writestring; $file='remember.txt'; file_put_contents($file, $writestring, FILE_APPEND); } else if('bucket: remember all' == $_REQUEST['msg']){ echo 'www.mywebsite.com/remember.txt'; } else{ mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $query="SELECT * FROM `responses` WHERE `condition` LIKE '".$_POST['msg']."%' ORDER BY RAND() LIMIT 1"; $result=mysql_query($query); $print=mysql_fetch_assoc($result); echo $print['reply']; mysql_close(); } ?> I have the feeling I need to insert another stripos statement somewhere, and then use substr to get [something]. After this point, I have no clue how to determine the oldest entry, delete that one, and then insert the new one. Another problem is that I can't figure out where to put the stripos block of code. I tried making its own else if (but due to either poor coding or something else) The bot didn't respond, it just sat there. Any Suggestions on what to do? Link to comment https://forums.phpfreaks.com/topic/184474-inventory/ Share on other sites More sharing options...
darkeye Posted December 9, 2009 Author Share Posted December 9, 2009 *Bump for Help* Any ideas? Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974178 Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 Something like the following? : DELETE FROM table WHERE owner = 1 ORDER BY timeAdded ASC LIMIT 1 Query in English: Delete the oldest row that user 1 owns. Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974186 Share on other sites More sharing options...
darkeye Posted December 9, 2009 Author Share Posted December 9, 2009 Alright. So I would just do something like this? <?php $needle2="/me gives bucket"; $pos3=stripos($haystack, $needle); if($pos3!==false){ $pos4=$pos3+16; $insertitem=substr($haystack, $pos4); $delquery="DELETE FROM table WHERE owner = 1 ORDER BY timeAdded ASC LIMIT 1"; $query="INSERT INTO inventory VALUES ('".$insertitem."','".NOW()."')"; mysql_query($delquery); mysql_query($query); } ?> Would that work for what I need it for? Where should I put it in my code? Thanks again! Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974197 Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 Well, you'd need to change the table name (to inventory), if you have an owner or not (user-specific or if you just want to do a dumping ground for all users), and the timeAdded column to whatever you have your timestamp column named to. Edit: on a side note you could also just have a set number of rows (x) and just update the oldest one each time instead of deleting/inserting for each item. Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974205 Share on other sites More sharing options...
darkeye Posted December 9, 2009 Author Share Posted December 9, 2009 so it would have to be: <?php $delquery="DELETE FROM inventory ORDER BY time ASC LIMIT 1"; ?> my time column is called "time"; Should I just put this block of code just below the original stripos statement? Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974210 Share on other sites More sharing options...
Philip Posted December 9, 2009 Share Posted December 9, 2009 Yup. Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974259 Share on other sites More sharing options...
darkeye Posted December 9, 2009 Author Share Posted December 9, 2009 Thanks. Link to comment https://forums.phpfreaks.com/topic/184474-inventory/#findComment-974387 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.