sparkx 0 Report post Posted April 29, 2007 I get an odd little error when I tried to use foreach with require. It seams that it should work but I can't seem to figure it out. I am a little new at php so I don't know the best way to discribe the problem. I use the following code (simplified for post): <?php$arr=array("Sparkx", "Studios");$include = "/My/Directory";foreach ($arr as &$value) {include($_SERVER["DOCUMENT_ROOT"]."$include/test.php?test=$value");}?> My page being included has a simple code that looks up and displayes information about the user. What I want to do it have it so that my page lookes up different user's details and all the information on one page (like a members list).Example:Username: Sparkx ID:1Username: Studios ID:2With the code above however I get the "Failed opening" error. Any suggestions? Am I on the right track?Thanks for the help,Sparkx Share this post Link to post Share on other sites
develCuy 0 Report post Posted April 29, 2007 Hello sparkx!First of all. your code "foreach ($arr as &$value)", does not needs the "&".Sometimes is better to use this:foreach ($arr as $id=>$value)Anyway. After read your comment, i suggest to you that to safe time a get better involved on PHP. Use CakePHP. Actually i'm developing with this great framework and the results are very good.Have a nice coding. Blessings! Share this post Link to post Share on other sites
Quatrux 4 Report post Posted April 30, 2007 You can only include files, so the query string isn't needed, it is a bit stupid to use it in my opinion, you just need to include the file and check the $value there, you have an array which you can use in that file, so if you remove the query string, everything will work as you want, even thought this method is a bit "stupid" to do what you want to do. Share this post Link to post Share on other sites
sparkx 0 Report post Posted May 2, 2007 Could you tell me the best way to do this type of a thing? Im am stuck and getting a little mad. I don't undstand:IF a database entry has 1,2 and is connected to with the variable $example and used like $arr=array($example);foreach ($arr as &$value) {echo<<<ENDYour saved value is $exampleEND;}Is different then the array being:$arr=array(0,1);But apparently you cant array db feilds. OK so could someone just give me ideas about what I should do in my situation. This is a close example to but not exact situation. It is simplifyed a little.I have an multi-player online game based 100% off of php (yes it is stupid but its a good way to learn php). A user can have an inventory. I wan't the users inventory to display the item name, discription and quantity they have. I wan't however the discription to be connected to anouther database table so it I can edit everyone's discription in just one quary (I know how to do that). The problem I am having is repeating and displaying the code that does it. First I connect to the user's sql and get the items they have and thier quantity. Then I look up the items they have and display it's details. I wan't to have a simple layout like:Items:ID: 0 | Discription: Can help you out | You own: 5ID: 2 | Discription: Can help your friends out | You own: 2 Is this it possible for me to do this? Could you help me out by providing example codes or links to tutorials that might help? That would be great.Thanks,Sparkx Share this post Link to post Share on other sites
bluefish1405241537 0 Report post Posted May 3, 2007 Well, how far have you gotten with the database? Do you have a database yet? Do you know how to do simple MySQL (I assume MySQL) queries? If so, you should use two different SELECT queries - one on the user's table that should fetch the row with that user's username, and one for each item to check it's description. For example: $q1 = mysql_query("select `items`, `quantities` from `users` where `username`='$username';"); //Get item data from user$array = mysql_fetch_array($q1); //Put into array$items = explode(",", $array['items']); //Set $items as an array of ids, assuming that items are stored as e.g. "0,1,2,3"$quantities = explode(",", $array['quantities']); //Set $quantities as an array of numbers, assuming that they are stored as e.g. "2,1,7,4", synchronized with the items fieldfor($i=0;$i<count($items);++$i) { //Start a loop for each item$id = $items[$i];$description = (get from mysql, using item id);echo "Id: ".$id." | Description: ".$description." | You own: ".$quantities[$i];} Sorry about the bit of pseudocode there - I didn't really want to write all the SQL. I assumed that IDs and quantities are stored in two comma separated fields - the first id in the ID field corresponds with the first quantity in the quantity field. If you need more clarification, just ask.As for tutorials if you don't know MySQL, I learned my first PHP from phpbuddy.com (see Working with Database), but I'm afraid I don't know any other tutorials. The official MySQL manual is good for checking syntax, but not much else. Share this post Link to post Share on other sites
vujsa 0 Report post Posted May 3, 2007 Okay, I'm a little confussed as to what you want to do.My concerns have several aspects:First, why not access the database directly instead of trying to access a page that accesses the database. The process ia very inefficeint the way you are attempting it.Your script should access the database directly and the query results will be returned as an array that you can use as you wish. Second, as suggested, the inclusion of a file with a query string url is complicated! I've never seen a need to use that method which wasn't better accomplished some other way. Basically, the include function includes the PHP code from that file, not the output of that file. I suppose you could buffer the output first but again, there are better methods available. If you are trying to extract data from an outputed webpage, then file_get_contents is probably the better method. Additionally, you can assign the contents of the outputed data to a variable!Third, the foreach function is a little complicated if you aren't familiar with it. Basically, you are modifying the original array based on the statements in the loop. Until you get used to the concept of the foreach loop, I suggest a regular for or while loop to do the same task. It is a little less efficeint but easier to master.I would like to caution other members against refering to someone's coding method or question as being "stupid". We all had to learn at some point and belittling a new coder isn't helpful to anyone. If you could better explain what you are trying to accomplish, it would be easier to direct you in the right direction.Look forward to providing you with a little assitance.vujsa Share this post Link to post Share on other sites
sparkx 0 Report post Posted May 5, 2007 (edited) Thanks bluefish and vijsa. I ended up useing the explode method. I am trying to make a game but few places on the internet have good tutorials for php and even fewer of them use my method. Now I have got a decent (low end) inventory now I need to set up a way to buy things. First I checked to see if the user has enough money then I would subtract the cost and add the id to the sql entry. As before however I have the 0,1 ect method in the being used in the sql (I know its not the best way). I simply add the variable ($ID) to the end of the sql statment. Anyone relize my problem yet though? If someone buys the item again they will recieve two entries for the same ID (0,1,1). This messes me up a little (as my items are stackable). I want to eventually store a key and a value in the sql (one for the item ID the other for the quantity owned). I tired an IF statment but because the variables don't match the problem continues. Is there a way I can search the sql entry for a value so lets say it this is the first of an item someone gets, it simply adds the var to the end of the sql, but if they aready have this item it adds to the quantity without duplicating the item ID. Do you understand what I am saying?Thank you all for the help that you have provided me with in php. This is by far the number 1 resource for me learning php,Sparkx Edited May 5, 2007 by sparkx (see edit history) Share this post Link to post Share on other sites
miCRoSCoPiC^eaRthLinG 0 Report post Posted May 6, 2007 Yup... what you're seeking should be fairly simple... simply keep a COUNTER field that'll keep track of how many of an item is owned by the user.. I'm demonstrate a simple example here.. say your inventory table looks like this. ----------------------- USERID - Integer ITEMID - Integer ITEMCOUNT - Integer PRIMARY KEYs - both USERID and ITEMID together ------------------------ When a user buys an item, simply get the item's ID and check if the user owns the item with the following SQL statement... $query = mysql_query("SELECT * FROM inventory_table WHERE USERID = 'users id' AND ITEMID = 'items id'");$result = mysql_num_rows($query); The returned result will be ZERO if the user doesn't own the item at all and will be '1' if the user has the item beforehand. Alternatively, you could check the ITEMCOUNT field itself... $query = mysql_query("SELECT ITEMCOUNT FROM inventory_table WHERE USERID = 'users id' AND ITEMID = 'items id'");$result = mysql_num_rows($query); The returned result will be ZERO if the user doesn't own the item at all and will be a non-negative number if the user has one or more of the item beforehand. Now you fork the code logic in 2 paths... Path A - You got ZERO as a result Issue an INSERT statement to create a new entry for this item... like... $query = mysql_query("INSERT INTO inventory_table ( USERID, ITEMID, ITEMCOUNT ) VALUES ( 'users id', 'items id', '1'); You should insert a 1 here for ITEMCOUNT, coz this is the first time the user is buying the item and post-purchase owns only one piece of it. Path B - You got a non-negative number or '1' as a result Instead of trying to manually figure out how many items the user has and adding a 1 to it, you should let mysql increment the field for faster operation... $query = mysql_query("UPDATE inventory_table SET ITEMCOUNT = ITEMCOUNT + 1 WHERE USERID = 'users id' AND ITEMID = 'items id'; In case you provide the user with the option of buying MORE THAN ONE of the same item at a time, increment the value by a suitable number instead of just '1'. Say a variable called $number_bought contains the number of items bought, your code should look like... $query = mysql_query("UPDATE inventory_table SET ITEMCOUNT = ITEMCOUNT + $number_bought WHERE USERID = 'users id' AND ITEMID = 'items id'; That should effectively deal with what you want to do... If you need further clarifications on any of the above points, write back.. Cheers, m^e Share this post Link to post Share on other sites
bluefish1405241537 0 Report post Posted May 6, 2007 Another way of doing it that might be simpler would be to:1. When the user buys the item:2. Loop through the array of item ids;3. At each iteration of the loop, check to see if the item ID is the same;4. If so, increment the quantity value and update that for the database;5. If not, add the ID.Also, you could just have that multiple ID thing, and have a code like the following to put it in a key-value pair of items and quantities. e.g. $array = explode(",", $item_ids); //Create array of item IDs, including multiple entries$keyval = new Array(); //Create the array to hold the key-value pairsforeach($array as $id) { //Loop through each IDif(isset($keyval[$id])) { //Check if the id has already been seen once$keyval[$id]++; //If so, increment the count} else {$keyval[$id] = 1; //If not, set it to 1}}The only problem with this method could be that some parts of the array are undefined and you need to be careful when accessing values. If you do use this, use the isset function to check if there is at least 1 of the item. Share this post Link to post Share on other sites
yordan 10 Report post Posted May 7, 2007 I was just wondering on a phylosophical point of view. You want to create a game. This game has to run on a server which has php and mysql ? So, this will not be a home game, a standard person cannot install it on his PC, unless he also installs a php and a mysql server ? Or is it a online-only game ? Or is it only for learning purposes, creating a game as a funny way of learning php and mysql ? RegardsYordan Share this post Link to post Share on other sites
sparkx 0 Report post Posted May 8, 2007 Its an online php game. It doesn't really have a main format so its not a 'regular' game. I think it is a great way to learn php, but thats me. Ill show you it when I finish. Its real basic but I am getting soo much done but I keep running into little errors here and there. I guess that is expectable as this is my first php project. Im sorry if it seems like Im asking too many questions but in compairesion to what I am getting accomplished it is actually very few. Anyway...I have an array that uses the key=>value method and I wanted to check if a value existed in that array. I use this code: $array = explode(',',0=>0,1=>1);$check = ($amount.'=>'.$ID);if (in_array($check,$array, TRUE)){true}else{false}Now I know this is a bad way to handle this but anyway. $ID is a set amount recieved from a form (and is the only thing I want to check), but amount can be changed (meaning the array 5=>1 which is the same ID but different amount). This will not work if the person chooses to buy more of that item this statment will go to false. Thats not good. I know I am very confusing and if you didn't understand any of what I just said thats ok. My ideas of fixing this are to have an any value set to $amount or in_string statment (if exists).Is there a way I can set a variable so that it can be anything and will be true no-mater what? Anouther way I was thinking is if there was such thing as in_string (like in_array but doesn't require the commos).Thanks again for all the help. Everyone here is very good at php I owe you guys big time,Sparkx Share this post Link to post Share on other sites