Jump to content

[SOLVED] ANOTHER if statement.


mcdof001

Recommended Posts

SO SORRY everyone. This is THE LAST error of my script. I have an if statemnt that is not working. What I would like to happen, is the rows from the mysql database being echoed if they have the same fitness rating as the person who filled in the form with a 2 point leaway. $rat is the variable for the person who entered the form. $ratin was pulled from the database. This is my source code:-

<?
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$poo  = "SELECT name, age, sex, exercise, music, fitness FROM aida";
$result = mysql_query($poo);
$rat = $_POST["rating"];
while($row = mysql_fetch_row($result))
{
    $name = $row[0];
    $age = $row[1];
    $sex = $row[2];
$exercise = $row[3];
$music = $row[4];
$ratin = $row[5];


if ($ratin == $rat) {
echo "Name :$name <br>" .
         "Age Group : $age <br>" . 
         "Sex : $sex <br><br>".
	 "Exercise (per week) : $exercise <br>" .
	 "Favourite Music : $music <br>" .
	 "Fitness Rating : $rating <br>" ; 
} elseif ($ratin == $rat + 1) {
echo "Name :$name <br>" .
         "Age Group : $age <br>" . 
         "Sex : $sex <br><br>".
	 "Exercise (per week) : $exercise <br>" .
	 "Favourite Music : $music <br>" .
	 "Fitness Rating : $rating <br>" ; 
} elseif ($ratin == $rat + 2) {
echo "Name :$name <br>" .
         "Age Group : $age <br>" . 
         "Sex : $sex <br><br>".
	 "Exercise (per week) : $exercise <br>" .
	 "Favourite Music : $music <br>" .
	 "Fitness Rating : $rating <br>" ; 
} elseif ($ratin == $rat - 1) {
echo "Name :$name <br>" .
         "Age Group : $age <br>" . 
         "Sex : $sex <br><br>".
	 "Exercise (per week) : $exercise <br>" .
	 "Favourite Music : $music <br>" .
	 "Fitness Rating : $rating <br>" ; 
} elseif ($ratin == $rat - 2) {
echo "Name :$name <br>" .
         "Age Group : $age <br>" . 
         "Sex : $sex <br><br>".
	 "Exercise (per week) : $exercise <br>" .
	 "Favourite Music : $music <br>" .
	 "Fitness Rating : $rating <br>" ; 
}
}
mysql_close();
?>
<body style="filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, EndColorStr='#003399', StartColorStr='#FFFFFF');">
</body>

Link to comment
Share on other sites

To many if/elses for such a simple thing.

 

$ok_vals = array(($rat - 2), ($rat - 1), $rat, ($rat + 1), ($rat + 2));

if (in_array($ratin,$ok_vals)) {
    echo "Name :$name <br>Age Group : $age <br>Sex : $sex <br><br>Exercise (per week) : $exercise <br>Favourite Music : $music <br>Fitness Rating : $rating <br>" ; 
}

Link to comment
Share on other sites

it still won't work! i think it does need to be in an array.

this is the edited code so far!

<?
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$poo  = "SELECT name, age, sex, exercise, music, fitness FROM aida";
$result = mysql_query($poo);
$rat = $_POST["rating"];
$ok_vals = array(($rat - 2), ($rat - 1), $rat, ($rat + 1), ($rat + 2));
while($row = mysql_fetch_row($result))
{
    $name = $row[0];
    $age = $row[1];
    $sex = $row[2];
    $exercise = $row[3];
    $music = $row[4];
    $rating = $row[5];
if (in_array($rating,$ok_vals)) {
    echo "Name :$name <br>Age Group : $age <br>Sex : $sex <br><br>Exercise (per week) : $exercise <br>Favourite Music : $music <br>Fitness Rating : $rating <br>" ; 
}
}
mysql_close();
?>
<body style="filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, EndColorStr='#003399', StartColorStr='#FFFFFF');">
</body>

Link to comment
Share on other sites

let's see what values we're looking at, if you like. add the else as below:

 

if (in_array($rating,$ok_vals)) {

    echo "Name :$name <br>Age Group : $age <br>Sex : $sex <br><br>Exercise (per week) : $exercise <br>Favourite Music : $music <br>Fitness Rating : $rating <br>" ;

} else { // Add this else

    echo "NOT in_array: ".$rating."<BR>";

    echo "Array: ";

    print_r($ok_vals);

}

 

Link to comment
Share on other sites

i didn't think so.

i might cancel that whole function in a minute!

it just won't work! the gradient background still comes up. and im sure i had an unexpected t_string error come up before. wierd. any other suggestions very welcome. i have to go. ill be on later. thanks for all help so far!

Link to comment
Share on other sites

I am going to post the whole of my script, if that might help.

Here is index.html, the form that the user submits the information through:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Quiz</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <style type="text/css">
<!--
.style1 {font-family: "Trebuchet MS"}
.style5 {font-family: "Trebuchet MS"; color: #FF0000; font-weight: bold; }
#Layer1 {
position:absolute;
width:200px;
height:115px;
z-index:1;
left: 469px;
top: 74px;
}
-->
</style>
   <meta content="MSHTML 6.00.6000.16525" name="GENERATOR" />
</head>
<body style="FILTER: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#003399', EndColorStr='#FFFFFF')">
   <form id="quiz" name="quiz" action="results.php" enctype="multipart/form-data">
     <table width="391" border="0">
       <tbody>
         <tr>
           <td width="131"><span class="style5">Name:</span></td>
           <td width="144">
            <input id="name" name="name" /></td>
        </tr>
         <tr>
           <td class="style5">Age Group: </td>
           <td><span class="style1">
              <select id="age" name="age"> 
                <option value="0-15">0-15</option> 
                <option value="16-30">16-30</option> 
                <option value="31-45">31-45</option> 
                <option value="46-60">46-60</option> 
                <option value="60+">60+</option>
              </select> </span></td>
        </tr>
         <tr>
           <td>
             <p class="style5">Email:</p></td>
           <td>
            <input id="email" name="email" /></td>
        </tr>
         <tr>
           <td><span class="style5">Sex:</span></td>
           <td><span class="style1">
              <select id="sex" name="sex"> 
                <option value="Male">Male</option> 
                <option value="Female">Female</option>
              </select> </span></td>
        </tr>
         <tr>
           <td><span class="style5">Exercise per week:</span></td>
           <td><span class="style1">
              <select id="exercise" name="exercise"> 
                <option value="0 Hours">Never</option> 
                <option value="1 Hours">Once</option> 
                <option value="2 Hours">Twice</option> 
                <option value="3 Hours">3 Times</option> 
                <option value="4 Hours">4 Times</option> 
                <option value="5 Hours Or More">5+ Times</option>
              </select> </span></td>
        </tr>
         <tr>
           <td><span class="style5">Are you good at dancing?</span></td>
           <td><span class="style1">
              <select id="dancing" name="dancing"> 
                <option value="Not A Very Good Dancer">I'm Not Very Good</option> 
                <option value="A Good Dancer">I'm Quite Good</option> 
                <option value="OK at Danicng">I'm OK</option>
              </select> </span></td>
        </tr>
         <tr>
           <td class="style5">What music do you like? </td>
           <td><span class="style1">
              <select id="music" name="music"> 
                <option value="Rock">Rock</option> 
                <option value="Heavy Metal">Heavy Metal</option> 
                <option value="Indie">Indie</option> 
                <option value="Rap">Rap</option> 
                <option value="Classical">Classical</option> 
                <option value="Dance">Dance</option> 
                <option value="Pop">Pop</option> 
                <option value="Jazz and Blues">Jazz and Blues</option> 
                <option value="Urban">Urban</option>
              </select> </span></td>
        </tr>
         <tr>
           <td><span class="style5">What do you think of parties? </span></td>
           <td><span class="style1">
              <select id="party" name="party"> 
                <option value="Loves Parties">I Love Parties</option> 
                <option value="Hates Parties">I Hate Parties</option> 
                <option value="Thinks Parties Are OK">I think Parties are OK</option>
              </select> </span></td>
        </tr>
      </tbody>
    </table>
     <table width="391" border="0">
       <tbody>
         <tr>
           <td>
             <div align="center">
              <input type="submit" value="Submit" name="Submit" /> </div></td>
        </tr>
      </tbody>
    </table>
     <p> </p>
  </form>
</body>
</html>

Here is results.php which redirects them to compare.php through a button whilst passign variables:-

<?
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$name = $_GET["name"];
$age = $_GET["age"];
$email = $_GET["email"];
$sex = $_GET["sex"];
$exercise = $_GET["exercise"];
$dancing = $_GET["dancing"];
$party = $_GET["party"];
$rating = $_POST["rating"]; 
if ($exercise == '0 Hours') {
$fit1 = 0;
} elseif ($exercise == '1 Hours') {    
    $fit1 = 1;
} elseif ($exercise == '2 Hours') {
    $fit1 = 2;
} elseif ($exercise == '3 Hours') {
    $fit1 = 3;
} elseif ($exercise == '4 Hours') {
    $fit1 = 4;
} else {
    $fit1 = 5;
}
if ($dancing == 'Not A Very Good Dancer') {
    $fit2 = 0;
} elseif ($dancing == 'OK at Danicng') {
    $fit2 = 1;
} else {
    $fit2 = 2;
}
if($party == 'Hates Parties') {
    $fit3 = 0;
} elseif ($party == 'Thinks Parties Are OK') {
    $fit3 = 1;
} else {
    $fit3 = 2;
}
$rating = $fit1 + $fit2 + $fit3;
$query = "INSERT INTO aida VALUES ('','$name','$age','$email','$sex','$exercise','$dancing','music','$party','$rating')";
mysql_query($query);
mysql_close();
echo "Thank you $name for singing up. Your fitness level was $rating."
?>
<style type="text/css">
<!--
.style1 {
font-family: "Trebuchet MS";
color: #FF0000;
font-weight: bold;
}
-->
</style>
<body style="filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, EndColorStr='#003399', StartColorStr='#FFFFFF');">
<form action="compare.php" method="post" enctype="multipart/form-data" name="form1">
<input name="rating" type="hidden" id="rating" value="<? echo"$rating" ?>">
  <input name="Compare Results" type="submit" id="Compare Results" value="Compare Results">
