Nicholas Posted February 11, 2007 Share Posted February 11, 2007 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. 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; Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/ Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182220 Share on other sites More sharing options...
Nicholas Posted February 11, 2007 Author Share Posted February 11, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182221 Share on other sites More sharing options...
trq Posted February 11, 2007 Share Posted February 11, 2007 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); Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182223 Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 You still haven't told us what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182225 Share on other sites More sharing options...
Nicholas Posted February 11, 2007 Author Share Posted February 11, 2007 jesirose: If there aren't any apparent code errors, I guess there isn't a problem. I just assumed that this wouldn't work as I coded it as pseudo-code, not expecting it to actually be correct. Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182228 Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 Well we posted some errors you had. The easiest way to find out if it works is to try it. Then when it doesn't, you address the errors you get. You could have done that in less time then it took to post and wait for us to reply Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182232 Share on other sites More sharing options...
ToonMariner Posted February 11, 2007 Share Posted February 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182241 Share on other sites More sharing options...
Nicholas Posted February 11, 2007 Author Share Posted February 11, 2007 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.'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182251 Share on other sites More sharing options...
Jessica Posted February 11, 2007 Share Posted February 11, 2007 Can you tell us which line is 100? Try doing it the way Toon posted. Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182255 Share on other sites More sharing options...
Nicholas Posted February 11, 2007 Author Share Posted February 11, 2007 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"? Quote Link to comment https://forums.phpfreaks.com/topic/38070-dynamic-variables/#findComment-182262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.