Page 1 of 1

On the right track?

Posted: Wed Nov 05, 2008 5:31 am
by zenix
Hi, yesterday I posted a problem I am having with calculating values and several people attempted to help and/or understand me. Thank you for your efforts.
I wonder if I might have the beginnings of an idea (but I'm too new at this to know for sure). The equation is correct, but it's supposed to be getting information from a different file. There is no reference to that file nor the fields of interest. Below is what I have:

function calculate_differences($movie_earnings,$movie_cost)

{
$difference = $movie_earnings - $movie_cost;
//if($difference == 0)
if($difference > 0)
{
$difference = substr($difference,1);
$font_color ='blue';
$profit_or_loss = "$".$difference."m";
}
elseif($difference == 0)
{
$font_color ='green';
$profit_or_loss = "$".$difference."m";
}
else
{
$font_color ='red';
$profit_or_loss = "$".$difference."m";
}
return "<font color='$font_color'>" . $profit_or_loss . "</font>";

}

.... Then I reference the equation later on:

$movie_health = calculate_differences($movie_earnings, $movie_cost);

The data is in another file named alter_movie.php. a dataset (if that's the correct word) looks like this:

//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=131,
movie_cost=60,
movie_earningss=157
WHERE movie_id = 3";
$results = mysql_query($update)
or die(mysql_error());

At the top of this file I have declared my vars:

//alter "movie" table to include running time/cost/takings fields
$add = "ALTER TABLE movie ADD COLUMN (
movie_running_time int NULL,
movie_cost int NULL,
movie_earnings int NULL)";
$results = mysql_query($add)
or die(mysql_error());

There is no mention of the vars being used in the equation anywhere except in the equation itself. Any helpful ideas would be very much appreciated. Thank you again for your time. Maybe I don't know what I am doing (yup, but I'm learning) but it seems to be that there should be a call to the variables in the other file. Right?

Re: On the right track?

Posted: Wed Nov 05, 2008 8:20 am
by aceconcepts
Why are you using substr() on an integer value?

Re: On the right track?

Posted: Wed Nov 05, 2008 8:30 am
by zenix
Because that is what is in the sample code. Is that posibly why I am not getting the output I'd like? I'd like to have how much a movie made/lost posted in the table I have.

Re: On the right track?

Posted: Wed Nov 05, 2008 8:39 am
by aceconcepts
I don't know if that's what's causing your problem. However, take a look at the PHP manual for substr() http://uk2.php.net/substr

So, what is your objective - explain it in one line.

Re: On the right track?

Posted: Wed Nov 05, 2008 8:41 am
by zenix
I'd like to calculate what a movie made/lost and have it entered into a table I have. Most the other fields populate correctly, just not this one and length of movie.

Re: On the right track?

Posted: Wed Nov 05, 2008 8:44 am
by aceconcepts
Ok. So you are having problems with profit/loss and length.

What are the data types for these two fields in your database?

Re: On the right track?

Posted: Wed Nov 05, 2008 8:46 am
by zenix
They are both integers, this is what the info. looks like in the file the information resides.

$update="UPDATE movie SET
movie_running_time=118,
movie_cost=280,
movie_earnings=137
WHERE movie_id = 1";
$results = mysql_query($update)
or die(mysql_error());


Also, thank you for the link, I book marked it. I think I'll be referring to it a lot.

Re: On the right track?

Posted: Wed Nov 05, 2008 8:48 am
by aceconcepts
The PHP manula is extremely helpful - use it.

So, both those fields are defined as INT in your database yeh?

Re: On the right track?

Posted: Wed Nov 05, 2008 8:51 am
by zenix
yeah, like this:

$add = "ALTER TABLE movie ADD COLUMN
( movie_running_time int NULL,
movie_cost int NULL,
movie_earnings int NULL)";
$results = mysql_query($add)
or die(mysql_error());

Re: On the right track?

Posted: Wed Nov 05, 2008 9:20 am
by aceconcepts
Have you tested the values you pass to your function?

Re: On the right track?

Posted: Thu Nov 06, 2008 3:51 pm
by zenix
Thank you for all your time! I think the problem is that I'm not referencing the data file correctly. I'm working on learning how to do that right now. The example I was given apparently has errors in it. With me being so ignorant with this it makes it kind of difficult to fully understand where I need to modify the code. All the vales appear good, the only trouble seems to be when I attempt arithmetic. If you wouldn't mind too much (more time) I could post the two or three files of code here. Maybe your expertise could see the likely multitude of problems with it and you could suggest an idea or two. That'd be neat, I'm becoming a bit disheartened with this whole thing, and I REALLY would like to learn it. Slowly at first of course. They are only about 3K each. If you'd prefer I not, I fully understand. You've probably got much better things to do than "babysit" a budding coder.

Re: On the right track?

Posted: Thu Nov 06, 2008 5:34 pm
by aceconcepts
Feel free to post anything.

Don't get disheartened about it. You're always going to run into "issues" - everyone does. The more the difficult the problem, the sweeter it feels when you find a solution.

Just never give up :D

Re: On the right track?

Posted: Fri Nov 07, 2008 5:12 am
by zenix
Thanks a lot Ace! The history is that I read the chapter in a book and felt pretty confident that I understood it all, it's still pretty basic stuff. I put together the code in the book feeling confident enough to make a few minor changes and no matter what it wouldn't work. To understand better why not I changed the code to match the book exactly and still nothing. I finally relented and went to the web site and copied and pasted the sample code into the editor....guess what! Still not working.

This is the data file code.


<?php
//Connect to mysql
$connect = mysql_connect ("localhost", "root")
or die ("Check the connection again");

//Make sure using correct database
mysql_select_db("moviedb1");

// insert data into table

$insert = "INSERT INTO movie (movie_id, movie_name, movie_type, " .
"movie_year, movie_leadactor, movie_director) " .
" VALUES (NULL, 'Lethal Weapon', 7, 1987, 1, 2), " .
"(NULL, 'Die Hard', 7, 1988, 3, 4), " .
"(NULL, 'Days of Thunder', 7, 1990, 5, 6), " .
"(NULL, 'Titanic', 2, 1997, 7, 8), " .
"(NULL, 'Real Genius', 5, 1985, 9, 10), " .
"(NULL, 'Meatballs', 5, 1979, 11, 12), " .
"(NULL, 'Nightmare on Elm Street', 6, 1984, 13, 14), " .
"(NULL, 'Smokey and the Bandit', 3, 1977, 15, 16), " .
"(NULL, 'The Others', 2, 2001, 17, 18), " .
"(NULL, 'Twister', 7, 1996, 19, 20)";
$results = mysql_query($insert)
or die(mysql_error());

// insert data into movie type table
$type = "INSERT INTO movietype (movietype_id, movietype_label) " .
"VALUES (NULL, 'Sci Fi'), " .
"(NULL, 'Drama'), " .
"(NULL, 'Adventure'), " .
"(NULL, 'War'), " .
"(NULL, 'Comedy'), " .
"(NULL, 'Horror'), " .
"(NULL, 'Action'), " .
"(NULL, 'Kids')" ;
$results = mysql_query($type)
or die(mysql_error());

//Insert data into people table
$people = "INSERT INTO people (people_id, people_fullname, " .
"people_isactor, people_isdirector) " .
"VALUES (NULL, 'Mel Gibson', 1, 0), " .
"(NULL, 'Richard Donner', 0, 1), " .
"(NULL, 'Bruce Willis', 1, 0), " .
"(NULL, 'Len Wisemen', 0, 1), " .
"(NULL, 'Tom Cruise', 1, 0), " .
"(NULL, 'Tony Scott', 0, 1), " .
"(NULL, 'Leonardo Decaprio', 1, 0), " .
"(NULL, 'James Cameron', 0, 1), " .
"(NULL, 'Val Kilmer', 1, 0), " .
"(NULL, 'Martha Coolidge', 0, 1), " .
"(NULL, 'John Baushi', 1, 0), " .
"(NULL, 'Ivan Reitman', 0, 1), " .
"(NULL, 'Robert Englund', 1, 0), " .
"(NULL, 'Wes Craven', 0, 1), " .
"(NULL, 'Burt Reynolds', 1, 0), " .
"(NULL, 'Hal Needham', 0, 1), " .
"(NULL, 'Nichole Kidman', 1, 0), " .
"(NULL, 'Alejandro Amenábar', 0, 1), " .
"(NULL, 'Bill Paxton', 1, 0), " .
"(NULL, 'Jan De Bont', 0, 1)";
$results = mysql_query($people)
or die(sql_error());

echo "Data inserted successfully!";
?>

This is the code for altering the table already made to include more info.
<?php
$link = mysql_connect("localhost", "root")
or die(mysql_error());

mysql_select_db("moviedb1")
or die(mysql_error());

//alter "movie" table to include running time/cost/takings fields
$add = "ALTER TABLE movie ADD COLUMN (
movie_running_time int NULL,
movie_cost int NULL,
movie_earnings int NULL)";
$results = mysql_query($add)
or die(mysql_error());

//
//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=118,
movie_cost=280,
movie_earnings=137
WHERE movie_id = 1";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=102,
$movie_cost=10,
$movie_earnings=5
WHERE movie_id = 2";
$results = mysql_query($update)
or die(mysql_error());

//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=131,
$movie_cost=60,
$movie_earningss=157
WHERE movie_id = 3";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=194,
$movie_cost=200,
$movie_earnings=1,848,
WHERE movie_id = 4";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update = "UPDATE movie SET
movie_running_time=108,
$movie_cost=1,
$movie_earnings=12
WHERE movie_id = 5";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=99,
$movie_cost=1,600,
$movie_earningss=43,046
WHERE movie_id = 6";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=91,
$movie_cost=1,800,
$movie_earnings=25,504
WHERE movie_id = 7";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=96,
$movie_cost=1,
$movie_earnings=126
WHERE movie_id = 8";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=104,
$movie_cost=17,
$movie_earnings=209
WHERE movie_id = 9";
$results = mysql_query($update)
or die(mysql_error());


//insert new data into "movie" table for each movie
$update="UPDATE movie SET
movie_running_time=113,
$movie_cost=92,
$movie_earnings=494
WHERE movie_id = 10";
$results = mysql_query($update)
or die(mysql_error());


?>



And this is the code for the page that brings it all together.

<?php

$link = mysql_connect("localhost", "root")
or die(mysql_error());

mysql_select_db("moviedb1")
or die(mysql_error());


/* Function to calculate if a movie made a profit,
loss or broke even */

function calculate_differences($movie_earnings,$movie_cost)

{
$difference = $movie_earnings - $movie_cost;

if($difference > 0)
{
$difference = substr($difference, -0);
$font_color ='blue';
$profit_or_loss = "$".$difference."m";
}
elseif($difference == 0)
{
$font_color ='green';
$profit_or_loss = "$".$difference."m";
}
else
{
$font_color ='red';
$profit_or_loss = "$".$difference."m";
}
return "<font color='$font_color'>" . $profit_or_loss . "</font>";

}

//Function to get directors name from the people table
function get_director()
{
global $movie_director;
global $director;

$query_d = "SELECT people_fullname
FROM people
WHERE people_id='$movie_director' ";
$results_d = mysql_query($query_d) or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract ($row_d);
$director = $people_fullname;
}
//function get actors name from people table
function get_leadactor()
{
global $movie_leadactor;
global $leadactor;

$query_a = "SELECT people_fullname
FROM people
WHERE people_id='$movie_leadactor'";
$results_a = mysql_query($query_a) or die (mysql_error());
$row_a = mysql_fetch_array($results_a);
extract ($row_a);
$leadactor = $people_fullname;
}

$query = "SELECT * FROM movie WHERE movie_id='" .
$_GET['movie_id']."'";
$result = mysql_query($query, $link) or die (mysql_error());

$movie_table_headings=<<<EOD
<tr>
<th> Movie Title</th>
<th> Year of Release</th>
<th> Movie Director</th>
<th> Movie Lead Actor</th>
<th> Movie Running Time</th>
<th> Movie Health</th>
</tr>
EOD;

while ($row = mysql_fetch_array($result))
{
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_earnings = $row['movie_earnings'];
$movie_cost = $row['movie_cost'];

//get director's name from people table
get_director($movie_director);

//get lead actor's name from people table
get_leadactor($movie_leadactor);

}


$movie_health = calculate_differences($movie_earnings, $movie_cost);
$page_start=<<<EOD
<HTML>
<head>
<title>Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;
//global $profit_or_loss = $movie_health;
$movie_details =<<<EOD
<table width='70%' border='2' cellspacing='2' cellpadding='2' align='center'>
<tr>
<th colspan='6'><u><h2>$movie_name: Details</h2></u></th>
</tr>
$movie_table_headings
<tr>
<td width='33%' align='center'>$movie_name</td>
<td align='center'>$movie_year</td>
<td align='center'>$director</td>
<td align='center'>$leadactor</td>
<td align='center'>$movie_running_time</td>
<td align='center'>$movie_health</td>

</tr>
</table>
<br />
<br />
EOD;


$page_end =<<<EOD
</body>
</html>
EOD;
$detailed_movie_info =<<<EOD
$page_start
$movie_details
$page_end
EOD;

echo $detailed_movie_info;
mysql_close();
?>

Thanks a lot, I appreciate your encouragement!

Re: On the right track?

Posted: Mon Nov 10, 2008 4:48 am
by aceconcepts
I think the problem lies in the following code:

Code: Select all

 
$query = "SELECT * FROM movie WHERE movie_id='" .
$_GET['movie_id']."'";
$result = mysql_query($query, $link) or die (mysql_error());
 
$movie_table_headings=<<<EOD
<tr>
<th> Movie Title</th>
<th> Year of Release</th>
<th> Movie Director</th>
<th> Movie Lead Actor</th>
<th> Movie Running Time</th>
<th> Movie Health</th>
</tr>
EOD;
 
while ($row = mysql_fetch_array($result))
{
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_earnings = $row['movie_earnings'];
$movie_cost = $row['movie_cost'];
 
//get director's name from people table
get_director($movie_director);
 
//get lead actor's name from people table
get_leadactor($movie_leadactor);
 
}
 
 
$movie_health = calculate_differences($movie_earnings, $movie_cost); 
 
In your query you are only querying for a single record/movie. You then use a while loop to get the data from the database!!

When you are getting a single record (made unique by it's ID) you can use mysql_fetch_array() to get that row's data instead of setting up a loop.

So, what you could try is:

Instead of using a while loop change it to mysql_fetch_array()

Code: Select all

 
//REPLACE
while ($row = mysql_fetch_array($result))
{
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_earnings = $row['movie_earnings'];
$movie_cost = $row['movie_cost'];
 
//get director's name from people table
get_director($movie_director);
 
//get lead actor's name from people table
get_leadactor($movie_leadactor);
 
}
 
//WITH
$row=mysql_fetch_array($result);
 
$movie_name = $row['movie_name'];
$movie_director = $row['movie_director'];
$movie_leadactor = $row['movie_leadactor'];
$movie_year = $row['movie_year'];
$movie_running_time = $row['movie_running_time']." mins";
$movie_earnings = $row['movie_earnings'];
$movie_cost = $row['movie_cost'];
 
//get director's name from people table
get_director($movie_director);
 
//get lead actor's name from people table
get_leadactor($movie_leadactor);
 
$movie_health = calculate_differences($movie_earnings, $movie_cost); 
 
Give that a whirl.

Re: On the right track?

Posted: Mon Nov 10, 2008 6:34 am
by zenix
Thanks an awful lot for all your help!! You are an absolute genius!!! Well, compared to me!