homepage Welcome to WebmasterWorld Guest from 54.234.60.133
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Variable variables
Sub_Seven




msg:4445001
 4:54 am on Apr 25, 2012 (gmt 0)

Hello all, its been a while,

I'm creating vars dynamically here


$cn = 0;
while($row = mysql_fetch_array($result)){
{${catName.$cn} = $row['cp_cat_name'];}
$cn++;
}


So when I call them like so $catName0, $catName1 etc they work fine as variables.

The problem I'm having is trying to join them and call them dynamically as well (those are the best words I can find, I better try to explain below):


$cn = 0;
while($row = mysql_fetch_array($result)){
{${catName.$cn} = $row['cp_cat_name'];}
echo $catName.$cn
$cn++;
}


See, in the block of code above I'm trying that for as long as there are results I want to echo the name of both vars joined together

echo $catName.$cn

That would produce something like:

echo $catName1++ and so on, but it is just not working, all I get is the last var echoed ($cn) so I only get a number.

Can some one please tell me how do I join 2 vars in order to echo it as one?

Thanks for any help provided :)

 

cffrost2




msg:4445214
 12:04 pm on Apr 25, 2012 (gmt 0)

Hi. Your echo needs to be inside the WHILE loop. Your set variable resides just outside the curly brackets so it's only getting the last record from your query.


$cn = 0;
while($row = mysql_fetch_array($result)){
{${catName.$cn} = $row['cp_cat_name'];
echo $catName.$cn.' \n'; //maybe create a new line for each one
$cn++; } // this is where the while should end
}

remember, WHILE is as it sounds. "while"(condition is true){ execute this inside these curly brackets }
Hope this helps

Sub_Seven




msg:4445325
 2:52 pm on Apr 25, 2012 (gmt 0)

Hello cffrost2

My echo IS inside the while loop, on your code you ommited a curly bracket but it is fine on mine.

In my code whithin the while loop I am:

Generating the variable variable
Trying to echo the variable variable
Incrementing $cn with $cn++

cffrost2




msg:4445377
 3:57 pm on Apr 25, 2012 (gmt 0)

oops. I stand corrected. I'm sorry. I interpreted the {${catName as the first curly on accident.
Since you are building your variables, you have to echo them the same way.
Try this

$cn = 0;
while($row = mysql_fetch_array($result)){
{${'catName'.$cn} = $row['cp_cat_name'];}
echo ${'catName'.$cn};//use have to use the same literal syntax. I also added the '' quotes around catName. Didn't know if you left those out or if you were using defined variables. Omit them if needed
$cn++;
}

Also, I'm not sure of the need for the first set of curlys inside the while loop.

{${catName.$cn} = $row['cp_cat_name'];}

But it works with or without that set of curlys.
Sorry again for my initial miss. But this should get you going this time :)

rocknbil




msg:4445393
 4:12 pm on Apr 25, 2012 (gmt 0)

The inner brackets are for resolving ambiguity [php.net] so that PHP better understands what you're trying to do.

Do you have warnings and error reporting enabled? Because what you have there

