Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Loop File writing to generate files automatically
Old 04-17-2006, 10:42 AM Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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..
mafios is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-17-2006, 12:52 PM Re: Loop File writing to generate files automatically
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
What is the webpage. I would like some idea what this is producing.
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-17-2006, 12:55 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 01:03 PM Re: Loop File writing to generate files automatically
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
What exactly is not working?
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-17-2006, 01:49 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 02:01 PM Re: Loop File writing to generate files automatically
cdwhalley.com's Avatar
Skilled Talker

Posts: 75
Trades: 0
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..
cdwhalley.com is offline
Reply With Quote
View Public Profile Visit cdwhalley.com's homepage!
 
Old 04-17-2006, 02:30 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 03:05 PM Re: Loop File writing to generate files automatically
cdwhalley.com's Avatar
Skilled Talker

Posts: 75
Trades: 0
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.
cdwhalley.com is offline
Reply With Quote
View Public Profile Visit cdwhalley.com's homepage!
 
Old 04-17-2006, 03:16 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 03:38 PM Re: Loop File writing to generate files automatically
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
look, u dont want static pages, use a variable for the offset (LIMIT $offset, $perpage)

$offset = (($page * $perpage) - $perpage) +1
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-17-2006, 03:40 PM Re: Loop File writing to generate files automatically
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
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 
"&nbsp;";
                
$iq++;
            }
    }
   
?>
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Old 04-17-2006, 03:50 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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.
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 03:58 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 04:00 PM Re: Loop File writing to generate files automatically
cdwhalley.com's Avatar
Skilled Talker

Posts: 75
Trades: 0
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:
  1. 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.
  2. That the articles table in your database contains the fields id, headline, author and bodytext.
  3. 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..
cdwhalley.com is offline
Reply With Quote
View Public Profile Visit cdwhalley.com's homepage!
 
Old 04-17-2006, 04:04 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 04:31 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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.
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-17-2006, 04:35 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-19-2006, 12:06 PM Re: Loop File writing to generate files automatically
Novice Talker

Posts: 12
Trades: 0
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.
mafios is offline
Reply With Quote
View Public Profile
 
Old 04-22-2006, 07:34 PM Re: Loop File writing to generate files automatically
mad_willsy's Avatar
Super Spam Talker

Latest Blog Post:
R&R Catering Hire Testimonial
Posts: 805
Name: Will Craig
Location: Cheltenham, Gloucestershire, UK
Trades: 0
If you want static pages why use a dynamic language?

Static pages - HTML
Dynamic Pages - PHP
__________________
Wont :P

Please login or register to view this content. Registration is FREE
mad_willsy is offline
Reply With Quote
View Public Profile Visit mad_willsy's homepage!
 
Reply     « Reply to Loop File writing to generate files automatically
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53509 seconds with 12 queries