The online racing simulator
Storing in a "table" in PHP?
1
(31 posts, started )
Storing in a "table" in PHP?
Basically I want to get timing data with InSim and store it in a table format internally to update and search it frequently:

|Pos|PLID|Lap|LastSector|LastLap|Ahead|Behind|

This is in order to provide the data for buttons that show the time difference, and the change in time difference (eg [+0.38 (-0.01)] between the currently viewed driver and the driver one position ahead/behind.

My problem is transferring the data from the various packet objects to a suitable intermediary storage. Is there any (simple) way of doing this without using a temp file/db?
Im not sure about php myself but in C# theres arrays known as jagged arrays, They are basically arrays in arrays. Something like this....

[COLOR=blue]int[/COLOR][][] jaggedArray = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][6][];

You could use the first part of the array to store connection number or something, then the second part to store the data for that connection/user. Like i say, dont know if PHP has them but maybe?
Assuming the code is permanently running; A multidimensional array (as they're usually called in any sane language )?

$storage = array(array('plid' => $playerid, 'lap' => 0, ....), array('plid' => $playerid, 'lap' => 0, ....),)

You can index them however you like, and shuffle them...

HTH.
Grrr... I hate being wrong



E: wtf is HTH?
Yep, I was afraid multidimensional arrays is the way to do it. Just difficult to visualise it

Thanks y'all.

edit: Well, if both are arrays within arrays, you weren't really wrong ;o
I blame MSDN

[COLOR=blue]class[/COLOR] TestArraysClass
{
[COLOR=blue]static[/COLOR] [COLOR=blue]void[/COLOR] Main()
{
[COLOR=green]// Declare a single-dimensional array [/COLOR]
[COLOR=blue]int[/COLOR][] array1 = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][5];

[COLOR=green]// Declare and set array element values[/COLOR]
[COLOR=blue]int[/COLOR][] array2 = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][] { 1, 3, 5, 7, 9 };

[COLOR=green]// Alternative syntax[/COLOR]
[COLOR=blue]int[/COLOR][] array3 = { 1, 2, 3, 4, 5, 6 };

[COLOR=green]// Declare a two dimensional array[/COLOR]
[COLOR=blue]int[/COLOR][,] multiDimensionalArray1 = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][2, 3];

[COLOR=green]// Declare and set array element values[/COLOR]
[COLOR=blue]int[/COLOR][,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };

[COLOR=green]// Declare a jagged array[/COLOR]
[COLOR=blue]int[/COLOR][][] jaggedArray = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][6][];

[COLOR=green]// Set the values of the first array in the jagged array structure[/COLOR]
jaggedArray[0] = [COLOR=blue]new[/COLOR] [COLOR=blue]int[/COLOR][4] { 1, 2, 3, 4 };
}
}

http://msdn2.microsoft.com/en- ... rary/9b9dty7d(VS.80).aspx

Oh please... jagged arrays? Who comes up with this crap? Multi-dimensional... thanks.
Quote from JamesF1 :Oh please... jagged arrays? Who comes up with this crap? Multi-dimensional... thanks.

Im n00b
Who comes up with the names for Microsoft?
They must have meetings and be like,
"I know what we will do, just to p!ss off the community!"
"Let's call Multi-dimensional arrays, you know the thing that has been around for like over 10 years and, call it something else!"
"But what could confuse the community the most, and make our product even LESS intuitive then it already is!"
"Ah, I've got it, lets give it a name that no one would else would think of in there right mind! Jagged arrays!"
Hey, they could start using my variable names and called it a Rawr Array or a Moo Array
Go OO and define a class then make a simple array that contains instances and thus not deal with multi-dimensional mumbo-jumbo.

EDIT:
And for the record ragged/jagged arrays are not MS terms and they just define a specific type of multi-dimensional array.
Quote from xaotik :Go OO and define a class then make a simple array that contains instances and thus not deal with multi-dimensional mumbo-jumbo.

Sounds good to me
#14 - wien
Quote from xaotik :And for the record ragged/jagged arrays are not MS terms and they just define a specific type of multi-dimensional array.

Specifically one where the length of the sub-arrays (the 2nd dimension) vary. Hence "ragged".
Yes, there are different terms for different kinds of multi-dimensional arrays in C#, such as square and jagged.

As far as I'm aware the phrases were actually coined my MS, but as you need to define the arrays in slightly different ways a distinction was necessary.

The array mcgas001 posted originally is a jagged array, so he was correct as far as C# goes.
Being an object bigot, I just wonder among all the multi-dimensional this, jagged that, whether it wouldn't be more convenient to use a single dimensional array of objects that has the storage fields as access members?

