• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

php: variable variable names in a class

I want to create a generic class containing variable names based on the field names of an SQL query. In other words, I don't want to hard-code the names of the variables inside the class. Is that possible? This code doesn't work and is probably the wrong method, but it might give you an idea of what I am trying to do.

class whatever {
for ($x = 0; $x < mysql_num_fields($result); $x++)
var "$" . mysql_field_name($result, $x);
}
 
well, for starters

is that really your code? or did you just retype part of it?

should be

class whatever {
function myfunction($result)
{
for ($x = 0; $x < mysql_num_fields($result); $x++)
var "$" . mysql_field_name($result, $x);
}
}

are you also making a new query function to use instead of mysql_query?
that way your new function can call the mysql_query and follow it up with whatever->myfunction($resource)


 
that's the code I tried but it did not work, there may be numerous things wrong with it. I will try creating the variables inside a class function like you showed

i took a couple of programming courses with a good instructor and we used C++ and am taking a class involving PHP right now, but I'm not an advanced programmer... i wasn't planning to remake the mysql_query function; i'm hoping that's not necessary...
 
i guess what i want to know is if my concept is legitimate, using a for loop to create variable names of a class based on other variable names

for some reason, i don't think the var "$" . mysql_field_name($result, $x); is legitimate
 
http://www.php.net/manual/en/l...variables.variable.php
this says if variable var_a holds a value "asdf", I can use $$var_a to make a new variable called asdf. Here is the code I am trying, but it doesn't work with the var $$field_name; line uncommented. Any ideas?

class whatever
{
function whatever($result)
{
for ($x = 0; $x < mysql_num_fields($result); $x++)
{
$field_name = mysql_field_name($result, $x);
echo $field_name . "<br />";
// var $$field_name;
}
}
}
 
What is it, exactly, that you're trying to do?

I'd have to imagine that there are better ways to do it than dynamic variable names...particularly in the context of a class.
 
well, i want to display a table from a database and be able to sort and filter the table through javascript, so i am writing it all as a php document and echoing all the html and javascript, but I wanted to put each row of the table into an object so I can work with them... but maybe that is not necessary, maybe I would be better off just echoing the table directly into an html table and manipulating the table, row, and cell objects with javascript... i wish I could be more clear but I'm kind of rusty in my programming and don't have a lot of experience... like I said, I am assuming that the way to use php, html, and javascript together is to make the document php and echo all the html and javascript, but maybe i'm wrong there too, just looking for any tips I can find

oh, and another of my goals is not to hard-code any of the table names or column names into my php document because I want it to work for any sql table query
 
Well, if you're sorting the rows in Javascript, and not in PHP, then PHP doesn't really need to care about what the headings are. All PHP needs to do is output two arrays: one with headings and one with the data. Javascript then takes care of sorting and rendering the table. This would really only work well if you were not doing any pagination or searching and had a fairly small results set.

If you're doing any searching or pagination, however, you'd probably want PHP to take care of all rendering...which can still be done generically, and PHP still doesn't really need to know what the headings are.

For instance, if your query was something like this:

SELECT * FROM table ORDER BY ${REQUEST['orderfield']} ${REQUEST['orderdirection']};

Then you could use the following pseudocode to construct your table:

To generate the header:
$header = "<tr>";
for (i = 0 to number of fields)
{
if (field(i) = $REQUEST['orderfield'])
$header .= "<td>[SORT INDICATOR] fieldname</td>"
else
$header .= "<td>fieldname</td>"
}

To generate the body, once again:
for (i = 0 to number of rows)
{
for (j = 0 to number of fields)
{
//construct table row based on your values of the current row/column pair
}
}

This is far more scalable, as if you ever wanted to use pagination or searching, you no longer need to pull the entire dataset into the javascript page...you simply end up with a different data set and you only transmit to the user the data that fits in the dataset. Also, you never have to change your rendering...that stays the same no matter what the results of your query are.

Now, if you're using AJAX, that's completely different...but something tells me that you're probably not.

Also, it doesn't make sense to put each row of results into an object. That implies that you're going to use PHP to sort the results. When dealing with a database, you should always manipulate your data as much as possible in the context of the database, because it will always be faster than in code.

Consider this: you have a dataset with 10000 records, of which you want the first 10, and you want them sorted by balance due. Are you really going to load all 1000 records and sort them manually in code, whether that be PHP or javascript or whatever? No, you're going to let the RDBMS take care of that, because that's what it's for.
 
I am not planning to use any search or pagination, but I might want to depending on how satisfied I am with what I end up with here. The reason I want to use JavaScript to do the filtering and sorting is partly that it will be (at least I hope it will be) nearly instantaneous to the user. I want the user interface to be such that they can change the filter parameters and see the results without waiting to encourage them to change their "search" as much as they like. We have touched on AJAX slightly in my class, and it would probably be better than straight PHP if I decide to do all this on the back end. I guess their wait time with AJAX would be less than it would be with just PHP, but still dependent on their Internet connection speed. We'll see how it turns out. Thanks for your input.
 
Originally posted by: adsmith82
I am not planning to use any search or pagination, but I might want to depending on how satisfied I am with what I end up with here. The reason I want to use JavaScript to do the filtering and sorting is partly that it will be (at least I hope it will be) nearly instantaneous to the user. I want the user interface to be such that they can change the filter parameters and see the results without waiting to encourage them to change their "search" as much as they like. We have touched on AJAX slightly in my class, and it would probably be better than straight PHP if I decide to do all this on the back end. I guess their wait time with AJAX would be less than it would be with just PHP, but still dependent on their Internet connection speed. We'll see how it turns out. Thanks for your input.

Wouldn't it be more efficient to use javascript to sort it? Just loop though all the rows, get the column and do a sort. Would save bandwidth on both your side and the user side.
 
I think that's what he's intending to do, but you're wrong. It would only save bandwidth and time if the dataset was tiny (fewer than 100 records). Any more than that, and the javascript would get so slow and the initial load would be so long that it wouldn't make sense to do it that way.

Again, RDBMSes were built with exactly this purpose in mind. If your dataset is at all large, it will always be faster to sort/search at the databse level, even if the user is on a 56k connection. Whether or not you use AJAX or straight PHP/HTML is a matter of taste and/or style. Personally, I'm not a huge fan of AJAX, but it can be very useful if done right (gmail as an example).
 
Instead of having variable variables why don't you just stick everything in an associate array? That's what I usually do for database stuff where I'm not sure what fields I'll be getting from the query.
 
If you're looking to store column names along with it's data, you could use a hashmap. For each column name, Hash[column.name] = column.data. Then you could just search the hash for any available columns.
 
Back
Top