jeddi 02-08-2010, 05:18 PM Hi,
I want to create a variable name based on a counter which increases.
After reading the brief note in the Manual, I still can't grasp it !
I am trying to make $N_earn1 = $earn and $N_earn2 = $earn etc. etc.
This is my code:
$sql = "SELECT earn, comm, FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
$result = mysql_query($sql) or die("could not execute SELECT cb_main". mysql_error());
$num = mysql_num_rows($result);
if ($num == 0 ) {
$earn1 =0;
}
else {
$ctr = 1;
while( $row = mysql_fetch_assoc($result) ) {
extract($row);
$var = 'N_earn'.$ctr;
$$var = $earn;
$ctr++;
}
}
Is this the correct way to use it ?
When ctr = 1 is:
$$var = $earn; the equivelent of $N_earn1 = $earn; ?
.
mlseim 02-08-2010, 08:26 PM That looks correct to me.
Put an echo in to test it ...
while( $row = mysql_fetch_assoc($result) ) {
extract($row);
$var = 'N_earn'.$ctr;
$$var = $earn;
echo $N_earn1;
exit;
$ctr++;
}
MattF 02-08-2010, 09:03 PM Edited: One day I really will learn to read the questions instead of just scanning the code.
kbluhm 02-08-2010, 09:12 PM These lines:
$var = 'N_earn'.$ctr;
$$var = $earn;
Can also be written like so:
${ 'N_earn' . $ctr } = $earn;
Saving you having to assign a dummy variable.
I'd also recommend looking into using an array as opposed to variable variables. It'll be easier to track what you've assigned:
$N_earn = array();
/* ... */
$ctr = 1;
while( $row = mysql_fetch_assoc( $result ) )
{
$N_earn[ $ctr++ ] = $row['earn'];
}
jeddi 02-09-2010, 06:34 AM Thanks for your replies.
Actually I am trying to create an array of data with this code
any way, and I have the feeling that the $row array may already be what I want !
This is what I want to replace:
$earn = array(130,310,410,310,340,400);
$comm = array(310,652,410,310,76,40,361,256);
So instead of the that static data, I want to pick up the data
"earn" and "comm" from the table.
Maybe I do not need to extract the $row ?
nor use the variable variables ?
Something like this:
$earn = array();
$comm = array();
$sql = "SELECT earn, comm, FROM main WHERE id = '$Db_prod' ORDER BY mday_no DESC ";
$result = mysql_query($sql) or die("could not execute SELECT cb_main". mysql_error());
$num = mysql_num_rows($result);
if ($num == 0 ) {
$earn[] = 0;
$comm[] = 0;
}
else {
while( $row = mysql_fetch_assoc($result) ) {
$earn[] = $row['earn'];
$comm[] = $row['comm'];
}
}
If I can do it this way, then I don't need the counter
or those variable variables ?
Is this the better way to go ?
I have tried this and I am getting a problem with it:
When I display the resulting array with this:
print_r($earn);
I get:
Array ( [0] => Array ( ) [1] => 34.01 [2] => 33.36 [3] => 33.36 [4] => 82.25 [5] => 82.25 )
So I seem to have created a double array somehow :(
And hence max(earn); just returns "Array" rather than the
maximum value.
What am I doing wrong ?
.
Dormilich 02-09-2010, 11:46 AM Actually I am trying to create an array of data with this code any way, and I have the feeling that the $row array may already be what I want !
I doubt that, $row holds only the values from 1 DB row (that is, 4 members).
This is what I want to replace:
$earn = array(130,310,410,310,340,400);
$comm = array(310,652,410,310,76,40,361,256);
So instead of the that static data, I want to pick up the data
"earn" and "comm" from the table.
Maybe I do not need to extract the $row ?
unfortunately, you have (with this code) no other way of getting the data, since $result (which holds all data) is not an array.
jeddi 02-09-2010, 12:04 PM Hi Dormilich,
Maybe you did not see the while loop ???
I walk through the $row[] with it.
.
Dormilich 02-09-2010, 12:08 PM Maybe you did not see the while loop ???
I’ve seen that, but I thought you meant something different.
I walk through the $row[] with it.
nope. you walk through $result with that.
jeddi 02-09-2010, 03:50 PM nope. you walk through $result with that.
yer, OK
But the point is ... do you see whats wrong with this?
To me it looks like it should work - why am I getting this "double array"
IT OK NOW, I found my the error - it works OK now :)
Thanks
.
MattF 02-09-2010, 04:05 PM You're sure you've posted all of the relevant code from that script? I'll personally be damned if I can tell why it's multi-dimensional from looking at that code above.
jeddi 02-09-2010, 08:49 PM Hi Matt,
Just to ease your mind ...
I better confess, I had made a typo because I didn't
copy and paste the code like I usually do.
The top two array statements were:
$earn = array[];
$comm = array[];
square brackets instead of rounded.
so that was what was causing me grief !
But I had some other problems which you guys helped sort out,
so it wasn't wasted effort
and thanks again. :thumbsup:
.
|
|