{${catName.$cn}

is normally how you use a constant (it's unquoted.) Maybe this is an example of the curlies clarifying abmiguity in that it's assuming you mean the string 'catName'. If you've ever accidentally echo'ed an uqoted literal, you may have seen errors like "undefined constant BLAH at line 123, assuming 'BLAH'."

I'd try this (no guarantee it will work. :-) )

$cn = 0;
while($row = mysql_fetch_array($result)){
$vname = "catName$cn"; // or $vname = 'catName'.$cn
{${$vname} = $row['cp_cat_name'];}
echo $vname . ' ' . ${$vname} . '<br>';
$cn++;
}

Now for the big (rhetoric, probably doesn't need an answer) question: why you'd need to move into this complex method of storing arrays, obviously to be used later after your while loop - there is very likely an easier way to do whatever you're doing right inside your while loop. :-)

[edited by: rocknbil at 4:14 pm (utc) on Apr 25, 2012]

Sub_Seven




msg:4445394
 4:13 pm on Apr 25, 2012 (gmt 0)

Works perfect!

I guess the whole concept of variable variables and their manipulation is still not clear for me, this is like the second time I've needed to create vars like this.

Also, I'm not sure of the need for the first set of curlys inside the while loop.

You're right, not needed...

Thanks so much for the help, greatly appreciated :)

Sub_Seven




msg:4445417
 4:36 pm on Apr 25, 2012 (gmt 0)

Hey rocknbil

Didn't see you had replied when I answered.

I'm open to suggestions and I'd like to see that easier method, I can tell you I do this in order to accomplish what I need in the least amount of code possible, right know I'm working on a file that has gone over 1,000 lines without counting included files.

Let me show you the whole block of code, which by the way is giving me another problem that I will explain afterwards, maybe you can help with that as well

$cn = 0;
$result = mysql_query("SELECT * FROM cp_l1 WHERE cat_group = '".$cleanCategory."'") or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result)){
${catName.$cn} = $row['cp_cat_name'];
echo ${'catName'.$cn};
$result= mysql_query(" SELECT cp_l3.l3_prod_name, cp_l3.l3_active, cp_l2.l2_cp_cat_name, cp_l2.l2_prod_01_name, cp_l2.l2_prod_01_code
FROM `cp_l3`
INNER JOIN cp_l2 ON cp_l3.l3_cat_name=cp_l2.l2_cp_cat_name
WHERE `l3_cat_name` = '".${'catName'.$cn}."' LIMIT 1 ") or die ('Error: '.mysql_error ());
// Call the orpX code block
include($_SERVER['DOCUMENT_ROOT']."/includes/orp".$cn.".php");
$cn++;
}


And the latest problem: Making that mysql_query inside the while loop is stopping the loop from running again, why would that happen?

Is there a way to properly indent code in here somehow?

rocknbil




msg:4446086
 4:46 pm on Apr 26, 2012 (gmt 0)

Heh. You're overwriting the $result object. :-)


$result = mysql_query(...
while($row = mysql_fetch_array($result)){...
$result= mysql_query(...
// $result has now CHANGED so while says "nope,
//no more rows to fetch on $result"


Change the inner one to something like $result2.

Is that the entire use for $catName.$cn? It's not used after this? Why can't you do this? Simplified it so you can see the logic.


$cn = 0;
$result = mysql_query($first_query) or die ('Error: '.mysql_error ());
while($row = mysql_fetch_array($result)){
echo $row['cp_cat_name'] . '<br>';
$inner_query = 'SELECT cp_l3.l3_prod_name, cp_l3.l3_active, cp_l2.l2_cp_cat_name,
cp_l2.l2_prod_01_name, cp_l2.l2_prod_01_code FROM `cp_l3` INNER JOIN cp_l2 ON
cp_l3.l3_cat_name=cp_l2.l2_cp_cat_name WHERE `l3_cat_name` = \'' .
$row['cp_cat_name'] . '\' LIMIT 1';
$result2= mysql_query($inner_query) or die ('Error: '.mysql_error ());
// Do something with $result2 object
include($_SERVER['DOCUMENT_ROOT']."/includes/orp$cn.php"); // Note concat. not needed, double quoted
$cn++;
}


That should give you the exact same thing as long as you don't mess with the $result object or $row array from the outer loop. No counter (except for that include, and I'll bet there's a better way to do that too), no variable variables, no hoops or fire rings. :-) If the inner query does something, make sure you store the results in $row2 or some other variable.

If you really want a tailspin . . . this entire block can be done in a single query, no inner query required. Just don't have to time to work through it ATM.

Sub_Seven




msg:4446732
 12:29 am on Apr 28, 2012 (gmt 0)

Hello there,

You (as I have said n number of times) were right, I was overwriting the $result object, the weird thing is that even changing $result to something else like $result2 the script stops on the first run and it works fine as long as I remove the $_SERVER['DOCUMENT_ROOT']. part of my include... OH WELL.

I have taken the decision to start from scratch, in the mean while I will start creating long blocks of code to create what I wanted to do in the original (smaller) block of code, I need to rethink the whole thing even the way the DB is structured.

Thanks anyways for the help :)

rocknbil




msg:4446910
 3:41 pm on Apr 28, 2012 (gmt 0)

probably something here


include($_SERVER['DOCUMENT_ROOT']."/includes/orp$cn.php")

that's stopping it.

if (is_file($_SERVER['DOCUMENT_ROOT']."/includes/orp$cn.php")) {
include($_SERVER['DOCUMENT_ROOT']."/includes/orp$cn.php");
}
else { die("include($_SERVER['DOCUMENT_ROOT']."/includes/orp$cn.php does not exist"); }

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved