Jump to content

Dynamic Variables?


Nicholas

Recommended Posts

I it possible to have variables concatenated together?

For instance, in a loop you've got something like this:

 

$variable = "multipleSomethings";
for($num=1;$num<=10;$num++) {
$variable.$num = "multipleSomethings" . $num;
}

echo $variable1; // "multipleSomethings1"
echo $variable2; // "multipleSomethings2"

 

See what I'm saying?

 

Now try to understand my code. ;D

 

case 'pendCalendar':
// Get all the calendar events that haven't been accepted yet, and display them
print '<div class="para">' .
		'<h1>Pending Calendar Additions</h1>' .
		'The following are requests from coaches which have not yet been approved.<br />' .
		'Please check the information for accuracy, and then decide whether or not to accept the request.<br />' .
		'<strong>Be sure to check whether there are existing events that might cause a confliction.</strong><br /><br />' .
		'<form action="admin.php?action=updateCalendar" method="post">' .
		'<table cellpadding="0" cellspacing="0" style="border: 1px solid #666;" width="100%">' .
			'<tr>' .
				'<td style="background: #999; padding: 3px;" width="25%"><strong>Event Date:</strong></td>' .
				'<td style="background: #999; padding: 3px;" width="20%"><strong>Event Location:</strong></td>' .
				'<td style="background: #999; padding: 3px;" width="30%"><strong>Event Description:</strong></td>' .
				'<td style="background: #999; padding: 3px;" width="25%"><strong>Accept?</strong></td>' .
			'</tr>';

guestLogin($username,$password);
$query = mysql_query( "SELECT * FROM calendar07 WHERE accept='0' " );
$num = 1;
while($fetch = mysql_fetch_assoc($query)) {
	print '<input name="hidden_' . $num . '" type="hidden" value="' . $fetch['id'] . '>';
	print '<tr>' .
				'<td style="padding: 5px;" valign="top">' . $fetch['month'] . ' ' . $fetch['day'] . ': ' . $fetch['time'] . '</td>' .
				'<td style="padding: 5px;" valign="top">Field ' . $fetch['field'] . '</td>' .
				'<td style="padding: 5px;" valign="top"><em>Requested by: </em>' . $fetch['leader'] . '<br />' .
				'<em>Brief Summary: </em>' . $fetch['desc'] . '</td>' .
				'<td style="padding: 5px;" valign="top"><select name="event_' . $num . '">' .
					'<option value="accept">Accept Event</option>' .
					'<option value="deny">Refuse Event</option>' .
				'</select>' .
				'</td>' .
			'</tr>';
}

print '<tr>' .
		'<td colspan="4" style="text-align: right;"><input type="submit" value="Submit" /></td>' .
		'</tr>' .
		'</table>';
break;
case 'updateCalendar':
$num = 1;
do {
	if (isset($_POST["event.$num"])) {
		$accept.$num = $_POST["event.$num"];
		$id.$num = $_POST["hidden.$num"];
		$num++;
		$set = 'true';
	} else {
		$max = $num;
		$set = 'false';
	}
} while($set='true');

break;

 

Link to comment
https://forums.phpfreaks.com/topic/38070-dynamic-variables/
Share on other sites

Thanks for the quick response. And good eye on the =/== bit.

Technically, what I posted was exactly the area that you need to see. But I'll trim it down for clarity...

 

Better?

case 'pendCalendar':
// Get all the calendar events that haven't been accepted yet, and display them
...
<form action="admin.php?action=updateCalendar" method="post">
...
// Connect to the database etc...
// The fun part: looping through the results
$num = 1;
while($fetch = mysql_fetch_assoc($query)) {
	<input name="hidden_' . $num . '" type="hidden" value="' . $fetch['id'] . '> // Notice: hidden_$num will be dynamic
	<td style="padding: 5px;" valign="top">
		<select name="event_' . $num . '"> // Again, a dynamic name
			<option value="accept">Accept Event</option>
			<option value="deny">Refuse Event</option>
		</select>
}
break;
case 'updateCalendar':
$num = 1;
do {
	if (isset($_POST["event.$num"])) {
		$accept.$num = $_POST["event.$num"];
		$id.$num = $_POST["hidden.$num"];
		$num++;
		$set = 'true';
	} else {
		$max = $num;
		$set = 'false';
	}
} while($set='true');

break;

 

Link to comment
https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182221
Share on other sites

I think the guy is looking for variable variables....

 

http://uk.php.net/manual/en/language.variables.variable.php

 

and what he is trying to achive all lies within the first loop

 

so this should do the trick...

 

$var = "variable$num";
for($num=1;$num<=10;$num++) {
$$var = "multipleSomethings" . $num;
}

 

you will have to adjust the code after that to reflect the variable names accordingly.

 

Link to comment
https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182241
Share on other sites

Well it seems there are errors in the code.

After enabling all errors, and the having them displayed...

 

Notice: Undefined variable: accept in ... on line 100

 

error_reporting(E_ALL);
ini_set('display_errors','On');
$num = 1;
do {
if (isset($_POST["event.$num"])) {
	$accept.$num = $_POST["event.$num"];
	$id.$num = $_POST["hidden.$num"];
	$num++;
	$set = true;
} else {
	$max = $num;
	$set = false;
}
} while($set);

guestLogin($username,$password);
for($num = 1;$num <= $max; $num++) {
if ($accept.$num == 'accept') { // Line 100
	$query = "UPDATE calendar07 SET accept = '1' WHERE id='" . $id.$num . "'";
	$result = mysql_query($query);
	if ($result) {
		print 'Calendar updated for event id ' . $id.$num . '. The event is ' . $accept.$num . 'ed.';
	}
}
}			

Link to comment
https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182251
Share on other sites

First I'll need to understand variable variables...

The example on that page seems as if you could achieve the same thing with .=  .

 

Can someone explain further these lines:

 

At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

<?php
echo "$a ${$a}";
?>

produces the exact same output as:

<?php
echo "$a $hello";
?>

 

i.e. they both produce: hello world.

 

-------------------------------------------------------

I don't want seperate variables... Would echoing $a give "hello world"?

Link to comment
https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182262
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.