AltarofScience Posted September 18, 2011 Share Posted September 18, 2011 i have mysql community server 5.5 workbench 5.2 php 5.3.8 apache http server 2.2 summary: can i write the same code using less variables for the file name and content? okay so i managed to write a code that creates a filename, and each new filename is 1 higher. colony1 colony2 colony3. it also uses the same mysql query created variable to write idcol as 1 higher in the new file than in the previous written file. however to write the code i had to use a buttload of variables. here is the code, the resultant filename and the resultant content of the created file. is there a way to write such a code with less than 15 variables? the problem seems to be that when writing text to a file, i cannot insert certain characters outside of a variable into the final variable which is put in the $string variable input into the fwrite() function. File Name: Colony$.php where $=the last entry in the idcol column in table coltest. File Code: <?php $idcol = $; include('Colony0.php'); ?> </body> </html> where $=the last entry in the idcol column in table coltest. Code: <?php $dbhost = 'localhost:3306'; $dbuser = 'root'; $dbpass = 'root'; $dbname = 'aosdb'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query = "SELECT idcol FROM coltest ORDER BY idcol DESC LIMIT 1"; $result = mysql_query($query); $id = mysql_result($result, 0); $vcol = "Colony"; $vcolp = ".php"; $File = "$vcol$id$vcolp"; $FileHandle = fopen($File, 'w') or die("can't open file"); fclose($FileHandle); $File = "$vcol$id$vcolp"; $myFile = $File; $fh = fopen($myFile, 'w') or die("can't open file"); $idw = "$"; $icol= "idcol"; $ique = "?"; $iphp = "Colony0.php"; $ief = "<"; $ieb = ">"; $irese = "= "; $iresn = ";"; $iii= "include('$iphp');"; $ihtml ="</body> </html>"; $hph = "php"; $string = "$ief$ique$hph $idw$icol $irese$id$iresn $iii $ique$ieb $ihtml"; fwrite($fh, $string); fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/247357-file-creation/ Share on other sites More sharing options...
Pandemikk Posted September 18, 2011 Share Posted September 18, 2011 You need to read up on string concatenation. <?php $File = 'Colony' . $id . '.php'; $FileHandle = fopen($File, 'w') or die("can't open file"); fclose($FileHandle); $File = "$vcol$id$vcolp"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, '<?php $idcol = ' . $id . '; include(\'Colony0.php\'); ?> </body> </html>'); fclose($fh); ?> Link to comment https://forums.phpfreaks.com/topic/247357-file-creation/#findComment-1270331 Share on other sites More sharing options...
voip03 Posted September 18, 2011 Share Posted September 18, 2011 String adding in PHP http://www.plus2net.com/php_tutorial/php_string_add.php Link to comment https://forums.phpfreaks.com/topic/247357-file-creation/#findComment-1270335 Share on other sites More sharing options...
creata.physics Posted September 18, 2011 Share Posted September 18, 2011 String adding in PHP http://www.plus2net.com/php_tutorial/php_string_add.php He's doing exactly that already. If you're talking about: $string = 'test'; $string .= 'test2'; Then he wouldn't even need that with his current code, since no variables with strings have the same name. However, I do agree with Pandemikk, he needs to read up on string concatenation. Link to comment https://forums.phpfreaks.com/topic/247357-file-creation/#findComment-1270344 Share on other sites More sharing options...
AltarofScience Posted September 18, 2011 Author Share Posted September 18, 2011 You need to read up on string concatenation. <?php $File = 'Colony' . $id . '.php'; $FileHandle = fopen($File, 'w') or die("can't open file"); fclose($FileHandle); $File = "$vcol$id$vcolp"; $fh = fopen($myFile, 'w') or die("can't open file"); fwrite($fh, '<?php $idcol = ' . $id . '; include(\'Colony0.php\'); ?> </body> </html>'); fclose($fh); ?> oh, that is super helpful. i actually know what that is, but only in a sort of intellectual way. never occurred to me to use it here. i tested it out, your code works with my software. thanks alot. thats gonna save me oodles of time later. all those variables were driving me nuts. Link to comment https://forums.phpfreaks.com/topic/247357-file-creation/#findComment-1270346 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.