Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts

    help me using the 'while' command

    There's this file called movie_genres.php
    Code:
    Action
    Drama
    Crime
    Comedy
    Romance
    What I want to do is, turn each of these genres into a single string($) and use 'while' to display them as checkboxes (html form).

    So in the HTML they would look like:

    Code:
    <input type=checkbox name="nameofthegenre">Name of the genre<br>
    <input type=checkbox name="nameofthegenre">Name of the genre<br>
    <input type=checkbox name="nameofthegenre">Name of the genre<br>
    <input type=checkbox name="nameofthegenre">Name of the genre<br>

    The first thing i know i'll have to do is, include this file in a single string, right?
    Like:
    Code:
    $genres=file_get_contents("movie_genres.php");
    then what?

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Hmm. You have a file named 'movie_genres.php' that contains strings like that? Why? I'd think you'd want to have a flat file (plain text file) or database with the genres that you can pull from and loop through. A PHP script should contain PHP., plain text files should contain text, etc. Even XML would be a better way to go here.

    As far as the while() loop construct is concerned, you can read about it in the very thorough PHP Language Reference. Note the section on 'control structures'.

    Note: file_get_contents() returns a string from the file it reads in. It would be more efficient to read the file in with file(), returning the data as an array and use the foreach() loop construct to iterate through it.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts
    I think it's pointless to create a table in the DB only for the genres.

    Here's how's my table:

    |movie_id|movie_name|genre|
    I know that there are easier methods for doing this, but i want to make a loop for each name genre that is written on that movie_genres.php file.

  • #4
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    I think it's pointless to create a table in the DB only for the genres.
    Absolutely not.

    |movie_id|movie_name|genre|
    Wait, so you actually have a `genre` field in the table? Then it's simple, you need to normalize that table and create a separate `genre` table to store the genres, and have the PK field of that table linked into your `movie` table. Otherwise you've got redundant data that breaks 1NF.

    Once you get that table setup, pull down and loop through the genre names stored in it.

  • #5
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    660
    Thanks
    31
    Thanked 100 Times in 94 Posts
    I think a foreach loop will work better for you than a while loop. Here's the code:

    PHP Code:
    <?php

    $genresFile 
    file_get_contents"movie_genres.php" );

    // Now that we've got the file data, put all the entires into an array seperated by the new line (\n) character
    $genresArray explode"\n"$genresFile );

    // The above line will make our array look something like this: $genresArray = array( 0 => "First line genre", 1 => "Second line genre" ); etc.

    foreach( $genresArray as $genre ){

         
    // This will go through each entry of our array and produce the correct result in output
         
    echo( "<input type='checkbox' name='" $genre"'>" $genre "<br>" );

    }

    ?>
    The foreach loop takes each value of the array and then uses the $genre variable to use it in that circumstance.

    If you're dead set on using a while loop you'll have to make the first line of your code the number of following lines in the sequence.

    For example:

    Code:
    4
    Horror
    Sci-Fi
    Action
    Romance
    That way the while loop knows how many iterations to go through.

    Personally, I think the foreach(){} loop is your best bet in this circumstance.

    Hope this helps!

    Edit: Recommendation: I do agree with bdl, however. You should use a database table whenever possible. It is both more secure and more dynamic. The most common use of using files to store data that I've seen are either deprecated coding methods or college students learning CS.
    Last edited by ShaneC; 03-29-2009 at 01:24 AM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #6
    Regular Coder
    Join Date
    Mar 2009
    Posts
    120
    Thanks
    13
    Thanked 3 Times in 3 Posts
    ShaneC, that code worked like a charm! I've never tried that 'explode' command, very intersting.
    Thanks

    Wait, so you actually have a `genre` field in the table? Then it's simple, you need to normalize that table and create a separate `genre` table to store the genres, and have the PK field of that table linked into your `movie` table. Otherwise you've got redundant data that breaks 1NF.

    Once you get that table setup, pull down and loop through the genre names stored in it.

    bdl
    ,
    PK Field? You mean, PRIMARY KEY? Shouldnt PRIMARY KEY be the movie_id colunm ?? I didnt know it was possible to link tables, can you indicate me a link where i can learn about this 'table linking'?

  • #7
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    PK Field? You mean, PRIMARY KEY? Shouldnt PRIMARY KEY be the movie_id colunm ?? I didnt know it was possible to link tables, can you indicate me a link where i can learn about this 'table linking'?
    RDBMS = Relational Database Management System

    Your tables should all be linked. That's the entire point to using a relational database. Yes, I mean Primary Key

    Ideally you'd have two tables: `movie`, `genre`. The `genre` table PK field is a FK (Foreign Key) in the `movie` table, e.g.

    `movie`
    movie_id (PK)
    genre_id (FK --> genre.genre_id)
    title
    year_released
    ...

    `genre`
    genre_id (PK)
    name
    ...


    "SQL JOIN tutorial" | "what is an RDBMS" | "database normalization"

    MySQL Manual
    --Tutorial
    ----Using Foreign Keys
    --SQL Statement Syntax : SELECT syntax
    ----JOIN syntax
    (assumes of course you're using MySQL, but it's still relevant)
    Last edited by bdl; 03-29-2009 at 03:39 AM. Reason: Added a couple of links


  •  

    Posting Permissions

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