...

View Full Version : help me using the 'while' command



Dhuan
03-29-2009, 12:38 AM
There's this file called movie_genres.php

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:


<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:


$genres=file_get_contents("movie_genres.php");


then what?

bdl
03-29-2009, 12:47 AM
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 (http://www.php.net/langref). Note the section on 'control structures (http://us2.php.net/manual/en/language.control-structures.php)'.

Note: file_get_contents() (http://www.php.net/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() (http://us2.php.net/manual/en/control-structures.foreach.php) loop construct to iterate through it.

Dhuan
03-29-2009, 12:52 AM
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.

bdl
03-29-2009, 01:19 AM
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 (http://www.google.com/search?&q=rdbms%20first%20normal%20form).

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

ShaneC
03-29-2009, 01:21 AM
I think a foreach loop will work better for you than a while loop. Here's the 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:



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!

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.

Dhuan
03-29-2009, 02:28 AM
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'?

bdl
03-29-2009, 03:36 AM
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" (http://www.google.com/search?hl=en&q=SQL+JOIN+tutorial&btnG=Google+Search&aq=f&oq=) | "what is an RDBMS" (http://www.google.com/search?hl=en&q=what+is+an+RDBMS&btnG=Search) | "database normalization" (http://www.google.com/search?&q=database%20normalization)

MySQL Manual (http://dev.mysql.com/doc/refman/5.1/en/)
--Tutorial (ttp://dev.mysql.com/doc/refman/5.1/en/tutorial.html)
----Using Foreign Keys (http://dev.mysql.com/doc/refman/5.1/en/example-foreign-keys.html)
--SQL Statement Syntax (http://dev.mysql.com/doc/refman/5.1/en/sql-syntax.html) : SELECT syntax (http://dev.mysql.com/doc/refman/5.1/en/select.html)
----JOIN syntax (http://dev.mysql.com/doc/refman/5.1/en/join.html)
(assumes of course you're using MySQL, but it's still relevant)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum