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
Share on other sites

Yes, it's possible.

No, I won't try to sort through your code if you didn't post a specific problem, error, or issue.

I do see one immediate error.

while($set='true'); will always be true. = is assignment, == or === is comparison.

Link to comment
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
Share on other sites

Yes its possible, but I don't quite see the relevance with your code.

 

Also, note that booleans are not strings. eg;

 

$set = 'true';

 

should be...

 

$set = true;

 

And this....

 

} while($set='true');

 

will cause an infinite loop. Should be....

 

} while($set);

Link to comment
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
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
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
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.