PHP - Make A Dynamic Variable
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! Similar TutorialsFolks, 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. Hello everyone. This is my first post, so be nice! I am building a website that will have a lot of content, similar to a newspaper. I have some pretty good HTML/CSS pages written, but the problem is that I need a way to make things more dynamic. One of my templates has a Header, Left Column, Middle Column, Right Column, and Footer. Everything stays the same from page to page except for the Middle Column (which holds each article). As it stands now, if I had 12 articles, I would have to have 12 nearly duplicate HTML pages which isn't good! I started studying PHP a while ago, but put that on hold to learn HTML/CSS, so I've kinda forgotten how PHP can help me out! Can someone help me figure out how to use PHP to my benefit? Thanks, Debbie since many do not know the term sticky = when a form is submitted, the data contained is not deleted but instead last checked data will be shown. I have some dynamically generated checkboxes from the database. I tried the below code to make them sticky but they are simply preselected in stead of becoming sticky. What am I doing wrong? <?php $sql2 = "SELECT relation FROM table_rel_info"; $result2 = @mysqli_query($dbc, $sql2); while($row2 = mysqli_fetch_array($result2, MYSQLI_ASSOC)) { echo "<label>" . $row2['relation'] . "</label>"; echo "<input class='box' type='checkbox' value=1"; ?> <?php if (isset($row2['relation'])) { echo 'checked="checked"';}?> <?php echo "'/>" . "<br />"; } ?> I have a product page which populates all my products in one page. I have also a detail page which gives details on a product which i wanted to know. My problem is when I am going to click on the product that I want the detail page shows incorrect product details. I just want one detail product page so that it will be easy to edit the page in the future. I am asking an Idea on how to make one detail page in all of my products.. thanks... 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 ! <?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? 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 Hi All, For the last post,I m very happy that i found out so many great mind who are watching my problem and solving online. anyway great thanks for all, just now i m making a project and i m facing problem in the below code "i need to know how can make this code easy or how can i make rdata variable accessible for the download button" generatecv.php file <?php if(isset($_REQUEST['submit'])) { $name=$_REQUEST['pname']; $pin=$_REQUEST['pin']; $mnumb=$_REQUEST['mnumber']; $tnumb=$_REQUEST['telenumber']; $email=$_REQUEST['email']; $obj=$_REQUEST['obj']; $title=$_REQUEST['jobt']; $activity=$_REQUEST['activity']; //$file=fopen('test.doc','a+'); //echo @fwrite($file,$name); //fclose($file); $rdata=""; $rdata.="<table id='cvtbl' border='0' align='center' > <th>Resume</th> <tr><td>".$name.":</td></tr><tr><td>Objective:</td></tr>"; $rdata.="<tr><td>".$obj."</td></tr>"; $rdata.="<tr><td>Email:".$email."</td></tr>"; $rdata.="</table>"; echo $rdata; echo "<style>#cv{display:none;}</style>"; echo "<form><input type='submit' name='dwnld' id='dwnld' value='Download'/></form>"; } if(isset($_REQUEST['dwnld'])) { header("Content-type:application/msword"); header("Content-disposition:attachment;filename=test.doc"); $fp=fopen("test.doc","r") or die("try again"); $data=fread($fp,filesize('test.doc')); fwrite($fp,$rdata); echo $data; fclose($fp); //echo $rdata."<br />".$rdata; } ?> this is the html file <html> <title>User details</title> <body> <form name='cv' id='cv' method="post" enctype="multipart/form-data" action='generatecv.php'> Enter your name<input type=text name=pname id=pname /> Zip Code<input type=text name=pin id=pin /> Mobile number<input type=text name=mnumber id=mnumber /> <br />Telephone number <input type=text name=telenumber id=telenumber /> Email address<input type=text name=email id=email /> <br />Enter your objective<br /><textarea name=obj id=obj cols=30 rows=5 ></textarea> <!--University Name <input type=text name=university id=university /> --> <br /><!--uni, city ,state ,type of degree ,full degree name,date--> <br /><!--Experience<br /><textarea name=exp id=exp cols=30 rows=5 ></textarea> --> Select type of resume <select name='type' id='type' onChange=''> <option value='0'>Graduate</option> <option value='1'>Post Graduate</option> <option value='2'>Non Graguate</option> <option value='3'>Other</option> </select> Job title<input type='text' name='jobt' id='jobt' /> <br /><!--company name,city ,state ,office number ,job description ,date of employment--> <br />Activity <input type=text name=activity id=activity size=100 /> <br />Honors/Awards<input type=text name=honor id=honor size=100 /> <input type='submit' name='submit' id='submit' value='Generate CV' /> <input type='reset' name='Reset' id='submit' value='Reset' /> </form> </body> </html> Many thanks in advance When login in (in login.php), the script is assigning user information to scalar variables like so: $user_name = $assoc['user_name']; $user_id = $assoc['user_id']; Now I'd like to make use of those variables on other pages like this for example: echo "Posted by " . $user_name; I want that to be visible for everybody, even if the user logs out. Any ideas? I'm creating a website where writers can share stories. I have a section where the users enter a summary of there story. Anyways in the area where the story in being listed I want the the title of the story(with a link to the story) underneath the link I would like a summary. BUUUUT, the summary can get pretty long(up to 900 chars). So I am wondering if I can shorten the variable where the summary is stored, I want it to be shorten to a fixed amount of characters(maybe 200?) then followed by a ... . So how can I do this? Hello, I'm working on a script which should read lines from a file keyword.txt create a new file with that variable name, insert the content close and loop through all the lines in the keyword file. The loop itself works but now that i'm adding the fopen fwrite and fclose i'm running into trouble. I'm not that great a coder and have pieced this together so far. I'd appreciate any suggestions on how to fix the fopen and other file functions to create unique files using the keywords from the file Thanks in advance <?php $read_file = file('keywords.txt'); foreach($read_file as $var) { $file = fopen($var, "x"); $content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\"> <head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> <title>This is the title</title> </head> <body> <h2>The keyword is $var</h2> <p><p> </body> </html>"; fwrite($file, $content); fclose($file); } ?> 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 Okay so my news script is set to view only 10 pieces of news. But I want it so that it starts a new page once I have more than 10 pieces of news. Code: [Select] <?php require("functions.php"); include("dbconnect.php"); session_start(); head1(); body1(); new_temp(); sotw(); navbar(); $start = 0; $display = 10; $query = "SELECT * FROM news ORDER BY id DESC LIMIT $start, $display"; $result = mysql_query( $query ); if ($result) { while( $row = @mysql_fetch_array( $result, MYSQL_ASSOC ) ) { news_box( $row['news'], $row['title'], $row['user'], $row['date'], $row['id'] ); } mysql_free_result($result); } else { news_box( 'Could not retrieve news entries!', 'Error', 'Error', 'Error'); } footer(); mysql_close($link); ?> I tried a few things but they failed....miserably. |