Jump to content

Recommended Posts

I know this is possable, but I'm not quite sure where to begin looking / coding

 

What I need to achieve is this:

 

I have a field in my database, that contains a list, chunked out in groups, using square brackets.  The order is:

[item][Quantity][item2][Quantity2] ...
[Trees][300][boxes][4000][Gizmos][3]
==> Yeilds: 300 Trees, 4000 Boxes, 3 Gizmos

 

When I retrieve that list, I need to iterate through each element, adding the item into an array, and it's corresponding quantity into another array (each chunk at the same index).

 

Where should I start?

 

PS: I know this isn't all that practical, I'm trying to use this as a learning exercise is all.

Link to comment
https://forums.phpfreaks.com/topic/181907-iterate-through-items/
Share on other sites

While I'd probably do it in a multidimensional array, if you insist on using two separate arrays..

 

Edit: Looks like you didn't want a solution, just a starting point. Look into explode and trim.

 

A solution:

<?php
$itemStr = '[Trees][300][boxes][4000][Gizmos][3]';
$items = array();
$quantity = array();

$itemList = trim($itemStr, '[]');
$itemList = explode('][', $itemList);
for($i = 0; $i < count($list); $i += 2) {
    $items = $itemList[$i];
    $quantity = $itemList[$i+1];
}
?>

Why people store lists in databases like this is beyond me.

 

Anyways:

<?php
$val = '[Trees][300][boxes][4000][Gizmos][3]';

// remove trailing [ and ]
$val = trim( $val, '[]' );

$values = explode( '][', $val );
$max = count( $values );
for( $i = 0; $i < $max; $i += 2 ) {
  echo sprintf(
          "There are %s %s\n",
          $values[$i + 1],
          $values[$i]
      );
}
?>

Well that was much easier than I had originally expected it to be...

 

Alright, turning this whole idea upsidedown... How do I construct a table (well, multiple technically [the fewer the better]) that will allow for the following...

 

Let's pretend we're making a game.  In this game, there are blueprints.  A blueprint is a set of instructions that allows a user to produce an item by using other ingame items.

 

Example:

To Create:
Raven (ship): 11k Tritanium, 9k Pyerite, 2k Mexallon
Golem (ship): 1 Raven, 1000 Morphite, 4 Sensor Clusters, 5 Propulsion Units, 7 Hardpoints
Sensor Cluster (item): 300 Hypersynaptic Fibers, 90 methoflourine Plates, 8 morphite, 500 tritanium
Propulsion Unit (item): 470 Tritanium, 4 construction blocks, 1 Exotic dancer (lewl?)
Hardpoint (item): 7 crystaline carbide, 8 tungsten plating, 90 isogen, 30 zydrine
Exotic Dancer (Commodity): Bought off market

 

We're talking setting up a huge relational database here. It's totally doable, just a pain in the ass for the situation that it's going to be used in.  Essentially, there are thousands of blueprints and items in the game, but only a small handful that the user will be concerned with (only the stuff they need to use).  What's the most logical method of setting up relations here, if not simply comparing known data to what's being dumped from an ordered list?

 

Edit: I suppose I should add in a purpose here...

 

Hopefully, the goal here would be to have a quick access to a list of the owned blueprints for an individual player.  When one is selected, a bunch of data is displayed from it (total minerals required among other).

 

If raven is Tier 1 and Golem is Tier 2, I suppose a table for Tier 1, Tier 2, Components, and Minerals could be created.  and a for loop could be used to assess the returned data from a T2 ship and just build up an array.  Then math could be done...  Yes?  The issue with that though would potentially having a for each loop within a while, and if memory serves, PHP doesn't like that...

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

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

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.