twittoris Posted October 8, 2009 Share Posted October 8, 2009 Is it possible to have the script (PHP/Javascript) only display a certain amount of results (like top 3) that are in a table and take the next three and save it as a new table in a new file? Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/ Share on other sites More sharing options...
jamesxg1 Posted October 8, 2009 Share Posted October 8, 2009 Table ?, File ?. Describe in more detial what you want here ?, mysql_affected_rows() might be what your after. James. Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933187 Share on other sites More sharing options...
twittoris Posted October 8, 2009 Author Share Posted October 8, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933194 Share on other sites More sharing options...
twittoris Posted October 8, 2009 Author Share Posted October 8, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933388 Share on other sites More sharing options...
Psycho Posted October 8, 2009 Share Posted October 8, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933406 Share on other sites More sharing options...
twittoris Posted October 9, 2009 Author Share Posted October 9, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933445 Share on other sites More sharing options...
trq Posted October 9, 2009 Share Posted October 9, 2009 Should be... $website = "<a href='{$http}{$website}'>{$website}</a><br/>"; Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933490 Share on other sites More sharing options...
twittoris Posted October 9, 2009 Author Share Posted October 9, 2009 woah that gives a crazy output with two separate tables and none of my txt file. Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933505 Share on other sites More sharing options...
Psycho Posted October 9, 2009 Share Posted October 9, 2009 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> "; Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933530 Share on other sites More sharing options...
twittoris Posted October 9, 2009 Author Share Posted October 9, 2009 Is there a way to detect the last file name created so that when I reference the pages I can make a script to automatically load the correct file when the next page button is hit on the front end page? Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933742 Share on other sites More sharing options...
Psycho Posted October 9, 2009 Share Posted October 9, 2009 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> Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933779 Share on other sites More sharing options...
twittoris Posted October 9, 2009 Author Share Posted October 9, 2009 Yeah that does work alot better. However, I am inserting adsense automatically next to each link and if I am not mistaken the adsense robots work better on hard coded pages. Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933798 Share on other sites More sharing options...
twittoris Posted October 9, 2009 Author Share Posted October 9, 2009 How do you make the links open in a new window? Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933898 Share on other sites More sharing options...
Psycho Posted October 9, 2009 Share Posted October 9, 2009 target="_blank" Quote Link to comment https://forums.phpfreaks.com/topic/176987-solved-create-new-file-after-certain-amount-of-entries/#findComment-933968 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.