tywiggle Posted December 16, 2008 Share Posted December 16, 2008 Attempting to create a number of static web pages. Is there a way to automatically add the variable and then auto save the page as 'pattern'.php? Here is the code I have been using: <?php $cxn=mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query="select pattern, a, b, c, d, e from 'database' where pattern='pattern'";(<--this is the variable I need to change automatically) $result=mysqli_query($cxn, $query); $row=mysqli_fetch_assoc($result); extract($row); ?> After 'pattern' is automatically inserted into the code I need to automatically save the page as 'pattern'.php and then move on the the next 'pattern' in the database. Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/ Share on other sites More sharing options...
trq Posted December 16, 2008 Share Posted December 16, 2008 Something like? <?php $cxn = mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query = "SELECT pattern, a, b, c, d, e FROM database"; if ($result = mysqli_query($cxn, $query)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)) { ob_start(); foreach (range('a','e') as $pos) { echo $row[$pos]; } file_put_contents($row['pattern'] . '.php', ob_get_contents()); ob_end_clean(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-717107 Share on other sites More sharing options...
tywiggle Posted December 17, 2008 Author Share Posted December 17, 2008 Brilliant. Thank you very much. I am sure I will appreciate your code even more when I can fully understand it. Perhaps I can trouble you a bit more? When I execute this, I get the appropriate named files saved onto my website. They are all blank as in zero bytes. I assume I need to do something within the object buffer. I do not understand what "$pos" is or what it does in the echo statement. How would I execute the html portion of the page that calls the variables pulled in during the query? Do I extract the $row? Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-717719 Share on other sites More sharing options...
trq Posted December 17, 2008 Share Posted December 17, 2008 All this part does.... foreach (range('a','e') as $pos) { echo $row[$pos]; } is echo each field in your record. eg; Its the same as writting.... echo $row['a']; echo $row['b']; echo $row['c']; echo $row['d']; echo $row['e']; Now, Im not sure exactly what you want your files to contain, but any output between ob_start() and the call to ob_get_contents() will be written to the file. No its not enough to simply extract the variables from the array, they need to be echo'd. Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-717748 Share on other sites More sharing options...
tywiggle Posted December 17, 2008 Author Share Posted December 17, 2008 Thank you so much for your expertise and time. I am very willing to learn though a bit overwhelmed. Here is an example (chopped down and now messy) of what my html looks like and what I am attempting to feed the data into. There are a number of items named 'pattern' and I am trying to generate a php page for each one. I have previously been manually entering 'pattern' then saving as 'pattern.php'. With a limited understanding of php/mysql I have attempted to use the script you wrote but it is ending up with blank pages and no data as coded below. <!--my original code--> <?php $cxn=mysqli_connect ("localhost","xxxx","yyyy"); mysqli_select_db($cxn, "zzzz"); $query="Select pattern, folder, name, url, name1 from database where pattern= 'abcdefg'"; $result=mysqli_query($cxn, $query); $row=mysqli_fetch_assoc($result); extract($row); ?> <!-- new and improved code--> <?php $cxn = mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query = "SELECT pattern, folder, name, url, name1 FROM database"; if ($result = mysqli_query($cxn, $query)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)) { ob_start(); foreach (range('folder','name1') as $pos) { echo $row[$pos]; extract($row);<!--I added this and got the very last 'pattern' in the database to display a page--> } file_put_contents($row['pattern'] . '.php', ob_get_contents()); ob_end_clean(); } } } ?> <html> <head> <title><?php echo $pattern; ?> ?></title> <meta name="description" content="<?php echo $pattern; ?>/> <meta name="keywords" content="<?php echo $pattern; ?> " /> <style type="text/css"> </style> </head> <body> <table width="900" border="0" align="center" cellpadding="1" cellspacing="0" bgcolor="#FFFFFF"> <tr> <td bgcolor="#FFFFFF" valign="top"> <table cellspacing="0" cellpadding="0" border="0" width="800"> <tr> <td> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr bgcolor="#000033"> <td width="14" nowrap="nowrap" valign="middle"></td> <td height="24" nowrap="nowrap" colspan="12"><a href="http://www.<?php echo $url ?>.com" class="c3"></a> <div class="c6"><span class="c4"><?php echo $name; ?> .com</span>...<span class="c5"><strong><?php echo $name1; ?></strong></span></div> </td> <td nowrap="nowrap" valign="middle"><span class="c5"><strong> <?php $today = date("F j, Y");PRINT "$today"; ?></strong></span></td> </tr> </table> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tr> <td height="1" nowrap="nowrap" bgcolor="#FFFFFF" colspan="10" width="100%"></td> </tr> <tr> <td height="7" nowrap="nowrap" bgcolor="#990000" colspan="10" width="100%"></td> </tr> </table> <table cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" align="center" class="c9" width="100%"> <tr> <td width="100%" valign="top"><table cellspacing="0" cellpadding="0" border="0" align="center" width="100%" > <tr> <td height="14" valign="top" bgcolor="#FFFFFF"><span class="c2"> <a href="http://www.<?php echo $url; ?>.com"> <strong><?php echo $name; ?> Home</strong></a> <span class="c7"><strong>>></strong></span> <a href="http://www.<?php echo $url; ?>.com/<?php echo $name1; ?>.php"> <strong>Name 1</strong> <span class="c7"><strong>>></strong></span> <strong>Designs <?php echo $name1; ?> </strong></a> <span class="c7"><strong>>></strong></span> <span class="c12"><strong><?php echo $pattern; ?></strong></span> </span></td> </tr> <tr> <td> <table width="100%" border="0" cellspacing="0" cellpadding="5" bgcolor="#FFFFFF"> <tr> <td align="center"> <img src="http://www.dataprocess.com/name1/<?php echo $folder; ?>/<?php echo $pattern; ?>.jpg" border="0" /> </td> </tr> <tr> </tr> </table> </td> </tr> </table> </td> </tr> </table> </td> </tr> </table> <tr> <td colspan="3" bgcolor="#FFFFFF"> <div class="c6"> <hr width="90%" /> <span class="c11"><?php echo $url; ?>.com Copyright 2005-<?php $today = date("Y");PRINT "$today"; ?> All Rights Reserved </span></div> </td> </tr> </table> </td> </tr> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-717862 Share on other sites More sharing options...
xtopolis Posted December 17, 2008 Share Posted December 17, 2008 Please used [code ] tags next time! Also, I haven't read it all, but it looks like you're using range() improperly. <?php foreach (range('folder','name1') as $pos) { echo $row[$pos]; extract($row);<!--I added this and got the very last 'pattern' in the database to display a page--> } ?> The values of that range are: [pre]Array ( [0 ] => f [1] => g [2] => h [3] => i [4] => j [5] => k [6] => l [7] => m [8] => n ) [/pre] Was this what you intended? range Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-717940 Share on other sites More sharing options...
trq Posted December 17, 2008 Share Posted December 17, 2008 Ok, all that html mixed with php below the script I wrote you, put it all in a file called template.php. Then place this script within the same directory. <?php $cxn = mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query = "SELECT pattern, folder, name, url, name1 FROM database"; if ($result = mysqli_query($cxn, $query)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)) { ob_start(); extract($row); include "template.php"; file_put_contents($pattern . '.php', ob_get_contents()); ob_end_clean(); } } } ?> That should do it. Either that or move all that html / php into where the include statement is. Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-718138 Share on other sites More sharing options...
tywiggle Posted December 22, 2008 Author Share Posted December 22, 2008 You are a brilliant programmer. Your new script works like a charm. I have another question. I modified your script trying to get file_put_contents to put the files into sub directories but is not working. Have I made some type of syntax error or is this even possible? file_put_contents($subfolder / $pattern . '.php', ob_get_contents()); Thank you very much. <?php $cxn = mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query = "SELECT pattern, folder, name, url, name1, subfolder (!<--- added this---->) FROM database"; if ($result = mysqli_query($cxn, $query)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)) { ob_start(); extract($row); include "template.php"; file_put_contents($subfolder / $pattern . '.php', ob_get_contents()); ob_end_clean(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-721676 Share on other sites More sharing options...
trq Posted December 22, 2008 Share Posted December 22, 2008 Keep in mind the subfolders would need to exist, plus your syntax is slightly off. Try.... <?php $cxn = mysqli_connect ("localhost", "xxxx", "yyyy"); mysquli_select_db($cxn, "zzzz"); $query = "SELECT pattern, folder, name, url, name1, subfolder FROM database"; if ($result = mysqli_query($cxn, $query)) { if (mysqli_num_rows($result)) { while ($row = mysqli_fetch_assoc($result)) { ob_start(); extract($row); include "template.php"; if (!is_dir($subfolder)) { mkdir ($subfolder, 755); } file_put_contents("$subfolder/$pattern" . '.php', ob_get_contents()); ob_end_clean(); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/137212-solved-script-suggestion/#findComment-721778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.