Loop File writing to generate files automatically
04-17-2006, 10:42 AM
|
Loop File writing to generate files automatically
|
Posts: 12
|
I Need Help, I am facing a little problem here, and I'm stuck with something, especially that i am not a programmer.
here's the deal, i am getting the query from the data base and limiting the query for 10 entries per page
$rowsPerPage = 10;
$req = 'Anything';
$query = "SELECT * FROM `article` WHERE headline like '%$req%' . " LIMIT $offset, $rowsPerPage";
and it's getting all I need from the database correctly, then I wanted the same file I am working on to write the query result in multiple pages writing 10 results per page generating the files like the $req value. For this example if i had 30 results of the "Anything" to generate anything1.html anything2.html anything3.html. it doesnt matter the formart of the html files if it's generating them by d m y or any other format, the most important that i want to write a query result to multiple files.
then i added this to write the file but its writing all the result in one file
$filename = "$req" . ".html";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $show) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($req) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
As I mentioned this script is also working and have no problem with it, and of course the page i have is bigger than that, i am getting the results and have another query to count the numrows from the table i am querying but the only problem i have is i want the writing script to generate the query into many files limiting each file by 10 rows per page just like the $rowsPerPage, saving them as static files and not temp ones, on the server. I know this is a hard/complicated one.
Thank you so much in advance.
Last edited by mafios; 04-17-2006 at 11:37 AM..
|
|
|
|
04-17-2006, 12:52 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
What is the webpage. I would like some idea what this is producing.
|
|
|
|
04-17-2006, 12:55 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
the webpage is just to get results from a large database, i dont have it for public yet, i am still testing it out, i want to use the contents of the database but dont want each time to get a subject to go thru the sql, wanted to write them automatically in multiple pages once and then people can browse thru the static automatically written html pages. thanks for asking anyways, hope i can solve this problem soon 
|
|
|
|
04-17-2006, 01:03 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
What exactly is not working?
|
|
|
|
04-17-2006, 01:49 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
the script i have is working, the problem that i want the script to loop and generate the static pages giving each one a different name, placing in each page 10 rows of the query result. now the script is writing all of the query result in one page so i want it to split it into 10 rows per page and generate the files with different names
|
|
|
|
04-17-2006, 02:01 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 75
|
How about something like this. I've trimmed out a lot of it, to only show the significant bits
PHP Code:
for ($i=0, $j=mysql_num_rows($result); $i<$j; $i++) { $offset = $i * 10;//means it goes up in tens /*query goes here*/ $filename = "filename".$i.".html";//so the filename would be filename0.html for the first 10 and filename1.html for the second /*code for writing file goes here*/ }
I'm not sure how this will work out, because I'm not entirely certain about what you are trying to do, but perhaps it gives you some ideas?
Last edited by cdwhalley.com; 04-17-2006 at 03:05 PM..
|
|
|
|
04-17-2006, 02:30 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
Great Cdwhalley, thank you so much for your reply really.
with your code that i added, it wrote filename0.html and thats what i used to receive before, you gave me the loop function but its not writing each 10 on a file, it's writing them still all in one file. when i tried it, it wrote everything in the filename0.html which i wanted almost the same way but to write each 10, like filename0.html has 10 entries filename1.html has another 10.
is there any way with your code to do that? (i think that's what u meant also by giving me the code which i really appereciate but it's not working)
thank you again
|
|
|
|
04-17-2006, 03:05 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 75
|
Well, thats what I was intending my code to do  , but if its not doing it then I haven't been much help.
My only suggestion (not that I'm saying my code is flawless) is that your SQL query is returning all the data in one go, instead of splitting it into sections of 10. Firstly, do you actually have more than 10 articles in the database to start with, and secondly, are you sure you have the syntax of the query correct.
|
|
|
|
04-17-2006, 03:16 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
i think i have made everything right (i think lol) i have 40 articles in there which should be at least 4 pages, maybe i am missing something with it cause it looks right to me, i have echo'ed theh query which i called it $display and the writing cause i am using, i will show you the code i have
PHP Code:
<? php for ($i=0, $j=mysql_num_rows($result); $i<$j; $i++) { } $offset = $i * 10;//means it goes up in tens $query = "SELECT * FROM `article` WHERE headline like '%anything%'"; $result = mysql_query($query) or die('Error, query failed'); while($row = mysql_fetch_array($result)){ $content= $row[content]; $date = $row[harvestTime]; $source= $row[source]; $id = $row[id]; $url= $row[url]; $headline = ucwords (strtolower(stripslashes($row[headline]))); $display .= "<p><strong><a href=\"$url\">$headline</a></strong><br>From: $date<br><strong>Source: $source</strong>"; } $filename = "filename".$i.".html"; if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $display) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote (anything) to file ($filename)"; fclose($handle); mysql_close(); ?> <?php echo "$display"; ?>
and as i told you it's writing everything in one file now, i hope i am doing something wrong so i can fix it and make it work, cdwhalley, i really appreciate it and thans again
|
|
|
|
04-17-2006, 03:38 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
look, u dont want static pages, use a variable for the offset (LIMIT $offset, $perpage)
$offset = (($page * $perpage) - $perpage) +1
|
|
|
|
04-17-2006, 03:40 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
heres mine (unedited)
PHP Code:
<?php if (!($page)){ $page = 1;} $offset = ($page - 1) * $games_per_page; $qstr = "SELECT DISTINCT g.gId AS game_id, g.gName AS game_name, g.gThumb AS game_thumb, g.gDescription AS game_description, c.cName AS category_name FROM games g LEFT JOIN categories c ON c.cId=g.gInCategory WHERE g.gInCategory = $cid ORDER BY gName ASC LIMIT $offset, $games_per_page"; $count = 0; $games = mysql_query($qstr); while (list ($gameid, $gamename, $thumb, $description, $catname) = mysql_fetch_row($games)){ ?> <?php if ($count == 2) { ?> <tr align="left" valign="top"> <?php } ?> <td align="center"><span class="normalText"> <?php if ($seo_support == 1){ echo "<a href=\"play-$gameid.html\">"; } else { echo "<a href=\"ply.php?id=$gameid\">";} ?> <img src="../images/upload/<?php echo"$thumb"; ?>" alt="<?php echo"$gamename"; ?>" border="0" height="60" width="60"></span></td> <td class="normalText" width="33%"> <b> <?php if ($seo_support == 1){ echo "<a href=\"play-$gameid.html\">"; } else { echo "<a href=\"ply.php?id=$gameid\">";} ?> <?php echo"$gamename"; ?> </b><br> <?php echo"$description"; ?><br> <?php if ($seo_support == 1){ echo "<a href=\"play-$gameid.html\" class=gameLink>"; } else { echo "<a href=\"ply.php?id=$gameid\" class=gameLink>";} ?> <b>Play now!</b> </td> <?php if ($count == 1) { ?> </tr> <?php } ?> <?php if ($count == 2) { $count = $count -1; }elseif ($count == 0){ $count = $count +1; }else { $count = $count +1; } } ?> </tbody> </table> <?php $numgames = mysql_query("SELECT * FROM `games` where `gInCategory` = '$cid'"); $numrows = mysql_num_rows($numgames); $ii = ceil($numrows / $games_per_page);
$iq = 1; if ($ii != 1) { while ($iq <= $ii) { if ($iq != $page) { echo "<a href="; echo $_SERVER['PHP_SELF']; echo "?page="; echo $iq; echo "&cid="; echo $cid; echo ">"; } echo "["; echo $iq; echo "]"; if ($iq != $page) { echo "</a>"; } echo " "; $iq++; } } ?>
|
|
|
|
04-17-2006, 03:50 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
your page doesn't write static pages right?
cause limiting them as dynamic pages, i was having the code inside to link between pages showing 10 results a page, and i dont want that i just want to save them as static pages, i am having many articles and the reason of saving them as static cause i dont want to overload the server, and want to try something different, writing them as static would be really helpful for me.
|
|
|
|
04-17-2006, 03:58 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
i know cdwhalley made it right and i am doing something wrong, it might be one character missing, cause the code looks ok to me
|
|
|
|
04-17-2006, 04:00 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 75
|
I think we need to establish what you want better.
You have a database containing articles, but you don't want all 40 to be displayed on 1 page when a user searches for sone. You want a page that lists the first 10, and then links to a page that shows the next 10 etc. until the user has seen all the articles.
Is this right? I've written the code for a page but I'm not sure if it answers what you want. I've made a few presumptions in it: - That you a user has already searched for an article title. I pretended they searched for 'Anything', although it is not a proper 'search' because the headline of the article has to match exactly what the searched for.
- That the articles table in your database contains the fields id, headline, author and bodytext.
- That it is ok for the results to all be on the one page, although a different url changes the results that will be shown.
This code has been done without testing it, so I'm not certain if it works...
PHP Code:
<?php $searchquery=@$_POST['search'];//submitted from a previous page in a search form - used an @ because the search doesnt exist //in this case we will say they searched for: anything $searchquery="anything";//this line is to make a fake search
$host="localhost";//mysql server host $user="root";//mysql username $password="";//mysql password $database="database";//mysql database name $connection=mysql_connect($host,$user,$password) or die("Unable to connect to database. <br/>MySQL error:<I>".mysql_error()."</i>"); $db=mysql_select_db($database,$connection) or die("Unable to select database. <br/>MySQL error:<i>".mysql_error()."</i>");
$firstquery="SELECT id FROM articles"; $firstresult=mysql_query($firstquery); $totalarticles=mysql_num_rows($firstresult);//get the total number of articles
if(!$_GET['page']) {//if we are on the first page, display just the first 10 articles $start=0;//the database row we start from $query = "SELECT * FROM articles WHERE headline='$searchquery' ORDER BY id LIMIT $start,10";//this would give the first 10 articles where the headline = anything $result = mysql_query($query); while($row=mysql_fetch_array($result) {//loop through the first 10 articles and output html for them extract($row); echo $headline." written by ".$author."<br/>".$bodytext."</br>"; } echo "<a href=articledisplay.php?page=10>Next 10 articles</a>";//output a link for the next page (articledisplay.php is the name of this page) } else {//find out what page we are on and show the correct articles $start=$_GET['page']; $query = "SELECT * FROM articles WHERE headline='$searchquery' ORDER BY id LIMIT $start,10";//this would give the next 10 articles where the headline = anything $result = mysql_query($query); while($row=mysql_fetch_array($result) {//loop through the next 10 articles and output html for them extract($row); echo $headline." written by ".$author."<br/>".$bodytext."</br>"; } $next=$start+10;//the next set of articles if($totalarticles <= $next) {//if there are no more articles to show after this page die();//don't output the link to the next page } else {//if there are more articles to show after this page echo "<a href=articledisplay.php?page=".$next.">Next 10 articles</a>";//output a link for the next page (articledisplay.php is the name of this page) } }
I've tried to make it as well-commented as possible, but I don't know how easily you'll be able to find your way through it. Like I said, I wrote this code in one go off the top of my head, and its un-tested, so I don't know if it will actually work - although I am expecting it to work, of course 
Last edited by cdwhalley.com; 04-17-2006 at 04:26 PM..
|
|
|
|
04-17-2006, 04:04 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
wow cdwhalley that's alot of help man. i can't find the words to thank you enough.
well i have them displayed the way i want them and i have written the real table names. i have trimmed my code here and didnt send all of it so i wont confuse you more, the only problem is with the loop fopen to write multiple files 10 in each page, i have made it work already to display 10 results in each page from the sql server, with next and previous link using "select count as numrows" but the problem here that i want them to be saved with different names, something looks like the code u sent before. but dunno why it didnt work
|
|
|
|
04-17-2006, 04:31 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
i am going to try it now, man you're being a life saver even if i didn't try it yet but i appreciate your time and ur help, you did an excellent job, and sorry for all the headaches, it's still my 2nd week in php and mysql, i have never worked with it before, that's why ive been bit slow maybe. thanks again cdwhalley, you're the best.
|
|
|
|
04-17-2006, 04:35 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
ah. what you sent is where i started lol i had this as i told you and i sent you a trimmed piece of the code, i can display the results 10 in each page with next and previous link and working perfect, the only problem i had since i started my post is to write them to static pages and not like that lol
anyways thank you whalley 
|
|
|
|
04-19-2006, 12:06 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 12
|
actually i fixed the problem myself, thanks for every1 who tried to help, now the script is writing each 10 rows in a different page and linking them together.
|
|
|
|
04-22-2006, 07:34 PM
|
Re: Loop File writing to generate files automatically
|
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
|
If you want static pages why use a dynamic language?
Static pages - HTML
Dynamic Pages - PHP
|
|
|
|
|
« Reply to Loop File writing to generate files automatically
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|