...

View Full Version : array names as database column names



Arcticwarrio
04-17-2013, 10:09 PM
is it possible to have column names as an array?


TrackID | OPS['0'] | OPS['1'] | OPS['2'] | OPS['3'] | E['0'] | E['1'] | E['2'] | E['3']

etc
COMMENTED MY ERROR LINE
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\WebBook\tracking.php on line 74




$results = query("SELECT * FROM `tracking` WHERE `TasNO` = '".$_GET['TasNO']."'", __LINE__, __FILE__);
if (mysql_num_rows($results) == 0) {
$_SOURCE = &$_GET;
}else{
$_ROW = mysql_fetch_array($results);
$_SOURCE = &$_ROW;
}

var_dump($_SOURCE);
foreach ($_SOURCE['OPS'] as $k => $v){ // <---- LINE 74
if ($v >= 1){
$Content .= '
<tr>
<td>'.$ops[$k].' Estimate: <input type="hidden" name="OPS['.$k.']" value="'.$_SOURCE['OPS'][$k].'"></td>
<td><input type="text" name="Est'.$k.'" value="'.$_SOURCE['E'][$k].'"> </td>
<td>Actual: </td>
<td><input type="text" name="Act'.$k.'" value="'.$_SOURCE['A'][$k].'"></td>
</tr>
';
}



Thanks

Arctic.

P.S.

Vardump Output:


array(66) { [0]=> string(1) "1" ["trackID"]=> string(1) "1" [1]=> string(6) "216173" ["TasNO"]=> string(6) "216173" [2]=> string(11) "DH PROJECT " ["Customer"]=> string(11) "DH PROJECT " [3]=> string(1) "1" ["OPS['0']"]=> string(1) "1" [4]=> string(1) "0" ["OPS['1']"]=> string(1) "0" [5]=> string(1) "0" ["OPS['2']"]=> string(1) "0" [6]=> string(1) "0" ["OPS['3']"]=> string(1) "0" [7]=> string(1) "0" ["OPS[4]"]=> string(1) "0" [8]=> string(1) "0" ["OPS[5]"]=> string(1) "0" [9]=> string(1) "0" ["OPS[6]"]=> string(1) "0" [10]=> string(1) "0" ["OPS[7]"]=> string(1) "0" [11]=> string(1) "0" ["OPS[8]"]=> string(1) "0" [12]=> string(1) "0" ["OPS[9]"]=> string(1) "0" [13]=> string(5) "52.65" ["E['0']"]=> string(5) "52.65" [14]=> string(4) "0.00" ["E['1']"]=> string(4) "0.00" [15]=> string(4) "0.00" ["E['2']"]=> string(4) "0.00" [16]=> string(4) "0.00" ["E[3]"]=> string(4) "0.00" [17]=> string(4) "0.00" ["E[4]"]=> string(4) "0.00" [18]=> string(4) "0.00" ["E[5]"]=> string(4) "0.00" [19]=> string(4) "0.00" ["E[6]"]=> string(4) "0.00" [20]=> string(4) "0.00" ["E[7]"]=> string(4) "0.00" [21]=> string(4) "0.00" ["E[8]"]=> string(4) "0.00" [22]=> string(4) "0.00" ["E[9]"]=> string(4) "0.00" [23]=> string(4) "0.00" ["A[0]"]=> string(4) "0.00" [24]=> string(4) "0.00" ["A[1]"]=> string(4) "0.00" [25]=> string(4) "0.00" ["A[2]"]=> string(4) "0.00" [26]=> string(4) "0.00" ["A[3]"]=> string(4) "0.00" [27]=> string(4) "0.00" ["A[4]"]=> string(4) "0.00" [28]=> string(4) "0.00" ["A[5]"]=> string(4) "0.00" [29]=> string(4) "0.00" ["A[6]"]=> string(4) "0.00" [30]=> string(4) "0.00" ["A[7]"]=> string(4) "0.00" [31]=> string(4) "0.00" ["A[8]"]=> string(4) "0.00" [32]=> string(4) "0.00" ["A[9]"]=> string(4) "0.00" }

Fou-Lu
04-17-2013, 10:26 PM
Entirely separate? Sure.


while ($row[] = mysql_fetch_assoc($results)) or array_pop($row);
$cols = array_keys($row[0]);

$cols would be an array of the keys associated with row.


Wait, do you mean in mysql? Just looking at the var dump here: ["OPS['0']"]=> string(1) "1" [4]=> string(1) "0" ["OPS['1']"]=. That indicates your column names are OPS['0'], which no you cannot do. Doesn't make a whole lot of sense though; properties are not arrays in dbms, to do that you want to use a many to many relationship.

Arcticwarrio
04-18-2013, 07:38 AM
yeah i wanted the column names as field[1] field[2] etc

its ok i'll think of something else

thanks :)

Arcticwarrio
04-18-2013, 07:40 AM
can you store an array in mysql?


$results = query("INSERT INTO `tracking` VALUES (
NULL,
'array('a','b')',
'".$_CLEAN['Customer']."')";

etc

Fou-Lu
04-18-2013, 02:54 PM
You *can*, but it defeats the purpose of using a db since there's no integrity in doing so. Searching is a nightmare, updating would require a potential change in every record and deleting is just as difficult. To do that you would simply implode it or use a serialized array. You will eventually hit a char length limitation though OR have a column that is massive in size when its not needed (and eventually hit the same limitation anyway).
What you want to do is create many to one or many to many relationships using the dbms' structure. One to many is simply taking the many table and providing it a column association to the one table's primary key. An example would be a user that can upload many pictures. Each picture is owned only by that user, so the picture tracks what user it belongs to.
A many to many requires three tables (unless its self then it needs only two). An example of that would be a user and a usergroup where the user can belong to many groups. Neither the user nor the group track information about the other, and a third table is designed which creates a composite key based on the two foreign keys of each table. Each tuple within the third table tracks a user and the group they belong to.
This design allows for easy searching, easy modifications, and easy deletions, especially if cascades are used. Or you can restrict until you've performed other tasks (ie: you cannot delete a usergroup until all users have been removed from that group). The only thing this structure doesn't guarantee is that a record will exist with an association.
Joining is a lot more efficient than grabbing all records and then manipulating the data to determine if you found what you are looking for.

Arcticwarrio
04-23-2013, 08:32 AM
i cheated lol

names are now O1 O2 E1 E2 A1 A2 etc


foreach ($_SOURCE as $sk => $sv){
if (substr($sk,0,1) == 'O'){
$_SOURCE1['O'][substr($sk,1,1)] = $sv;
}
if (substr($sk,0,1) == 'E'){
$_SOURCE1['E'][substr($sk,1,1)] = $sv;
}
if (substr($sk,0,1) == 'A'){
$_SOURCE1['A'][substr($sk,1,1)] = $sv;
}
}
$_SOURCE = array_merge($_SOURCE, $_SOURCE1);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum