SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A Database or Just a File

    First of all I don't have much PHP and Database experience. I am going to code a custom PHP script for my game site.

    It basically gets a game ID and checks it in the list of games (name and src name fields) and creates the html code, that's all. Maybe some extra lines of error handling..

    As I am a newbee on both sides (PHP and database), is it possible to handle the situation by just using a text file as we read data in C etc. and skip to learn SQL for the moment?

    I can easily find it myself but, I would appreciate to have a suggested tutorial link on PHP I/O.

    Thanks.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    text file

    If there isn't that much info to go through, you could use PHP's file functions with a CSV text file easily enough. The examples in the documentation should suit as a "tutorial".

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by glenngould
    First of all I don't have much PHP and Database experience. I am going to code a custom PHP script for my game site.

    It basically gets a game ID and checks it in the list of games (name and src name fields) and creates the html code, that's all. Maybe some extra lines of error handling..

    As I am a newbee on both sides (PHP and database), is it possible to handle the situation by just using a text file as we read data in C etc. and skip to learn SQL for the moment?

    I can easily find it myself but, I would appreciate to have a suggested tutorial link on PHP I/O.

    Thanks.
    you could also use dom / xml (the irony of "dom" will hit you when you start working with it), although it isn't the easiest thing to learn how to apply.

    think through this, though. *if* you will be adding features in the future that may require logins fore each user, a db might be better. for this app, though, a text / xml file will suffice unless you have a gazillion pieces of data to move around.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    db or file

    Good point skeeterbug. AFAIK MySQL lets you import data from both CSV and XML files, so either would be a good choice that you could put into a database later if/when you needed to.

  5. #5
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I may better use a MYSQL database for possible future improvements.
    If I am right, there must be no real difference; to learn to read from a text file or an SQL database, with PHP.

    There are free MYSQL tools that almost any webhost offers in their cpanel. Learning how to use it (at least a little) may be good for me. What do you think?
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    text - xml - db

    Either way you'll have to learn some PHP. Either the file functions, the parser functions, or the mysql functions. If you're short of time, I'd go with the CSV and change to MySQL later. But if you have the time, go with the MySQL. You can use the knowledge for other uses too, like for login info and a newsletter opt-in list.

  7. #7
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does $row stands for? it will always equal 1 (while echoing), am I missing smt. here? How to tell there are 3 set of records in the example below?

    01,gamename,gamesrc,
    02,gamename,gamesrc,
    03,gamename,gamesrc,

    Code:
    <?php
    $row = 1;
    $handle = fopen("test.csv", "r");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
       $num = count($data);
       echo "<p> $num fields in line $row: <br /></p>\n";
       $row++;
       for ($c=0; $c < $num; $c++) {
           echo $data[$c] . "<br />\n";
       }
    }
    fclose($handle);
    ?>
    My possible solution is to use a 2d array $data[recordno][fieldno]
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  8. #8
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    $row var

    Unless I'm not seeing something (very possible and even likely ) the code looks OK to me. Are you echo-ing $row inside the while? Do you have permissions set to read on the text.csv file?

  9. #9
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have no problem executing the script. i just don't get what is the purpose of $row.
    here is the .csv and the output:

    csv file:

    01,gamename1,gamesrc1,
    02,gamename2,gamesrc2,
    03,gamename3,gamesrc3


    output:


    9 fields in line 1:


    01
    gamename1
    gamesrc1
    02
    gamename2
    gamesrc2
    03
    gamename3
    gamesrc3


    is there a specific newline character to use in a .csv after each row?
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  10. #10
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    csv

    At first I though it might have to do with the fgetcsv line length being set to 1000. It only has to be enough to deal with the longest line. But the problem is that you ended each line with a comma so the file is being treated as one long line. Each line should end without the comma. Try a test.csv file like this
    Code:
    01,gamename1,gamesrc1
    02,gamename2,gamesrc2
    03,gamename3,gamesrc3

  11. #11
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is what i get:

    csv:

    01,gamename1,gamesrc1
    02,gamename2,gamesrc2
    03,gamename3,gamesrc3


    output:

    7 fields in line 1:


    01
    gamename1
    gamesrc1 02
    gamename2
    gamesrc2 03
    gamename3
    gamesrc3
    Last edited by glenngould; Apr 15, 2006 at 09:03.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  12. #12
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At this point, I have still no idea on how to recognize a linebreak in a .csv file but I don't care much. I've finished my first code. Please have a look.

    Anything to improve / simplify the code? Any security risks? It simply reads from a database of games. The data structure of the .csv file is:

    gameid,gamename,gamesrc,gamewidth,gameheight

    PHP Code:

    <?php
    define
    ("total_fields",    5);

    define("id_field",    0);
    define("name_field",    1);
    define("src_field",    2);
    define("width_field",    3);
    define("height_field",    4);

    $game=$_GET['game'];


    $handle fopen("database.csv""r");
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
        
         
    $game_name $data[(($game-1)*total_fields)+name_field];
        
    $game_src $data[(($game-1)*total_fields)+src_field];
        
    $game_width $data[(($game-1)*total_fields)+width_field];
        
    $game_height $data[(($game-1)*total_fields)+height_field];
    }

    fclose($handle);

    setcookie("lastgame_name"$game_nametime()+60*60*24*30"/"".domain.com"0);

    setcookie("lastgame_src"$game_srctime()+60*60*24*30"/"".domain.com"0);

    ?>
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    csv display

    Quote Originally Posted by glenngould
    I have still no idea on how to recognize a linebreak in a .csv file
    the csv function recognises the separator - the ",", and hard line breaks. You don't need to put any <br/>, \r, or \n into the csv file, just hit enter in your editor.
    If you change your test file to
    PHP Code:
    <?php
    $row 
    1;
    $handle fopen("test.csv""r");
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
       
    $num count($data);
       echo 
    "<p> $num fields in line $row: <br /></p>\n";
       
    $row++;
       for (
    $c=0$c $num$c++) {
           echo 
    "Field $c$data[$c], ";
       }
    echo 
    "<br />";
    }
    fclose($handle);
    ?>
    the output display may better formatted to see what's going on.
    As far as security is concerned, as long as the file can't be written to, and it doesn't contain anything "sensitive" I wouldn't worry. If it's something that users could "cheat" with by altering the cookie, you may want to use mcrypt functions to encrypt/decrypt the cookie values.

  14. #14
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the csv function recognises the separator - the ",", and hard line breaks. You don't need to put any <br/>, \r, or \n into the csv file, just hit enter in your editor.
    Check my post #11, for what happens when I just hit enter after each record! It merges the last field on the 1st line with the first field on the 2nd line.

    Well, I just checked the .csv with notepad: It seems my editor puts another whitespace character instead of a newline(using "FirstPage")! I think that's why I get only 1 row! I'll check it soon.

    Btw, there is no need to detect the newlines in my case, so I'll skip that.
    Last edited by glenngould; Apr 16, 2006 at 10:10.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by glenngould
    Check my post #11, for what happens when I just hit enter after each record!
    Sorry, I thought that was a result of the display script, not the text editor. I haven't used Word or FrontPage to edit code for so long I didn't even think of all the problens they can cause when they're used as a script editor.

  16. #16
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have another problem. It was working ok but don't know what is happening now!

    I wanna use $game_name in the html part of the page; I get nothing!
    I tested with echo and it seems the value assigned to $game_name disappears just after the while loop terminates. (look at the code in post #12)
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  17. #17
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    POST and GET vars

    All POST and GET variables are strings. You may have to type cast them to ints. If you put this before the $handle line you should see the types and values
    PHP Code:
    var_dump(id_field);
    var_dump(name_field);
    var_dump(src_field);
    var_dump(width_field);
    var_dump(height_field);
    var_dump($game); 

  18. #18
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you that was not the problem but var_dump() function is very helpful.

    My problem is I get NULL value outside the while loop for $data. I need $data outside the loop!
    ($something=$data[x] not working also)

    PHP Code:

    <?php 
    $row 
    1
    $handle fopen("database.csv""r"); 
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) { 
       
    $num count($data); 
       echo 
    "<p> $num fields in line $row: <br /></p>\n"
       
    $row++; 
       for (
    $c=0$c $num$c++) { 
           echo 
    "Field $c$data[$c], "
       } 
    echo 
    "<br />"


    //LOOK HERE
    echo "Checking outside the loop:  $data[0] ";

    fclose($handle); 
    ?>
    Last edited by glenngould; Apr 16, 2006 at 16:33.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  19. #19
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    variable scope

    It doesn't have scope outside of the while, if you declare it ooutside of the while it should work OK.
    PHP Code:
    $data = array();
    while ((
    $data ...... 
    except it will have the value of the last pass through the while.
    Quote Originally Posted by glenngould
    My possible solution is to use a 2d array $data[recordno][fieldno]
    If you need all the values you may have to pass the values to another array to get the values outside of the while. maybe something like
    PHP Code:
    $game_value_array = array();
    $g_v_a_index_value 0;
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
     
    $game_value_array[$g_v_a_index_value][$game_name] = $data[(($game-1)*total_fields)+name_field];
     
    $game_value_array[$g_v_a_index_value][$game_src] = $data[(($game-1)*total_fields)+src_field];
     
    $game_value_array[$g_v_a_index_value][$game_width] = $data[(($game-1)*total_fields)+width_field];
     
    $game_value_array[$g_v_a_index_value][$game_height] = $data[(($game-1)*total_fields)+height_field];
    $g_v_a_index_value += 1;

    and then access them with
    $game_value_array[index #][field value]

  20. #20
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, now I have all the information to build it correctly! My older version was somehow working, because all data was merged in 1 row, so $data has all fields (still don't know how I managed the scope issue).

    If you need all the values you may have to pass the values to another array
    Actually I need only 1 set of fields, not all.

    PHP Code:
    //we don't have to use
    //$data[(($game-1)*total_fields)+name_field]
    //as we have one row in $data each time

        
    if ($row==$game) {
        
    $g_name $data[name_field];
        
    $g_src $data[src_field];
        
    $g_width $data[width_field];
        
    $g_height $data[height_field];
        break;
        
        } 

    Anyway, thank you for the great help!!
    Last edited by glenngould; Apr 16, 2006 at 15:50.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  21. #21
    SitePoint Guru glenngould's Avatar
    Join Date
    Nov 2005
    Posts
    661
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the ones tracking this thread, here is the (at last ) working code:

    PHP Code:
    <?php
    define
    ("total_fields",    7); //no use for now

    define("id_field",    0);
    define("name_field",    1);
    define("src_field",    2);
    define("width_field",    3);
    define("height_field",    4);
    define("bgcolor_field",    5);
    define("img_field",    6);

    $game=$_GET['game'];

    $g_name='';
    $g_src='';
    $g_width=0;
    $g_height=0;
    $g_bgcolor='';
    $g_img='';
    $g_link='';

    $row 1;
    $handle fopen("database.csv""r");
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {
        
    $num count($data);
        
        
        if (
    $row==$game) {
        
    $g_name $data[name_field];
        
    $g_src $data[src_field];
        
    $g_width $data[width_field];
        
    $g_height $data[height_field];
        
    $g_bgcolor$data[bgcolor_field];
        
    $g_img"images/games/" $data[img_field] . ".gif";
        
    $g_link=  "playgame.php?game=" $data[id_field];
        break;
        
        }
        
        
    $row++; 
        
    }

    fclose($handle);

    ?>
    don't know why use "!==" instead of "!=" is it the proper php syntax? must mean inverse of "==" like: "!(==)"
    Last edited by glenngould; Apr 16, 2006 at 17:18.
    Tweep List adds an avatar menu to Twitter (open source)
    Word Stats shows your most used words on Twitter

  22. #22
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    equal

    The PHP docs explain it better than I can. Basically, with 2 it's "equal to", and 3 it's "identically equal to" (same type). So !== means "not identically equal to".
    http://us2.php.net/manual/en/languag...comparison.php


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •