Current location: Hot Scripts Forums » Programming Languages » PHP » Dynamic Variables?


Dynamic Variables?

Reply
  #1 (permalink)  
Old 01-28-08, 06:25 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Spin Dynamic Variables?

Hey all,

Well I have tried to find a solution for the following all day long now, but don`t seem to get anywhere.

Following situation:
I have a function file, one function is responsible for getting data from a table. Simple you might say first, but the problem is, that if I want to get a whole bunch of rows, I will have a hard time generalizing that. Usually I call everything in a for loop directly in the code, but this time I want to have something like this:

1. The code calls the function with the SQL logic
2. The function executes the SQL command and saves one row per array
3. The function returns all arrays
4. The code takes care of further manipulation


Now here is the thing: Assume I call all rows from a table where gender == male. Of course that will be multiple. Somehow my function will have to save all this in an array row[1],row[2],row[3],...Now that not being enough, it will have to return all rows, so eg. return row;

Now the code has to go through all valid rows, for example

PHP Code:

for($i 0, isset($row $i), $i++) {

.
.
.

But I do not seem to figure out how to all this. Following shows where I currently stand:

PHP Code:

class datstr {


    
//Queries DB
    
function qryDb($logic$count$list) {
        if(!isset(
$logic) {
            return 
"ERROR: No logic transfered";
        }
        
        require(
"config.cfg.php");
        
mysql_connect($this->dbhost$this->dbuser$this->dbpass) or die("Could not connect to database");
        
mysql_select_db($this->db) or die("Could not find database table");
        
$result mysql_query($logic) or die("Error with Query logic:<br />" mysql_error());
        
        
/*
        //Requesting Count?
        if($count == 1) {
            $rows = mysql_num_rows($result);
            mysql_close();
            return array($result, $rows);
        } else {
            mysql_close();
            return $result;
        }
        */
        
        
foreach($row mysql_fetch_object($result)) {
            return array(
extract($resultEXTR_PREFIX_ALL'row'));
        }
        
    } 
Can someone help me with this?

Thanks a lot
Max
Reply With Quote
  #2 (permalink)  
Old 01-28-08, 06:29 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
I think Variable Variables is what you're looking for.

However, using arrays is often a better practice.
Reply With Quote
  #3 (permalink)  
Old 01-28-08, 07:22 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Hi max, do you mean that you want to return an array of rows?

PHP Code:

        $retvals = array(); //initialise array to hold data
        
foreach($row mysql_fetch_object($result)) {
            
//Add row array to $retvals array
            
$retvals[] = array(extract($resultEXTR_PREFIX_ALL'row'));
        }
        return 
$retvals
Something like this?
The problem with your initial code is that it was returning just the initial row, and not all rows

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #4 (permalink)  
Old 01-28-08, 07:42 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you Jay, this was exactly what I was looking for...





One more problem though, how do I merge two variables (hard to say), what I mean is actually editing a variable name with a dynamic extension:

PHP Code:

        for($i 0, isset($row $i), $i++) {

            echo 
$row $i;
        } 
esp. row 1 : $row . $i

If for example $row == "test" and $i == "1" I want to get $row1 == "test".


Hard to explain, someone gets what I mean?


Thanks for the wonderfull help, finally a good forum
Greetings,
Max
Reply With Quote
  #5 (permalink)  
Old 01-28-08, 07:48 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, it is a little vague um i think its going to be ${$row.$i}, although to be honest, nico was right in saying that you should use an array rather than dynamic variables, as they can get out of control. it would be better stored in something like
$row['test1']
Oh, and welcome to the forums. yes they are really useful. A lot of good coders on here

Jay
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #6 (permalink)  
Old 01-28-08, 07:48 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
EDIT: Jay beat me to it.

With variable variables (more info in my first post above - the link):

PHP Code:

echo ${'row' $i}; 

Reply With Quote
  #7 (permalink)  
Old 01-28-08, 08:05 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
And thanks again guys.

One more problem though:

PHP Code:

53:        for($i 0, isset(${$row $i}), $i++) {

54:            echo ${'row' $i};  
55:        } 
produces output:
Code:
Parse error: syntax error, unexpected ')', expecting ';' in C:\WAMP\www\scrimmfinder\query.php on line 53

How would I do this whole thing with an array? I am kind of scared of what that would actually mean. What I actually want to do here, is read out every row on its own, stored in one part of an array like returned from the function above.

Thanks again,
Max
Reply With Quote
  #8 (permalink)  
Old 01-28-08, 08:20 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
i think you want something more like this, although i cant stipulate enough how BAD it is to use variable variables
PHP Code:

foreach($row as $r)
{
    foreach(
$i as $j)
    {
        if(isset(${
$r.$j}))
        {
            echo ${
$r.$j};
        }
    }

__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #9 (permalink)  
Old 01-28-08, 08:22 AM
TenFormer's Avatar
TenFormer TenFormer is offline
Newbie Coder
 
Join Date: Jan 2007
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
ok so before I try this, how would you do something like that with arrays? I don't really see how to do that.
Reply With Quote
  #10 (permalink)  
Old 01-28-08, 08:24 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
To be able to demonstrate how best to use it for arrays, it might be best if you describe what it is that this loop is meant to do
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Generating dynamic variables n3wb!e PHP 2 09-26-07 06:40 AM
php benchmark: variables with casts vs variables without casts markg85 PHP 3 09-04-07 02:19 PM
dynamic link via multiple variables pkcidstudio PHP 6 04-19-07 08:15 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 03:27 PM
Dynamic image URLs in templates. aka "How to: Assign Variables ? Please Help" saymaad PHP 11 03-05-06 02:48 AM


All times are GMT -5. The time now is 07:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.