</form>
<p class="style1"> </p>
</body>

Here is compare.php, the one I've been having problems with:-

<?
include("dbinfo.inc.php");
mysql_connect(mysql,$username,$password);
@mysql_select_db($database) or die( "Unable to select database"); 
$poo  = "SELECT name, age, sex, exercise, music, fitness FROM aida";
$result = mysql_query($poo);
$rat = $_POST["rating"];
$ok_vals = array(($rat - 2), ($rat - 1), $rat, ($rat + 1), ($rat + 2));
while($row = mysql_fetch_row($result))
{
    $name = $row[0];
    $age = $row[1];
    $sex = $row[2];
    $exercise = $row[3];
    $music = $row[4];
    $rating = $row[5];

if (in_array($rating,$ok_vals)) {
    echo "Name :$name 
    Age Group : $age 
    Sex : $sex 
    Exercise (per week) : $exercise 
    Favourite Music : $music 
    Fitness Rating : $rating" ; 
} else {
    echo "NOT in_array: ".$rating."<BR>";
    echo "Array: ";
    print_r($ok_vals);
}
}
mysql_close();
?>
<body style="filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=1, EndColorStr='#003399', StartColorStr='#FFFFFF');">
</body>

Here is dbinfo.inc.php, the file that has the database info:-

<?php
$username="4138_aida";
$password="******";
$database="4138_aida";
?> 

Lastly here is my mysql database, (I have entered the form a few times to make sure results come up):-

-- phpMyAdmin SQL Dump
-- version 2.8.0.3
-- http://www.phpmyadmin.net
-- 
-- Host: mysql
-- Generation Time: Sep 30, 2007 at 08:13 AM
-- Server version: 5.0.45
-- PHP Version: 5.2.4
-- 
-- Database: `4138_aida`
-- 

-- --------------------------------------------------------

-- 
-- Table structure for table `aida`
-- 

CREATE TABLE `aida` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(20) NOT NULL,
  `age` varchar(20) NOT NULL,
  `email` varchar(80) NOT NULL,
  `sex` varchar(20) NOT NULL,
  `exercise` varchar(20) NOT NULL,
  `dancing` varchar(20) NOT NULL,
  `music` varchar(20) NOT NULL,
  `party` varchar(60) NOT NULL,
  `rating` varchar(20) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=56 DEFAULT CHARSET=latin1 AUTO_INCREMENT=56 ;

-- 
-- Dumping data for table `aida`
-- 

INSERT INTO `aida` (`id`, `name`, `age`, `email`, `sex`, `exercise`, `dancing`, `music`, `party`, `rating`) VALUES (54, 'kjgjkyuf', '0-15', 'itkuikki', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(53, 'ryrtk', '0-15', 'ghluyjyu', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(52, 'fdzgshg', '0-15', 'dfjhfg', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(51, 'jhchfxdghdihoghk', '0-15', 'fjhjujfykhgh', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(50, 'RHTEH', '0-15', 'HRYHRJR', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(49, 'erhetyjtyfdety', '0-15', 'ethyyjejnj', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(48, 'rth4tyjr', '0-15', 'yheyjhyrt', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(47, 'dytjuyk', '0-15', 'rjtnyj', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(46, 'fdzgshg', '0-15', 'dfjhfg', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(45, 'dfhfj', '0-15', 'djgghdf', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(44, 'HFFK', '0-15', 'GFDJF', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(43, 'jhchfxdghdihoghk', '0-15', 'fjhjujfykhgh', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(42, 'jhchfxdghdiho', '0-15', 'fjhjujfy', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(41, 'jhchfxdghd', '0-15', 'fjhjujfy', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(40, 'jhchfxdghd', '0-15', 'fjhjujfy', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2'),
(55, 'rtyrjh', '0-15', 'ryhrh', 'Male', '0 Hours', 'Not A Very Good Danc', 'music', 'Loves Parties', '2');

Any suggestions EXTREMELY welcome? P.S- There is probably a million errors!

Thanks In Advance,

mcdof001

Link to comment
Share on other sites

Your code did work. I was pulling the wrong column from the database. Everything now works perfect Thanks very much. If I add to it later, I'll be sure to be asking for help! I'm going to go down to a library and take out some books instead of wasting your time! Thanks again! mcdof001

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.