Jump to content


Photo

variable inside a variable


  • Please log in to reply
11 replies to this topic

#1 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 12 July 2006 - 10:48 PM

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.

//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>";
			}


#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 10:57 PM

If you mean create a variable called $n_1, $n_2 etc then:
$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>";
}

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:
$$nvalue = $nvalue;
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 12 July 2006 - 11:11 PM

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?


		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'];
       }


Thanks Again

#4 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 12 July 2006 - 11:33 PM

I imagine that's as efficient as you can get.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 12 July 2006 - 11:39 PM

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


#6 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 13 July 2006 - 12:07 AM

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

#7 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 13 July 2006 - 12:11 AM

One last one for today!

I am trying to get this to much simpler 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]'
	";

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


// Query
			for($i = 1; $i <= $num_players; $i++) {
$sql = "UPDATE fantasynascar SET p_" . $i . "='$_POST[p_" . $i . "]'
	WHERE fantasy_id = '$_POST[fantasy_id]'
	";


#8 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 13 July 2006 - 12:20 AM

try this and see what happens

// Query
for($i = 1; $i <= $num_players; $i++) {
	$sql = "UPDATE fantasynascar SET p_" . $i . "= ".$_POST[."p_" . $i . ]."
	          WHERE fantasy_id = ".$_POST[fantasy_id]."";
}


#9 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 13 July 2006 - 12:28 AM

Parse error: syntax error, unexpected '.', expecting ']'


Is the error message I get with that snipet.  I have been getting that with others I try too. 

#10 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 13 July 2006 - 12:45 AM

// 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";
}

or

// 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";
}


#11 komquat

komquat
  • Members
  • PipPipPip
  • Advanced Member
  • 130 posts
  • LocationHoughton, MI

Posted 13 July 2006 - 12:54 AM

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

#12 cmgmyr

cmgmyr
  • Members
  • PipPipPip
  • Advanced Member
  • 1,278 posts
  • LocationUSA

Posted 13 July 2006 - 12:58 AM

glad we could help you out!

-Chris




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users