Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts

    array names as database column names

    is it possible to have column names as an array?

    Code:
    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


    PHP Code:
    $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:

    Code:
    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" }
    Last edited by Arcticwarrio; 04-17-2013 at 10:15 PM. Reason: added var_dump output
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Entirely separate? Sure.
    PHP Code:
    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.

    Edit:
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    yeah i wanted the column names as field[1] field[2] etc

    its ok i'll think of something else

    thanks
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    can you store an array in mysql?
    PHP Code:
    $results query("INSERT INTO `tracking` VALUES (
            NULL, 
            'array('a','b')', 
            '"
    .$_CLEAN['Customer']."')"
    etc
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i cheated lol

    names are now O1 O2 E1 E2 A1 A2 etc
    PHP Code:
    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); 
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •