Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I use the entries in an array as variables

Status
Not open for further replies.

EdLentz

Technical User
Mar 20, 2002
85
US
I have a page that I need to query a DB, put all the records in an array and then use the row info as variables in a while statement to create files.
Here is my code for getting the array info
<?php
require('/var/
$query = "SELECT * FROM assignments";
if ($result=mysqli_query($link,$query)) {

// Associative array
while ($row = mysqli_fetch_assoc($result)){
printf ("%s %s %s %s",$row["extension"],$row["secret"],$row["macaddress"],$row["template"]);

}
// Free result set
mysqli_free_result($result);
}

//Close Mysqli link
mysqli_close($link);
?>

This prints the needed info to my screen. This part was to test to make sure I was doing something right.

The info in the array needs to be used in this code:
copy('/var/ '/var/$template = "working.cfg";
$mac = "80.28.83.66.5f.23";
$extension = "102";
$secret = "aaasuda9841japsu9ajapdj";
$newname = "102";
$myfile = fopen("$template", "a") or die("Unable to open file!");
$txt = "account.1.display_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.auth_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.user_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.password = $secret\n";
fwrite($myfile, $txt);
$txt = "account.1.label = $extension\n";
fwrite($myfile, $txt);
fclose($myfile);
rename("$template", "$mac.cfg");
echo "The file creation is done";
mysqli_close($link);

It would replace the $mac, $template, $extension, and the $secret variables that I setup to test the file creation.

How do I do this?
Thanks in advance for any help.
 
There's several ways to accomplish this. The dirtier and more straight forward way of doing this, is to just place your file creation code inside the while loop, and assign the query values to your variables:

Code:
[b]
while ($row = mysqli_fetch_assoc($result))
{
[/b]
[COLOR=#868686]
copy('/var/[URL unfurl="true"]www/html/cqadmin/Templates/CQ600Template.cfg',[/URL] '/var/[URL unfurl="true"]www/html/cqadmin/test/working.cfg');[/URL]
$template = [b][COLOR=#007733]$row["template"][/color];[/b]
$mac = [b][COLOR=#007733]$row["macaddress"][/color];[/b]
$extension = [b][COLOR=#007733]$row["extension"][/color];[/b]
$secret = [b][COLOR=#007733]$row["secret"][/color][/b];
$newname = "102";
$myfile = fopen("$template", "a") or die("Unable to open file!");
$txt = "account.1.display_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.auth_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.user_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.password = $secret\n";
fwrite($myfile, $txt);
$txt = "account.1.label = $extension\n";
fwrite($myfile, $txt);
fclose($myfile);
rename("$template", "$mac.cfg");
echo "The file creation is done";
[/color]
[b]
}
[/b]

The nicer way would be to make your file creation code into a function, that can take parameters. That way you can call it inside the while loop, and pass the values you are getting in the while loop the function to create the files.



----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Thanks Phil for the help. I am just about where I need to be with this portion of my project. This works except the copy function does not get the template file and append the info, (extension, secret) to the file. It does create the mac named file with the fwrite information correctly.
Code:
<?php
require('/var/[URL unfurl="true"]www/html/cqadmin/utils/connect.php');[/URL]
error_reporting(E_ALL);
        ini_set('display_errors', '1');
//Query for getting the Extension & Secrets
$result = mysqli_query($link,"SELECT * FROM `assignments` LIMIT 0, 30");
while ($row = mysqli_fetch_assoc($result))
{
$template = $row["template"];
$mac = $row["macaddress"];
$extension = $row["extension"];
$secret = $row["secret"];
$newname = $row["extension"];
copy('/var/[URL unfurl="true"]www/html/cqadmin/Templates/$template',[/URL] '/var/[URL unfurl="true"]www/html/cqadmin/test/working.cfg');[/URL]

$myfile = fopen("$template", "a") or die("Unable to open file!");
$txt = "account.1.display_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.auth_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.user_name = $extension\n";
fwrite($myfile, $txt);
$txt = "account.1.password = $secret\n";
fwrite($myfile, $txt);
$txt = "account.1.label = $extension\n";
fwrite($myfile, $txt);
fclose($myfile);
rename("$template", "$mac.cfg");
}
echo "The file creation is done";

mysqli_close($link);

?>
Getting this error: Warning: copy(/var/ failed to open stream: No such file or directory in /var/ on line 14
 
Variables are not expanded inside single quotes. Its trying to open a file with the name "$template" instead of using the value of the variable $template as the file name.

Turn this:

copy([highlight #FCE94F]'[/highlight]/var/www/html/cqadmin/Templates/$template[highlight #FCE94F]'[/highlight], '/var/
into

copy("/var/www/html/cqadmin/Templates/$template", '/var/
or even:

copy([highlight #FCE94F]'[/highlight]/var/www/html/cqadmin/Templates/[highlight #FCE94F]'[/highlight] . $template, '/var/


----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech
 
Thanks Phil

That got it.

Thank you so much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top