As an aside, jagged/ragged terminology pre-dates C#. I learned about them when I first started programming Ansi-C on pre-Solaris Sun System IV.
#17 - Woz
Yep jagged, ragged, sparse (whatever you want to call them) is an old term used well before C#.
Ah, found a nice little thread to bump

If I have an object array like so:

<?php 
$guests 
= array();

// if received packet is ISP_NPL, put new guest object in array
$guests[$npl['uname']] = new guest($npl['uname'], $npl['pname'], $isp_header['ucid']);

// this is the guest class
class guest {
 var 
$uname;
 var 
$pname;
 var 
$ucid;
 var 
$plid;

 function 
__construct($uname$pname$ucid$plid NULL;) {
  foreach(
get_class_vars(get_class($this) as $var => $val) {
   
$this->$var = $$var;
  }
 }

 function 
__destruct() {}
}
?>

So basically each connection is a guest object, and all connections are stuffed into an array. My question is: what's the correct way of destroying one of these guest objects and removing all reference of the entry from the array?

Currently I just unset the array key, but I'm not sure the object is actually destroyed

<?php 
// if packet is ISP_CNL
foreach($guests as $guest) {
 if(
$guest->ucid == $isp_header['ucid']) {
  unset(
$guests[$guest->uname]);
 }
}
?>

Hope that made at least a little bit of sense
Quote from NotAnIllusion :Currently I just unset the array key, but I'm not sure the object is actually destroyed

The object will be destroyed once there are no more references to it, unsetting the array key will result in destroying the object if the array of guests is the only place you're storing references to the guest objects.


<?php 
public function __destruct() { echo "I'm being destroyed!"; }
?>

Heh, well. Unsetting the array key doesn't trigger the destructor echo, so there's a reference somewhere. Now if I'd just find it Could it be because I do foreach($guests as $guest) at various places in my code?

Isn't it possible to call the destructor to force it to, well, be destroyed? It would be a doddle, but I don't know what the actual object is called, lol. It's so confusing.
As far as I'm aware not only are jagged arrays and multidimensional arrays something quite different (jagged array = array of arrays, each sub-array can have a different length, whereas multidimensional arrays have a fixed size for each dimension you specify), but the former are also slightly faster. IIRC one-dimensional array access has a few speed optimizations and with jagged arrays it can use these optimizations multiple times (since you're always just accessing one-dimensional arrays). A multidimensional array does not have these optimizations and needs to do a full lookup.

However I'm not sure if that's true for PHP or modern languages in general. It's quite a while since I read that.
Quote from NotAnIllusion :Could it be because I do foreach($guests as $guest) at various places in my code?

If you're doing this in global scope or in the bottom scope of a "main loop" it might leave a reference to one guest in $guest.

Quote from NotAnIllusion :Isn't it possible to call the destructor to force it to, well, be destroyed?

Short answer: No.
Well, I'm not really concerned with the multidimensional arrays atm, because the object array does what I need.

The new problem is that when I receive an ISP_CNL, if I do the following to destroy the leaving connection's guest object, the array still shows my guest object until I unset the array key:

<?php 
// if packet is ISP_CNL
foreach($guests as $guest) {
 if(
$guest->ucid == $isp_header['ucid']) {
  
$guests[$guest->uname]->__destruct();
  
print_r($guests)
 }
}
?>


ip : cnl
[color="red"]I'm being destroyed![/color]
Array
(
[] => guest Object
(
[uname] =>
[pname] => host
[ucid] => 0
[plid] =>
)

[notanillusion] => [color="red"]guest Object[/color]
(
[uname] => notanillusion
[pname] => ^1Pringles
[ucid] => 13
[plid] =>
)

)

So I'm confused, shouldn't calling the destructor as in this post destroy the guest object in the array, and not show it next to my name anymore

edit: so it's not possible to call the destructor in that way to erase all references of the object. Roger. Guess I'll try to figure out a way to make sure all those guest objects are definitely removed. Ty

*goes back to drawing board*
Quote from NotAnIllusion :So I'm confused, shouldn't calling the destructor as in this post destroy the guest object in the array

Calling the destructor doesn't destroy the object, it's simply a convenience method that gets called automatically when the object is actually being destroyed.

To destroy the object you should unset the reference(s) to it.
Ok, that I can understand. A plot is beginning to emerge
1

Storing in a "table" in PHP?
(31 posts, started )
FGED GREDG RDFGDR GSFDG