0

I'm building a web-app that has a rather large (10K rows) reference table.

Table reference has a category, id , description.

As I'm using this reference a data a lot to fill drop-down boxes and such, I'm trying to put them in arrays. I had them in one large array it first, but it seems to me it's more efficient to have an array per category within the reference table.

The dynamic variable names are created, but populating them from another array is not working.

$r_refcats = mysqli_query($global_dbh,"select distinct(cat) from reference");
while(($row = mysqli_fetch_array($r_refcats)) != false){ $$row[0]=array();};

$r_references = mysqli_query($global_dbh,"select cat,id,description from reference");
while(($row = mysqli_fetch_array($r_references)) != false) { $$row[0]=array($row);}

Because I can't use a $$row[0][] in the last line, it keeps overwriting the same value :)

Any tips on how to do this correctly?

Alex W
  • 37,233
  • 13
  • 109
  • 109
MarlonB
  • 63
  • 1
  • 6
  • I think your premise is wrong: having lots of dynamically named variables will not make PHP more efficient than one array with lots of dynamic keys; in fact, internally the data will look very similar. What it will definitely do is make your code harder to read, and prevent you using the builtin array functions or even splitting your code into separate functions (since you'd have to somehow pass all your variables at once). – IMSoP Dec 13 '13 at 22:33
  • I have to loop many times through the array to populate dropdownboxes. It should save me some if conditions and having to loop through the large array each time if i split up the arrays in categoties. Or do i misunderstand that? – MarlonB Dec 15 '13 at 13:07
  • You don't want one list with all the entries in, but an outer array with categories for keys and the separate lists for values is much simpler than what you have here. Accessing a single category whose name you know in advance would just require you to type `$categories['foo']` instead of `$foo`. OTOH, looping through *all* categories with completely separate variables would be all but impossible. – IMSoP Dec 15 '13 at 19:47

2 Answers2

0

How about array_push(), it should treat it the same as []:

while(($row = mysqli_fetch_array($r_references)) != false) {
    //$$row[0]=array($row)
    array_push($$row[0],array($row));
}
Samuel Cook
  • 16,620
  • 7
  • 50
  • 62
0

You need to add to the array, not just rewrite it:

$r_refcats = mysqli_query($global_dbh,"select distinct(cat) from reference");
while(($row = mysqli_fetch_array($r_refcats)) != false) {
    ${$row[0]}=array();
}

$r_references = mysqli_query($global_dbh,"select cat,id,description from reference");
while(($row = mysqli_fetch_array($r_references)) != false) {
    ${$row[0]}[] = $row;
}

Also, I think you just want to use $row as your array, not an array of $row. In other words, I think you want to have the data be the data in $row, not array(0=>$row) -- hence my edit to the last line.

elixenide
  • 44,308
  • 16
  • 74
  • 100
  • The key insight here is the `${$name}` syntax to make clear which is the dynamic variable name and which is the action on that variable. – IMSoP Dec 13 '13 at 22:37