Jump to content

Recommended Posts

Is it possible to do this

 

first store different things into an array

  • thing1
  • thing2
  • thing3
  • thing4

second store numbers into a mysql row

to record how much of each thing

0,1,1,5,2,8,3,10(data to store into th row)

so that would mean 1 of thing1

5 of thing2

8 of thing3 and

10 of thing4

would it be possible for PHP to parse all of that and output how much of each?

Link to comment
https://forums.phpfreaks.com/topic/55496-is-it-possible-to-do-this/
Share on other sites

Your question is pretty vague. Why would you want to store things in an array and there related amounts in a database. How are you going to relate the two separate sources of data?

 

Without really understanding what it is exactly your trying to do, I'm going to try an example. PHP has associative arrays. These are great for grouping keys / values of related data. eg;

 

<?php

  $things = array('thing1' => 0,'thing2' => 1,'thing3' => 1,'thing4' => 5);
  foreach ($things as $k => $v) {
    echo "There are $v of $k<br />";
  }

?>

Hmm.... Something like this:

 

<?php

$arr = (0,1,2,2,1,3,5,2,8,1,3,10,2);

$countArr = array_count_values ($arr);

print_r($countArr);

// Output:
// Array
// (
//    [0] => 1
//    [1] => 3
//    [5] => 1
//    [2] => 4
//    [8] => 1
//    [3] => 2
//    [10] => 1
// )


// To put in database

foreach ($countArr as $value => $count) {
 $values[] = "('$value','$count')";
}

$valueStr = "(" . implode(',', $values) . ")";

$query = "INSERT INTO table (value, count) VALUES " . $valueStr;
$result - mysql_query($query);

?>

 

How you would insert that into a database woud depend on the structure of your table.

It was supposed to be for a shop/inventory thing on a game I was working on

 

maybe it would be better l to create a row if the item row doesn't exist for the person and update it if it does exist.

my table layout for my inventory table is

id(id for the row goes here) uid(member id goes here) itemname(the items name goes here) amount(the items amount goes here)

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.