Jump to content

variable inside a variable


komquat

Recommended Posts

I am trying to run a loop for information pulled from a table.

From the following code you can see what I mean.  In the for loop at the bottom, $nvalue currently returns the correct value, I just need it as a variable, example it returns n_1, but I need it to return $n_1.

Help is greatly appreciated.

Thanks in advance.

[code]
//Collect the # of players playing
$sql_num_players = "SELECT fantasy_nascar, first_name, user_tag
FROM users
WHERE fantasy_nascar = '1'
";
$qry_num_players = mysql_query($sql_num_players, $connection) or die ("Could not get number of players");

$num_players = mysql_num_rows($qry_num_players);


// Query for players from nascar table
$sql_current = "SELECT *
from fantasynascar
WHERE Week = '$_POST[Week]'
";

$qry_edit = mysql_query($sql_current, $connection) or die ("Could not Execute query.");

//Check number results
$num = mysql_num_rows($qry_edit);

if ($num < 1) {
//No results, print message
$display_block = "Sorry, no results returned";
} else {
//if results are found, get data with while

while ($row = mysql_fetch_array($qry_edit)) {
$fantasy_id = $row['fantasy_id'];
$n_1 = $row['n_1'];
$d_1 = $row['d_1'];
$p_1 = $row['p_1'];
$n_2 = $row['n_2'];
$d_2 = $row['d_2'];
$p_2 = $row['p_2'];
$n_3 = $row['n_3'];
$d_3 = $row['d_3'];
$p_3 = $row['p_3'];
$n_4 = $row['n_4'];
$d_4 = $row['d_4'];
$p_4 = $row['p_4'];
$n_5 = $row['n_5'];
$d_5 = $row['d_5'];
$p_5 = $row['p_5'];
$n_6 = $row['n_6'];
$d_6 = $row['d_6'];
$p_6 = $row['p_6'];
$n_7 = $row['n_7'];
$d_7 = $row['d_7'];
$p_7 = $row['p_7'];
$n_8 = $row['n_8'];
$d_8 = $row['d_8'];
$p_8 = $row['p_8'];
$n_9 = $row['n_9'];
$d_9 = $row['d_9'];
$p_9 = $row['p_9'];
$n_10 = $row['n_10'];
$d_10 = $row['d_10'];
$p_10 = $row['p_10'];
$n_11 = $row['n_11'];
$d_11 = $row['d_11'];
$p_11 = $row['p_11'];
$n_12 = $row['n_12'];
$d_12 = $row['d_12'];
$p_12 = $row['p_12'];
$n_13 = $row['n_13'];
$d_13 = $row['d_13'];
$p_13 = $row['p_13'];
$n_14 = $row['n_14'];
$d_14 = $row['d_14'];
$p_14 = $row['p_14'];
$n_15 = $row['n_15'];
$d_15 = $row['d_15'];
$p_15 = $row['p_15'];
$n_16 = $row['n_16'];
$d_16 = $row['d_16'];
$p_16 = $row['p_16'];
$n_17 = $row['n_17'];
$d_17 = $row['d_17'];
$p_17 = $row['p_17'];
$n_18 = $row['n_18'];
$d_18 = $row['d_18'];
$p_18 = $row['p_18'];
$n_19 = $row['n_19'];
$d_19 = $row['d_19'];
$p_19 = $row['p_19'];
      }


//create display table
$display_block = "
<form method='post' action='race_results_final.php'>
<input type='hidden' name='fantasy_id' value='$fantasy_id' >
                <input type='hidden' name='Week' value='$Week'>
                <p>$Week</p><br>
<p>
<table border='1'><tr><th>Name</th><th>Driver</th><th>Points</th></tr>";
        //Player 1
for($i = 1; $i <= $num_players; $i++) {
$nvalue = "n_";
$nvalue .= $i;
        $display_block .= "<tr><td>$nvalue</td><td>$d_$i</td><td>
                    <input type='hidden' name='n_$i' value='$n_$i'>
<input type='hidden' name='d_$i' value='$d_$i'>
<input type='text' name='p_$i' value='$p_$i' size=5></td></tr>";
}
[/code]
Link to comment
Share on other sites

If you mean create a variable called $n_1, $n_2 etc then:
[code]
$display_block = "
<form method='post' action='race_results_final.php'>
<input type='hidden' name='fantasy_id' value='$fantasy_id' >
                <input type='hidden' name='Week' value='$Week'>
                <p>$Week</p><br>
<p>
<table border='1'><tr><th>Name</th><th>Driver</th><th>Points</th></tr>";
        //Player 1
for($i = 1; $i <= $num_players; $i++) {
$nvalue = "n_";
$nvalue .= $i;
$$nvalue = $nvalue;
        $display_block .= "<tr><td>$nvalue</td><td>$d_$i</td><td>
                    <input type='hidden' name='n_$i' value='$n_$i'>
<input type='hidden' name='d_$i' value='$d_$i'>
<input type='text' name='p_$i' value='$p_$i' size=5></td></tr>";
}[/code]

There will no be a variable created $n_1,$n_2,$n_3 etc. with the values: 'n_1','n_2','n_3' etc.

The modification is this:
[b]$$nvalue = $nvalue;[/b]
Link to comment
Share on other sites

Thank you, the modification was actually $nvalue = $$nvalue, the opposite of what you had written.

Is there any way to shorten up the while loop?

[code]

while ($row = mysql_fetch_array($qry_edit)) {
$fantasy_id = $row['fantasy_id'];
$n_1 = $row['n_1'];
$d_1 = $row['d_1'];
$p_1 = $row['p_1'];
$n_2 = $row['n_2'];
$d_2 = $row['d_2'];
$p_2 = $row['p_2'];
$n_3 = $row['n_3'];
$d_3 = $row['d_3'];
$p_3 = $row['p_3'];
$n_4 = $row['n_4'];
$d_4 = $row['d_4'];
$p_4 = $row['p_4'];
$n_5 = $row['n_5'];
$d_5 = $row['d_5'];
$p_5 = $row['p_5'];
$n_6 = $row['n_6'];
$d_6 = $row['d_6'];
$p_6 = $row['p_6'];
$n_7 = $row['n_7'];
$d_7 = $row['d_7'];
$p_7 = $row['p_7'];
$n_8 = $row['n_8'];
$d_8 = $row['d_8'];
$p_8 = $row['p_8'];
$n_9 = $row['n_9'];
$d_9 = $row['d_9'];
$p_9 = $row['p_9'];
$n_10 = $row['n_10'];
$d_10 = $row['d_10'];
$p_10 = $row['p_10'];
$n_11 = $row['n_11'];
$d_11 = $row['d_11'];
$p_11 = $row['p_11'];
$n_12 = $row['n_12'];
$d_12 = $row['d_12'];
$p_12 = $row['p_12'];
$n_13 = $row['n_13'];
$d_13 = $row['d_13'];
$p_13 = $row['p_13'];
$n_14 = $row['n_14'];
$d_14 = $row['d_14'];
$p_14 = $row['p_14'];
$n_15 = $row['n_15'];
$d_15 = $row['d_15'];
$p_15 = $row['p_15'];
$n_16 = $row['n_16'];
$d_16 = $row['d_16'];
$p_16 = $row['p_16'];
$n_17 = $row['n_17'];
$d_17 = $row['d_17'];
$p_17 = $row['p_17'];
$n_18 = $row['n_18'];
$d_18 = $row['d_18'];
$p_18 = $row['p_18'];
$n_19 = $row['n_19'];
$d_19 = $row['d_19'];
$p_19 = $row['p_19'];
      }

[/code]

Thanks Again
Link to comment
Share on other sites

[code]Try this instead:
[code]<?php
$x = array('n','d','p');
while ($row = mysql_fetch_array($qry_edit)) {
      $fantasy_id = $row['fantasy_id'];
      for($i=1;$i<20;+)
          foreach($x as $v)
            ${$v . '_' . $i} = $row[$v .'_' . $i];
}?>[/code]

This uses variable variables.

Ken
[/code]
Link to comment
Share on other sites

WOW!!  You guys are simply amazing!!

I have been writing 1 line of code for each entry forever, and I was thinking yesterday, there has to be a better way.  Apparently there is.

I have many more of these in my code, so if I get stuck, I will be back.

Thanks again to all
Link to comment
Share on other sites

One last one for today!

I am trying to get this to much simpler code:

[code]
$sql = "
UPDATE fantasynascar SET
p_1 = '$_POST[p_1]',
p_2 = '$_POST[p_2]',
p_3 = '$_POST[p_3]',
p_4 = '$_POST[p_4]',
p_5 = '$_POST[p_5]',
p_6 = '$_POST[p_6]',
p_7 = '$_POST[p_7]',
p_8 = '$_POST[p_8]',
p_9 = '$_POST[p_9]',
p_10 = '$_POST[p_10]',
p_11 = '$_POST[p_11]',
p_12 = '$_POST[p_12]',
p_13 = '$_POST[p_13]',
p_14 = '$_POST[p_14]',
p_15 = '$_POST[p_15]',
p_16 = '$_POST[p_16]',
p_17 = '$_POST[p_17]',
p_18 = '$_POST[p_18]',
p_19 = '$_POST[p_19]'
        WHERE fantasy_id = '$_POST[fantasy_id]'
";
[/code]

I have tried this, but I am not getting it to work.  Am I close?


[code]
// Query
for($i = 1; $i <= $num_players; $i++) {
$sql = "UPDATE fantasynascar SET p_" . $i . "='$_POST[p_" . $i . "]'
WHERE fantasy_id = '$_POST[fantasy_id]'
";
[/code]
Link to comment
Share on other sites

try this and see what happens

[code=php:0]// Query
for($i = 1; $i <= $num_players; $i++) {
$sql = "UPDATE fantasynascar SET p_" . $i . "= ".$_POST[."p_" . $i . ]."
          WHERE fantasy_id = ".$_POST[fantasy_id]."";
}[/code]
Link to comment
Share on other sites

[code=php:0]// Query
for($i = 1; $i <= $num_players; $i++) {

$to_update = $_POST['p_'.$i];
$fid = $_POST['fantasy_id'];

$sql = "UPDATE fantasynascar SET p_".$i." = $to_update
          WHERE fantasy_id = $fid";
}[/code]

or

[code=php:0]// Query
for($i = 1; $i <= $num_players; $i++) {

$to_update = $_POST['p_'.$i];
$fid = $_POST['fantasy_id'];

$sql = "UPDATE fantasynascar SET p_$i = $to_update
          WHERE fantasy_id = $fid";
}[/code]
Link to comment
Share on other sites

With that new code, it updates only the last one.  I move the query in the '}' and it all works great. 

I have a few more but will try to work them out myself.  If I get stuck I will be back.

Thanks for all you help, you guys are great.  I recommend this site often because of the great help I get here.

Thanks again

Andy
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.