• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

fwrite

Originally posted by Wojtek
what size are you talking about?
6kb? 50kb? 500? 1Mb?
Front what size will it have some slowdowns?
That's not something anyone can answer since it depends a lot on the server.

I timed how long it took for a 7.5Kb file to read and reverse_sort, then a 75Kb file (which took twice as long), then a 750Kb (which took 40 times longer which I don't quite get).

My last post was more about "best practice" then anything. Even if you're going to be using a 1Mb flat file it doesn't mean your visitors will stare at a blank screen for 5 minutes, it's just not a good way to do it.

Learning how MySQL works really isn't that hard at all, it just takes some time and effort and it's just more efficient. Ultimatly it's up to you if you want to get into it or not.
 
Carebear is right. Its much more efficient for you to take the load then your visitors. You are one user, as apposed to thousands of users. And Wojtek My question still stands, why reinvent the wheel. your code allows you to add news, but what about editing, deleting, multi-users? Long term, downloading a script is far wiser, unless there are no scripts that do what you want them to do.
 
Originally posted by spec
Carebear is right. Its much more efficient for you to take the load then your visitors. You are one user, as apposed to thousands of users. And Wojtek My question still stands, why reinvent the wheel. your code allows you to add news, but what about editing, deleting, multi-users? Long term, downloading a script is far wiser, unless there are no scripts that do what you want them to do.
Im not using this script as a news script. But as a poem archive.
http://www.allpoem.com/poems/love/index.php = exemple

let le explain how im using it.

Each poem category has a folder. Ex: love, friendship, famous, etc
in each folder, i have 5 files:
addpoem.php <- script used to add poems
config.php <- config files
index.php <- includes ../../header.php | includes readpoems.php | includes ../../footer.php
poems.dat <- poems stored here
readpoems.php <- script that reads poems from poems.dat and prints them


"Its much more efficient for you to take the load then your visitors. You are one user, as apposed to thousands of users"
I understand what you mean, but i dont know where the problem is? is it in the script that ads the content? or reads the content?
 
Ok let me step back for a sec. My apologies, I dont know how but I missed the fact that you were the one asking for help. I thought you were giving advice..

Ok So lets recap. There have been two methods sugested. One where the data is is ordered when you run the addpoem script. the Next option is to order it when the data is requested.

Now I recommend you order the data when you add the poem. The downside to this is that the larger the file, the longer it will take.

If you did it the other way, although, you would still have the same issue; only the user would suffer the wait. Ontop of that if you have many users running the script at once it would add to the load to the server.

Using Mysql is another option. This is alot easier to do, because alot of the functions required to add, edit and delete data is already there. infact if you were the only one adding poems you could use phpmyadmin to do it and you wouldnt have to bother with any script but the [retrieving] end.

If your host has Mysql installed and/or phpMyAdmin this task would be much easier.
 
Originally posted by Wojtek
"Its much more efficient for you to take the load then your visitors. You are one user, as apposed to thousands of users"
I understand what you mean, but i dont know where the problem is? is it in the script that ads the content? or reads the content?
Just take a step and think of it as something that would happen in real life :).
You have a notepad file that you keep and add poems to. When you get a new poem you just open it and skip to the end and add the new poem. Same as with your file the oldest poem will be the first one and the most recent poem is that last one.
Since you're using notepad you don't have the concept of page numbers that you'd have in for instance Word.

So if someone asks you to see the most recent poem you hit print, take the stack of printed pages and then reshuffle them so the last page to come out of the printer is at the top, so showing the last poems at the top.
This isn't a particular good way to do it that is it? It's messy and slow since you have to print all of the page just to know where the last poem (or last 10 poems) will be.

Now you could do the same but instead of notepad you use Word. You put each poem on a seperate page so that if you have 50 pages you know you have 50 poems. To print the most recent poem you know you can just print out page 50.
Now since it's word you also have more options when printing so if you wanted to give someone the last 10 poems you'd print pages 41-50 and check the option to reverse the print order so the last poem is the first one to come out, saving you from having to shuffle them manually.
Now this way would save you a lot of time compared to how you'd do it in notepad right?

Compare the notepad example to the way you're doing it now with a .txt file and the Word example to how it would work with MySQL.
Did that help you understand what the problem is?

In the notepad example the bigger that file gets, the longer you'll be waiting for it all to be finished printing (which translates to loading the file into memory), where with Word you'd just put in which specific pages you want to print and it would always take the same amount of time.
 
Other advantages of using a database are that you could keep all of the poems in one place and just indicate which category they belong to.
If you keep track of who submitted the poem it's just as easy to ask the database: "show me all the poems that CareBear submitted" as it is to tell it: "give me the last 10 submitted poems" or "give me the last 10 submitted poems in the friendship category".
 
Ok, I've desided to go with mysql :)

I've already created the table format:

| Category | Title | Poem | Author | Score |


PHP:
<?include "header.php"?>

<?

$dbh=mysql_connect ("localhost", "wojtek_poems", "PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("wojtek_poems");
 
select Category, Title, Poem, Author, Score from wojtek_poems
where Category = 'Love Poems';

?>

<?include "footer.php"?>

By myself, thats all I can do right now :(

However, I look up for some next/previous links script so it only displays 1 poem at a time and next/prev links and I got that:

PHP:
<?include "header.php"?>

<?

$dbh=mysql_connect ("localhost", "wojtek_poems", "PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("wojtek_poems");

if(!isset($poem)) $poem = 0;

$query = "SELECT * FROM wojtek_poems where Category = 'Love Poems' LIMIT " . $poem . ", 1";
$result = mysql_query($query) or die ('I cannot connect to the database because: ' . mysql_error());

$query = "SELECT count(*) as count FROM table"; 
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$numrows = $row['count'];
if($poem > 0) 
   echo "<a href=\"" . $PHP_SELF . "?poem=" . ($poem - 1) . 
        "\">Previous</a><BR>\n";
if($numrows > ($poem + 1)) 
   echo "<a href=\"" . $PHP_SELF . "?poem=" . ($poem + 1) . 
        "\">Next</a><BR>\n";

?>

<?include "footer.php"?></p>

Does that code make sense? What should I replace/change?
How do I format the results?
Also, I'd like to have a score for each poems. each poem would start with 0 and users can add from +1 to +5 from the page the poem is displayed. And I'll add that number to the existing score filed in the database, this way I could do show top 10 rated poems.

Thanks :)
 
Last edited:
PHP:
// from first snippet
$query = "select Category, Title, Poem, Author, Score from wojtek_poems where Category = 'Love Poems'"; 
$result = mysql_query($query);

The second set I bet you downloaded. Its pretty shitty coding. If you link me to the page to see what it looks like (output) I can help you with that. Do you just want Previous, next buttons?
PHP:
//from second snippet

//this statement is pretty silly
$query = "SELECT * FROM wojtek_poems where Category = 'Love Poems' LIMIT " . $poem . ", 1";

You see double quotes are used when there is a variable or single quotes inside. So why are there double quotes and then you break out of the string for the variable.
 
I didnt upload yet the files as im having troubles creating the tables first :(

Code:
create table Poems
(Category varchar,
 Title varchar,
 Author varchar,
 Poem varchar,
 Score number,
 Comments varchar);
gives
Code:
Database wojtek_poems running on localhost 
Error

SQL-query :  

CREATE TABLE Poems(
Category varchar,
Title varchar,
Author varchar,
Poem varchar,
Score number,
Comments varchar
) 

MySQL said: 


You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near '
Title varchar,
Author varchar,
Poem varchar,
Score number,
Com

The 2nd part is a mix of my 1st part and http://codewalkers.com/tutorials/4/5.html
 
basically thats what I want to achieve:

---------------------------------------------------------------------
Script #1
- Read content from table: | Category | Title | Author | Poem | Score | Comments
- Display one database result at a time (next/prev buttons)

Script #2
- Add poems to the database
---------------------------------------------------------------------

To read the content from the database, I was thinking of passing arguments like poems.php?Category=Love and it would display one by one poems that match Love as Category. So love poem #5 would be poems.php?Category=Love&Poem=5


I tried to create the database using phpmyadmin and sql query, but I got an error:

---------------------------------------------------------------------
Database wojtek_poems running on localhost
Error

SQL-query :

CREATE TABLE Poems(
Category varchar,
Title varchar,
Author varchar,
Poem varchar,
Score number,
Comments varchar
)

MySQL said:


You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '
Title varchar,
Author varchar,
Poem varchar,
Score number,
Com
---------------------------------------------------------------------
 
Ok We'll work on this in 3 parts.
First the mysql. Your mysql syntax is wrong. you do not specify the lengths of each column or have an index. I ALWAYS use an auto increment index as it helps with searching. If you can create as many tables as you want I recommend seperating each "category" into seperate tables. That way there is less data to search. But here is the code:

Code:
CREATE TABLE `poems` (
`id` INT( 4 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,
`category` VARCHAR( 50 ) DEFAULT 'None' NOT NULL ,
`title` VARCHAR( 50 ) DEFAULT 'None' NOT NULL ,
`author` VARCHAR( 100 ) DEFAULT 'None' NOT NULL ,
`poem` TEXT NOT NULL ,
`score` INT( 5 ) NOT NULL ,
`comments` VARCHAR( 250 ) NOT NULL ,
UNIQUE (
`id` 
)
);
Notice I made an id, when inputing data NEVER fill this field in. It automatically fills itself in. Next I made peom a TEXT field. with varchar you have a maximum of 255 characters (including whitespace) so if you have a large poem it will not fit in a varchar field. and of course at the end I made id unique.

I recommend you seperate each category into tables. so instead of 'poems' you would use many tables like 'love'

Code:
CREATE TABLE `love` (
`id` INT( 4 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,
`category` VARCHAR( 50 ) DEFAULT 'None' NOT NULL ,
`title` VARCHAR( 50 ) DEFAULT 'None' NOT NULL ,
`author` VARCHAR( 100 ) DEFAULT 'None' NOT NULL ,
`poem` TEXT NOT NULL ,
`score` INT( 5 ) NOT NULL ,
`comments` VARCHAR( 250 ) NOT NULL ,
UNIQUE (
`id` 
)
);
 
Last edited:
I did what you suggested. 1 table/per category instaed of All in one table

------------------------------------------------------------------------
CREATE TABLE `love` (
`id` INT( 4 ) DEFAULT '0' NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 50 ) DEFAULT 'None' NOT NULL ,
`author` VARCHAR( 100 ) DEFAULT 'None' NOT NULL ,
`poem` TEXT NOT NULL ,
`score` INT( 5 ) NOT NULL ,
`comments` TEXT NOT NULL ,
UNIQUE (
`id`
)
);
------------------------------------------------------------------------


How do I add content into the fields using a script?
 
I came up with such code:

PHP:
<?php
if($Title) {
$Title=$_GET['Title'];

$dbh=mysql_connect ("localhost", "wojtek_poems", "PASSWORD") or die ('I cannot connect to the database because: ' . mysql_error()); 

mysql_select_db ("wojtek_poems"); 

insert into love
  (Author, AuthorEmail, Title, Cateory, Poem, Comment)
  values ($_GET['Author'], $_GET['AuthorEmail'], $_GET['Title'], $_GET['Category'], $_GET['Poem'], $_GET['Comment']);

echo "Poem Added";
}
 ?>



<?include "../header.php"?>

<font face="verdana" size="1">
<form method="get" action="AddPoem.php">
<p align="center"><b>Add a Poem</b></p>
<div align="center">
  <center>
  <table border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td>

<font face="verdana" size="1">Author:
</font>

      </td>
      <td>

<font face="verdana" size="1">
<input type="text" name="Author" size="50">
</font>

      </td>
    </tr>
    <tr>
      <td>

<font face="verdana" size="1">Author Email/Url:
</font>

        <p>&nbsp;</td>
      <td>

<font face="verdana" size="1">
<input type="text" name="AuthorEmail" size="50">
</font>

        <p>&nbsp;</td>
    </tr>
    <tr>
      <td>

<font face="verdana" size="1">
Poem Title:
</font>

      </td>
      <td>

<font face="verdana" size="1">
<input type="text" name="Title" size="50">
</font>

      </td>
    </tr>
    <tr>
      <td>

<font face="verdana" size="1">
Category:
</font>

      </td>
      <td>

<font face="verdana" size="1">
<select size="1" name="Category">
<option>Birthday</option>
<option>Childrens</option>
<option>Christmas</option>
<option>Easter</option>
<option>Famous</option>
<option>FathersDay</option>
<option>Friendship</option>
<option>Graduation</option>
<option>Humorous</option>
<option>Love</option>
<option>Marriage Poems</option>
<option>MothersDay</option>
<option>NewYears</option>
<option>Passover</option>
<option>Philosophical</option>
<option>Sonnets</option>
<option>ThankYou</option>
<option>ValentinesDay</option>
<option>Other</option>
</select>
</font>

      </td>
    </tr>
    <tr>
      <td>

<font face="verdana" size="1">
Poem:
</font>

      </td>
      <td>

<font face="verdana" size="1">
<textarea name="Poem" cols="50" rows="10"></textarea>
</font>

      </td>
    </tr>
    <tr>
      <td><font face="verdana" size="1">Comment:</font></td>
      <td>

<font face="verdana" size="1">
<input type="text" name="Comment" size="50">
</font>

      </td>
    </tr>
  </center>
  <center>
    <tr>
      <td colspan="2">
        <p align="center">&nbsp;

      </td>
    </tr>
    <tr>
      <td colspan="2">
        <p align="center">

<font face="verdana" size="1">
<input type="submit" value="  Add Poem  ">
</font>

      </td>
    </tr>
  </table>
  </center>
</div>
</form>
</font>

<?include "../footer.php"?>
Makes sense? Good coding?
 
sorry but no but close.

your mysql query is all wrong. please see below.
If you are allowing users to add poems then you will need alot of error checking, which I dont have added. If it is just you then you should be ok with maybe adding a login, or putting it in a passworded folder.
The first bit checks if action is set. action just checks to see if they submited. next the script checks for empty fields, mind you if you want users to submit poems your going to need to tighten security or you will get ALOT of abuse.
your mysql connection is good, and I added some really basic database adding. If this is going to be public PLEASE add some more security.


PHP:
<?php 
if(!empty($_GET['action'])) { 
if($_GET['$action'] != 'add') die('Please use the form to add');
if(!empty($_GET['Title'])) $title= $_GET['Title'];
if(!empty($_GET['Author'])) $author = $_GET['Author'];
if(!empty($_GET['AuthorEmail'])) $email = $_GET['AuthorEmail'];
if(!empty($_GET['Category'])) $cat = $_GET['Category'];
if(!empty($_GET['Poem'])) $poem =  $_GET['Poem'];
if(!empty($_GET['Comment'])) $comments = $_GET['Comment']; 

$dbh=mysql_connect ('localhost', 'wojtek_poems', 'PASSWORD') or die ('I cannot connect to the database because: ' . mysql_error());  

mysql_select_db ('wojtek_poems');  

$query = "insert into love 
  ( title, author,  poem, comments) 
  values ('$title', '$author', '$poem', '$comments')"; 
$result = mysql_query($query);

echo "Poem Added"; 
} 
 ?> 



<?include "../header.php"?> 


....

<form method="get" action="AddPoem.php?action=add">
 
update

PHP:
if(!empty($_GET['Title'])) $title= $_GET['Title']; 
else die('Please add a title');
if(!empty($_GET['Author'])) $author = $_GET['Author']; 
else die('Please add a author');
if(!empty($_GET['AuthorEmail'])) $email = $_GET['AuthorEmail']; 
else die('Please add a Author Email');
if(!empty($_GET['Poem'])) $poem =  $_GET['Poem']; 
else die('Please add a Poem');
if(!empty($_GET['Comment'])) $comments = $_GET['Comment'];  
else die('Please add Comments');
and you may notice that AuthorEmail isnt actually used since you never created that column in the table!

and another thought. Dont use Get for your method. Use Post. Imagine a URL with The Raven in it. Quote the Raven, Crap I think my computer just exploded.
 
Last edited:
I'll be the only one adding poems, that file will be in a password protected folder so its ok :)

I used AuthorEmail here because I droped the other table and created a new one with that field.

And what do you mean by using post?
Instead of
<form method="get" action="AddPoem.php?action=add">
use
<form method="post" action="AddPoem.php?action=add">
?
 
Is the syntax ok like that?

------------------------------------------------------------------
$query = "insert into " . $cat . "
(Author, AuthorEmail, Title, Poem, Comment)
values ('$author', '$email', '$title', '$poem', '$comment')";
------------------------------------------------------------------

So it checks what I've selected from the category dropdown and uses that as the table name into where to add.
 
Last edited:
yay!
It's working :)

Code:
<?php
if(!empty($_GET['Author'])) {  
if(!empty($_GET['Author'])) $author = $_GET['Author']; 
if(!empty($_GET['AuthorEmail'])) $email = $_GET['AuthorEmail']; 
if(!empty($_GET['Title'])) $title= $_GET['Title'];
if(!empty($_GET['Category'])) $cat = $_GET['Category']; 
if(!empty($_GET['Poem'])) $poem =  $_GET['Poem']; 
if(!empty($_GET['Comment'])) $comment = $_GET['Comment'];  

$dbh=mysql_connect ('localhost', 'wojtek_poems', 'password') or die ('I cannot connect to the database because: ' . 

mysql_error());   

mysql_select_db ('wojtek_poems');   

$query = "insert into " . $cat . "  
  (Author, AuthorEmail, Title, Poem, Comment) 
  values ('$author', '$email', '$title', '$poem', '$comment')";  
$result = mysql_query($query); 

echo "Poem Added";  
}  
 ?>  

<?include "header.php"?> 

<form method="get" action="test.php">
<form method="post" action="test.php?action=add"> didnt work.
It always displayed 'Please use the form to add' on a white page.

Oki, script #2 is working! :) now the main part....
Read from the database and print one entry at a time with prev/next buttons...
 
Back
Top