PHP - Simple Dynamic Variable Example
<?php
$foo = 5; $too = 7; ${"foo"}++; ${"too"}++; echo $foo; echo '<br />' . $too; $my_var_name = "foo"; ${$my_var_name}++; echo $foo; ?> I understand the part that $my_var_name is going to increment $foo. But I don't understand that when we used ${"foo"} we did not add the $ sign with it. But when we use ${$my_var_name} we have to add another $ sign. why is that? Similar TutorialsHi there, I'm a movie freak and I'm planning of building a simple dynamic movie list. There are some movie lister software available on internet, but I want it lighter and customized. Here is the idea: It looks like yahoo mail inbox...I want to create this script based on php and sql. I think this is the right place where can I get some help. Thanks in advance. NaimeniM hi guys I just could not find the answer on forum. I have a url like this http://www.example.com/deal.php?id=367 and my question is : how can i use curl function to get the html and the id value which is 367 thanks I have a form that inserts information into a mysql, the only prolem is I need to add different surrounding html to the information when entering it into the database depending what the value is...(essentially I want to store font color around different levels) How would I define the same variable with different information depending on the value? The information is called using: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $editLEVEL=$_POST['level']; I want it so if 'Level1' is the posted value then $editLEVEL would be '<color=1>$editLEVEL</color>' but if 'Level2' is the posted value the $editLEVEL would be '<color=2>$editLEVEL</color>' If you dont understand just let me know I'll try explain best I can. OK, what I am trying to do is generate a dynamic variable name. I want to setup a foreach statement that will generate variable names based on an array of data. $numbers = array('one', 'two', 'three', 'four', 'five', 'six', 'seven'); foreach ($numbers as $number) { } What I need, is for a bunch of variables to be generated and filled with content. $variable_one_label = "Blah"; $variable_two_label = "Blah Again"; How can I generate the dynamic variable based on the array value? Hi ! I'd like to count all occurences in "$thiscodestring" into a "dynamic?" array and print it out after the end of the loop, how do I assign the variablenames dynamically ? $query = "SELECT codes, status FROM table"; while ($row = mysql_fetch_assoc($result)) { $thiscodestring = $row[codes]; // looks like "a1 c2 d3 10 15 1 a1 a1"; $singlecodes = explode(" ", $thiscodestring); foreach($singlecodes as $thiscode) { $$thiscode[count] = $$thiscode[count] + 1; } } After all Mysql-Stuff is done I'd like to print out the counted codes, for example the code "a1" was found 100 times in all querys then $$a1[count] should be 100, c2 was found 15 times, then $$c2[count] should be 15 and so on. Question: How do I tell php to assign a "dynamic" variable with the name of the found string which was exploded (in this example a1, c2, d3... ) before ?! $$ does not seem to work. And how can I then print_r() the value if I dont know the name ? Thanks ! Hello. I want to create some variables dynamically. I have this so far: for($i = 1; $i <= 4 ; $i++){ $a = 'member' . $i; $$a = array(); } Does that give me this 4 arrays?: $member1, $member2, $member3 and $member4? Of course I want some process inside of the loop, but that would depend on the variables. Would that work? thanks! This might be a very easy problem to solve. I am literally brand new to php. I am trying to create a simple site that will load content depending on what page 'id' is in the URL and if it doesn't exist to show a page for that scenario. eg index.php?id=1 will show the page 1.php It works fine apart from showing an error when there is no id at all, eg www.website.com will show an error but www.website.com/index.php?id=1 will show a page I want to be able to show the homepage if there is no 'id' at all. Code: [Select] <?php $id = $_GET['id']; if(file_exists("./".$id.".php")) { include ("./".$id.".php"); } else { include ("404.php"); } ?> Any help is appreciated Thanks Hello All, I am new to php and programming in general. My question is as follows. I have created a header.inc.php and the index.php which includes header.inc.php. I have title set in header.inc.php under the "head". Code: [Select] <title><?php echo $title; ?> In the index.php, I have set variable $title to "Home Page" $title = "Home Page"; But whenever I visit index.php, the title is never called. Is it because the header has no clue where the $title is? Hi I'm trying the following code: Code: [Select] $t = '12<-- AB_C -->'; $AB_C = 'abc'; echo preg_replace('/\<-- ([A-Z_]+) --\>/', "$$1", $t);I want to get "12abc" , but it outputs: 12$AB_C , so, it not recognize the replacement as dynamic variable. Is it any way to use the matched word in preg_replace() as a variable, or dynamic variable? Hello, I'm working on a dynamic text form (I guess thats what its called). Basically, I have a html page that echos in some PHP code. What the PHP code does is show different strings of text depending on a variable: Code: [Select] <PHP? $brief1_info = "brief info goes here"; $brief1 = 0; if (isset($_POST['brief1Go'])) { $brief1 = $brief1 + 1; } else if (isset($_POST['brief1Back'])) { $brief1 = $brief1 - 1; } $breif1Controller = " <form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\"> <input type=\"submit\" name=\"brief1Back\" id=\"brief1Back\" value=\"BACK\" /> </form> <form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\"> <input type=\"submit\" name=\"brief1Go\" id=\"brief1Go\" value=\"CONTINUE\" /> </form>"; if($brief1 == 0){ $brief1_info = "<b>Welcome Commander,</b> you are currently viewing the Mission Control Brief page, here you can view all your missions that you need to complete in order to advance through your prestiges. You will need to follow your briefs carefully in order to succeed. <br /><br /> "; } else if($brief1 == 1){ $brief1_info = "Okay, let me show you around the place ..."; } else if($brief1 == 2){ $brief1_info = "brief is on 2"; } ?> The problem is, the BACK and CONTINUE buttons don't work. If $brief1 equals 0, and the user presses continue, the is should go to 1, and the brief1_info string is changed (so some different text shows in my html). It actually works when brief1 is on '0', but when its on 1 and the user pressed continue, the brief1 is changed to 1, but because the submit button refreshes the page, the variable is re-read again, and thus is reset back to 0. Is there a way around this? Or is there a better method than what I'm doing? Thanks Hi all, here's my code: Code: [Select] <?php foreach ($_SESSION['topping'] as $value) { echo "<tr><td width='30%'>Topping</td><td width='50%'>$value</td><td width='20%'><select name='notopping'>"; foreach ($_SESSION['cupcake'] as $number) { '<option name="notoppings[]" value="'.$number.'">".$number."</option>'; } echo "</select></td></tr>"; } ?> $_SESSION['cupcake'] is a value from either 6, 12, 24 or 36. What I want to do is put them into a drop down box (second foreach) as the value and the displayed value - counting up from 1 (so 1,2,3,4,5,6 or up to 12,24 etc). Also by creating this as an array, does this mean than for each topping (say Vanilla and Chocolate) the value dynamically created can be used on the next page by using $_POST['notoppings'] to display each type (two different numbers - one for Vanilla and one for Chocolate). Does that make sense? Thanks! Jason Hey, <a href="edit.php?id='.$row['id'].'">edit</a> I am learning PHP and I am wondering what I put after my SQL connection info so that I can create a variable $id $id = <what goes here> How do I make $id to store the unique number in this variable.. For example edit.php?id=100 how does it store this number...thanks... Okay, So I have a dropdown box: Code: [Select] <select name="imgsize"> <option value="small">480 x 360</option> <option value="medium">720 x 540</option> <option value="big">1200 x 900</option> <option value="large">1440 x 1080</option> </select> and I have code that gets the size: Code: [Select] $resizeimg = $_REQUEST["imgsize"]; But I cannot get it to work with an if statement: Code: [Select] if ($resizeimg == "large") { } It just comes up with about 50 lines of gibberish. I have used error_reporting(-1); but that is not coming up with any errors. Could anyone help please. Its probably so simple, but my mind is drawing a blank When i execute the following code, it works and retrieves the record info i am looking for for the appropriate record, 2155015898: $adgrouppass = 2155015898; $selector->adGroupIds = array($adgrouppass); However, when i try to get the adgroup variable from my url, which is where it is located, it doesn't work: $adgroup = trim($_GET['adgroup']); $adgrouppass = $adgroup; $selector->adGroupIds = array($adgrouppass); yet, in trying to fix it, if i echo the above variable, it says it is correct, which is 2155015898. Not sure if the $adgrouppass needs to be a string or variable to get it to pass it correctly. Can anyone help? Hi there, I am working on a php web form. I have a variable named $isFruit having value of 1 When I do a: print_r($isFruit) It gives me value as 1, which is good. Now I need to pass this php variable into a jquery function: $('document').ready(function() { //I need to get the value of $isFruit variable here } How can pass the php variable into this function. Kindly reply. All comments and feedbacks are always welcomed. Thank you! The code below works echo '<script language="javascript">'; echo 'top.location.href = "/breaking-news/bsnewsstorygoeshere/";'; echo '</script>'; this also works echo '<script language="javascript">'; echo 'top.location.href = "http://anywebsite.com";'; echo '</script>'; the problem, is I have a URL listed in a url variable that can be retreived via $_GET['url'] my question is how to get that variable into the above code? I've tried echo '<script language="javascript">'; echo "top.location.href = \"$_GET['url']\";"; echo '</script>'; but that doesnt seem to work, can anyone advise? Hi there, I am working on a PHP web form and I have a very simple situation I guess, I have a variable named: $MyVal When I do print_r($MyVal); To see whats inside, I get: SimpleXMLElement Object ( [0] => 8.23 ) Now I am assigning this variable into a session variable so that I can do calculations with it. So I assign: $_SESSION['MySesVal'] = $MyVal; But after assigning to session variable, when I do my calculations: $finalValue = $_SESSION['MySesVal'] * 4; I get 0. So is it because the actual $MyVal variable has some XML stuff as: SimpleXMLElement Object ( [0] => 8.23 ) So what is the right way to properly assign $MyVal variable to a session variable to do calculations. Please reply. All comments and feedbacks are always welcome. Thank you! OK, have no idea what's going on... I've done this a million times... why wont this output!?? I must have a major brain meltdown and dont know it yet!!! Code: [Select] <?php // this echoes just fine: echo $_POST['testfield']; // but this wont echo: echo if (isset($_POST['testfield'])) { $_POST['testfield'] = $test; } echo $test; /// or even this DOESNT echo either!: $_POST['testfield'] = $test; echo $test; ?> Folks, I need help (Php code ) to generate a Dynamic Text on a Base Image. What i want to do is, to make this Image as header on my Site and to make this Header Specific to a Site, i want to Add the Domain Name on the Lower Left of the Image. Got the Idea? Here is the Image link: Quote http://img27.imageshack.us/i/shoppingheader1.jpg/ PHP Variable that holds the Domain name is: $domain All i need the Dynamic PHP Codes that i can put on all my sites to generate this Text on Image (Header) Dynamically... May Anyone Help me with this Please? Cheers Natasha T. Hi all I need to combine these two scripts: Firstly, the following decides which out of the following list is selected based on its value in the mySQL table: <select name="pack_choice"> <option value="Meters / Pack"<?php echo (($result['pack_choice']=="Meters / Pack") ? ' selected="selected"':'') ?>>Meters / Pack (m2)</option> <option value="m3"<?php echo (($result['pack_choice']=="m3") ? ' selected="selected"':'') ?>>Meters / Pack (m3)</option> <option value="Quantity"<?php echo (($result['pack_choice']=="Quantity") ? ' selected="selected"':'') ?>>Quantity</option> </select> Although this works OK, I need it also to show dynamic values like this: select name="category"> <?php $listCategories=mysql_query("SELECT * FROM `product_categories` ORDER BY id ASC"); while($categoryReturned=mysql_fetch_array($listCategories)) { echo "<option value=\"".$categoryReturned['name']."\">".$categoryReturned['name']."</option>"; } ?> </select> I'm not sure if this is possible? Many thanks for your help. Pete |