Hello,
I am still learning php, and got stumped with how to generate dynamic variables.

I normally do something like this after I do my query SELECT to get all the data from the row.

$ID = $row->ID;
	$parentID = $row->parentID;
	$name = $row->name;
	$color = $row->color;
	$location = $row->location;
	$status = $row->status;

This is short example, sometimes it can get pretty long. So, I am sure there is a better way to do this.

If I start off with a comma separated string like:

$string = "ID, parentID, name, color, location, status";

How do I go about creating variables if I were to supply the above string?

Can someone pls either give me an example, or point me to a thread that can?

Instead of creating a comma separated string, why don't you use foreach loop ? You can get the key as well as the value. Use the 'key' as variable name!

i use list
ex.

list( $id,$parid,$name,$color,$location,$status ) = explode(',',$string);

Thank your for your quick replies.

I am not a total newbie to PHP, but I am not an advanced one either. I mainly use alot of functions, am still learning classes and arrays. Before I dive into classes, arrays, and even oop, I would like to get a better understanding of dynamic variables, or variable variables. (or whatever what I am trying to do is called).

My goal is to take the below code and make sections of code dynamic.
I thought the idea of starting with a static string of fieldnames would be a good start.
I have about 20 edit pages, all with a different set of variables.
I would like to replace them all, and make one edit page to handle different content tables.

A typical URL would be something like:

domain.com?guiID=x&id=1

Where "x" is the gui_table ID, and "id" is the content ID for content_table.

I am starting to understand how to use "while, for, and foreach" loops to make static strings, as in Section #3s Insert column names string.
But I am stumped on how to make the variables available to the rest of the code.

// This string #1 is generated from a recursive query looking for values in "fieldname" column in gui_table table.
// Example: SELECT ID, fieldname FROM gui_table WHERE parentID = 'x'. Then do again using ID, to get all fieldname(s) of children of 'x'.

// # 1.
$string = "ID, parentID, name, color, location, status";

/// IF SAVE BUTTON AT BOTTOM IS CLICKED, GRAB THE FOLLOWING:
if (isset($_POST['trigger'])){
	// # 2. POST SECTION
	if (isset($_POST['id'])) $id = trim($_POST['id']);
	if (isset($_POST['parentID'])) $parentID = trim($_POST['parentID']);
	if (isset($_POST['name'])) $name = trim($_POST['name']);
	if (isset($_POST['color'])) $color = trim($_POST['color']);
	if (isset($_POST['location'])) $location = trim($_POST['location']);
	if (isset($_POST['status'])) $status = trim($_POST['status']);

	/// # 3. INSERT QUERY - If id does not exist, insert new row
	if ($id == ''){
		$query = "INSERT INTO content_table (ID , parentID, name , color , location , status)
		VALUES (NULL, '$parentID', '$name', '$color', '$location' , '$status')";
		$result = mysql_query($query);
	}
	// # 4. UPDATE QUERY - If id does exist, do row update
	else {
	    $query = "UPDATE content_table SET
			`parentID` = '$parentID',
			`name` = '$name',
			`color` = '$color',
			`location` = '$location',
			`status` = '$status'
			WHERE `ID` = '$id' ";
		$result = mysql_query($query);
	}
}

// IF id in URL, get the row content
if ($id <> ""){
	// # 5. SELECT QUERY 
	$query = "SELECT * FROM content_table WHERE `ID`='1' ";
	$result = mysql_query($query);
	$Numrecs = mysql_num_rows($result);
	if ($Numrecs){
		while ($row = mysql_fetch_object($result)){
			// # 6. GET DATA - make following available to variables on page (form) for editing
			$ID = $row->ID;
			$parentID = $row->parentID;
			$name = $row->name;
			$color = $row->color;
			$location = $row->location;
			$status = $row->status;
		}
	}
}

// # 7. FORM

<form name="contentForm" action="<?php echo $_SERVER['$PHP_SELF'];?>" method="post" enctype="multipart/form-data">
	<strong>Name</strong>
	<input type="text" name="name" value="<?=$name?>">
	
	<strong>Color</strong>
	<input type="text" name="color" value="<?=$color?>">
	
	<strong>Location</strong>
	<input type="text" name="location" value="<?=$location?>">
	
	<strong>Status</strong>
	<input type="text" name="status" value="<?=$status?>">
	<input type="hidden" name="id" value="<?=$id?>">
	<input type="hidden" name="trigger" value="1">
	<input type="submit" name="Save" value="Save Changes">
</form>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.