Jump to content

[SOLVED] Create New file After certain amount of Entries


twittoris

Recommended Posts

I am currently creating a table like so:

while (!feof($fp)) {
$line = fgets($fp,1024); //use 2048 if very long lines
$row++;
list ($website, $description)= split ("\|", $line);
//$website= "<a href=\"" . $website . "\">" . $website . "</a><br/>";
$Http = substr($website, 0,7);
if ($Http=="http://") {
   $website= "<a href=\"".$website. "\">" . $website . "</a><br/>";
} else {
   $website="<a href=\"http://".$website. "\">" . $website . "</a><br/>";
}   
$col[$row] = array($row, $website, $description, $adsense);
      
}

fclose($fp);

sort($col);
reset ($col);

$arrays = count($col) - 1;

$loop = -1;
while ($loop < $arrays) {
$loop++;
echo '

<tr>
<td>'.$col[$loop][1].'</td>
<td>'.$col[$loop][2].'</td>
<td>'.$col[$loop][3].'</td>
</tr>';

}

echo '
</table>
<p><small>1 December 2004 · Last updated: '.date('j F Y', getlastmod());  
$HtmlCode= ob_get_contents(); 
ob_end_flush(); 

$fh=fopen('cookies.html','w');
fwrite($fh,$HtmlCode);

fclose($fh);

?>

I want to take the table that is created in cookies.html and create cookies1.html, cookies2.html with every 3 rows created.

 

 

 

EDIT: CODE TAGS

Link to comment
Share on other sites

A lot of your code makes no sense. If you are giving $row an incremental value, then why are you sorting the array? It will already be in sequential value. Plus sort() is not meant to be used on a multidimensional array. reset() will also not be needed since the array will already be at the first element. In fact, you don't need $row at all.

 

<?php

while (!feof($fp))
{
   $line = fgets($fp,1024); //use 2048 if very long lines
   list ($website, $description) = split ("|", $line);
   //$website= "<a href="" . $website . "">" . $website . "</a><br/>";

   $http = (substr($website, 0,7)=="http://") ? '' : 'http://' ;
   $website = "<a href="{$http}{$website}">{$website}</a><br/> ";
   $rows[] = array(
      'website'     => $website,
      'description' => $description,
      'adsense'     => $adsense
   );
}

fclose($fp);

$tableCount = ceil(count($rows)/3);
$recordsPerTable = 3;
$tablesHTML = '';

for($i=1; $i<=$tableCount; $i++)
{
   $rowsHTML = '';
   for ($j=1; $j<=$recordsPerTable; $j++)
   {
      $rowsHTML .= "  <tr> ";
      $rowsHTML .= "    <td>{$rows[$i]['website']}</td> ";
      $rowsHTML .= "    <td>{$rows[$i]['description']}</td> ";
      $rowsHTML .= "    <td>{$rows[$i]['adsense']}</td> ";
      $rowsHTML .= "  </tr> ";
   }

   $tablesHTML .= "<table> {$rowsHTML}</table> ";
}

echo $tablesHTML;
echo "<p><small>1 December 2004 · Last updated: ".date('j F Y', getlastmod());  

$HtmlCode = ob_get_contents();
ob_end_flush(); 

$fh=fopen('cookies.html','w');
fwrite($fh,$HtmlCode);

fclose($fh);

?>

Link to comment
Share on other sites

Oh yeah I initially had it set to arrange in alphabetical order.

that code gave me an error, 

$http = (substr($website, 0,7)=="http://") ? '' : 'http://' ;

$website = "<a href="{$http}{$website}">{$website}</a><br/> ";

 

the $website= gives an error of

Parse error: syntax error, unexpected '{' in /home/content/e/m/p/empireestate/html/list/listcreator.php on line 115

 

and im not good that yet

Link to comment
Share on other sites

OK, I misread your first post and thought you wanted separate tables on the same page. No matter the logic is still valid. You just need to create a new file on each iteration of the first for loop where this line is

   $tablesHTML .= "<table> {$rowsHTML}</table> ";

Link to comment
Share on other sites

You are making no sense now. I don't see what that question has to do with what you originally asked. You need to understand that we are not sitting there with you involved in whatever you are working on. We only know what you tell us, and that isn't much.

 

However, I will venture a guess that what you are doing is trying to process some data file and create multiple flat HTML pages. And now you are wanting to create links in those HTML files to navigate from one page to another. Why?

 

If you are using PHP, just create ONE PHP page that will allow the user to dynamically display the different "pages"? The whole point of using a server-side language is that you don't have to hard code the pages - they can be dynamic. And, you seem to have taken the approach of using PHP to generate hard coded pages.

 

Give this a try. Just set the $dataFile to the file you are reading data from. You can now have all your "pages" with a single file. And if your input file with the data changes you don't have to regenerate the flat files - this page will continue to work with the new data.

 

<?php

$recordsPerPage = 3;
$dataFile = 'somefile.txt';


$fileContents = file($dataFile);

foreach ($fileContents as $line)
{
   list ($website, $description) = explode('|', $line);
   $website = trim($website);
   $description = trim($description);

   $http = (substr($website, 0,7)=="http://") ? '' : 'http://' ;
   $website = "<a href=\"{$http}{$website}\">{$website}</a><br/> ";
   $rows[] = array(
      'website'     => $website,
      'description' => $description,
      'adsense'     => $adsense
   );
}

//Set number of total pages
$pageCount = ceil(count($rows) / $recordsPerPage);

//Set current page
$page = (int) $_GET['page'];
if ($page<1 || $page>$pageCount)
{
    $page = 1;
}

//Create page specific output
$rowsHTML = '';
for ($i=0; $i<$recordsPerPage; $i++)
{
    $record = ($page-1) * $recordsPerPage + $i;
    $rowsHTML .= "  <tr> ";
    $rowsHTML .= "    <td>{$rows[$record]['website']}</td> ";
    $rowsHTML .= "    <td>{$rows[$record]['description']}</td> ";
    $rowsHTML .= "    <td>{$rows[$record]['adsense']}</td> ";
    $rowsHTML .= "  </tr> ";
}

//Create page navigation
$nav = "Page {$page} of {$pageCount}: ";
$nav .= ($page==1) ? 'Prev': '<a href="?page='.($page-1).'">Prev</a>';
$nav .= ' | ';
$nav .= ($page==$pageCount) ? 'Next': '<a href="?page='.($page+1).'">Next</a>';

//Set todays date
$lastUpdate = date('j F Y', getlastmod());  

?>
<html>
<head></head>

<body>
<table>
<?php echo $nav; ?>
<br /><br />
<?php echo $rowsHTML; ?>
</table>
<br /><br />
<p><small>1 December 2004 · Last updated: <?php echo $lastUpdate; ?>  
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.