Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
William LaMorie
11,769 PointsIs there a way to pass code into a function to reference a variable that does not yet exist?
I've got an offbeat question, that isn't really unique to PHP, but I've got the problem in PHP, so I'm asking under the auspices of PHP! =)
I'm doing some work for a friend, and the site will require a lot of different tables from SQL with a lot of different ways of viewing them. I've been working on making a single function wrapper that will format most of those tables and spit them out all nice and shinny for me. It's going pretty well so far, but one of the things I would really like to do is embed columns of PHP driven actions into my SQL table, and I'd like to be able to determine what they do on the fly, I've got part of that sorted out, but I don't know if there's a way to do the rest of it.
Right now, I'm passing the details of the new column into my function as such with an array:
array("title" => "header", "insert" => $insertAfter, "value" =>$value)
and that all works fine. What doesn't, and what I would like to do, is for example, embed a button into each row that references the primary key for that row, and does something (like open an edit page, a delete confirmation page, a clone ect...). While I could likely hard code values for my $value that would accomplish this, I'd like to be able to just reference the variable for the field of interest inside the function.
so for example, I'm pulling my data from the MySQL db as such:
while($row = mysqli_fetch_row($result))
and then the columns as such:
for($col = 0; $col < $columns; $col++)
// where columns comes from $columns = mysqli_num_fields($result)
and I'd like to be able to make a column that in essence does something like:
<a href=somepage.php?var=row[col]> .... (not actually going to be using a GET for this, but that's beside the point)
so... I'd like to be able to pass a "value" for the cell into the function of something like
"value" = <a href=somepage.php?var=$row[$col]>....
but this is, of course, giving me an error because $row doesn't exist in this scope. Making it global would be silly, very very silly and bad and would explode in my face. I don't want to pass $row in, I want to generate the said link on the fly for each record...
Does anyone have any idea of what any workaround would be? Or should I just hard code the cases I think I would need into my function?
Thanks! =) -Will
2 Answers
Andrew Shook
31,709 PointsCouldn't you just pass $row["primary_key"] into the function? I've never used mysqli, but I assume that you can have it fetch rows as an array. So, couldn't you just pass the primary key for that row into the function instead of the whole $row variable?
William LaMorie
11,769 PointsNo, because $row is not defined outside the scope of the function, so $row['index'] also isn't, so it's going to chuck me an undefined variable error there too.
Just to attempt to redefine what I'm trying to do differently, because I think it's a bit confusing (and I'm sure there's a way around it, it might be totally different then what I'm trying to do, and that'd be great).
I've wrote some code to get various tables from a mysql database, a function. I'm trying to expand the functionality of said function (ha), so that in addition to showing some/all rows and columns and putting classes on things and all sorts of good things like that, I'd like to be able to pass an argument that gives it additional ways to use the mysql information while the connection is open. This requires using an array that only exist within the scope of the function, namely $row.
I can think of some ways I could do this:
Make a mini-language, which constructs into php on the inside of the function.
Have some preset options for things I'd like to do with columns from the table.
Regex it on after.
But those all have their own issues.
The first is just silly. That's a lot of code overhead, and should really be expanded out into a class, and that seems over loaded for what I want to do.
The second would work, but is limiting. It'd be nice to have one small suite of options so I could do any server-side modifications to a sql record, so like if I could pass in something like math functions as well as just accessors.
The 3rd wouldn't work with the way I want to use the info from the mysql db. For example, I would like to hide most of the key values most the times in most of the time just by not giving them to the html, as opposed to not selecting them with the sql query. I'm doing that totally because I would like to be able to access the key as the reference for the record when I pass it off to be modified or deleted or cloned... so I can't regex it on to the html string after with out doing a lot more calls to the database, and I'd rather avoid that.
So those limitations lead me back to wanting something where I can just pass an arg to the function that has code in it. I've managed to push them though as string literals, but I can't find a way to turn them into variables on the other side of the function (so the column just displays a whole bunch of $row[1]'s or what have in that case.
Likely this whole thing is an absurd exercise, and I wouldn't be surprised if there were a library out there that did everything I wanted... but if so I haven't been able to find it, and would LOVE any good suggestions for fixing my issue that way.
But... I'd also like to know if there is a way to push an undefined variable though the function args with out tossing an error, be it some fun reference/dereference-fu I don't know or making a string literal that gets converted with some string-fu I don't know on the inside, or if it's some fun magic with exception handling I don't know.
Christian Andersson
8,712 PointsChristian Andersson
8,712 PointsI'm a bit confused about what you are trying to do and why. You have elaborated your problem quite well, but I still don't follow. Maybe it's because it's nearly 5am here =P
But do you think you could give a better example? I think I'd need something more... tangible to understand your problem. A visual reference or a demo code or something of that nature would be sweet. =)