Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to populate results with a description from my MySQL database table interest_type

My private function is

    private function Get_Interests_Types_From_DB()
    {
        $sql = "SELECT 
                    InterestID,
                    InterestDescription
                FROM
                    interest_type";

        $result = mysqli_query($this->Con, $sql);

        while($row = mysqli_fetch_array($result))
        {
            $arrayResult[] = $row;
        }

        return ($arrayResult);
    }

The area of code that I am trying to use this function in is within a public function within the same class. The values from the form are numeric. I am trying to tie the $interests variable with the InterestID from the table and then print the InterestDescription, instead of the value of $interests.

function ProcessRegistrationForm()
    {   
        $fname = $_POST['firstname'];
        $lname = $_POST['lastname'];
        $email = $_POST['email'];
        $gender = $_POST['gender'];
        $interests = $_POST['interests'];

        if(!isset($_POST['firstname']) || !isset($_POST['lastname']) || !isset($_POST['email']) ||
            ($_POST['firstname']) == '' || ($_POST['lastname']) == '' || ($_POST['email']) == '')
        {
            echo("Please enter your first / last name and email.");
        }   
        else
        {
            echo("<h2>Results</h2>");
            echo("<div id='results'>");
            echo $fname; 
            echo("<br />");
            echo $lname; 
            echo("<br />");
            echo $email;
            echo("<br />");
            echo $gender;
            echo("<br />");

            $interestDescription = $this->Get_Interests_Types_From_DB();

            foreach($interests as $likes)
            {
                if($likes == $interestDescription['InterestID'])
                    echo $$interestDescription['InterestDescription'] . "<br />";
            }
            echo("<p style='font-weight: bold;'>Your data has been saved! We will contact you soon!</p>");
            echo("</div>");
        }

        $myClub = new Club("localhost","A340User","Pass123Word","info_club");

        $date = date("Y/m/d");

        $sql="INSERT INTO member
                    (`FirstName`,`LastName`,`Gender`,`Email`,`MemberSince`)
                VALUES
                    ('$fname','$lname','$gender','$email','$date');";

        $result = mysqli_query($this->Con,$sql);
        /*if($result == true) 
        {
            echo "Successful Insert<br />";
        }
        else
        {
            echo "Error Inserting class" . mysqli_error($this->Con) ." <br />";
        }*/

        for($i = 0; $i < sizeof($interests); $i++)
        {
            $interest = $interests[$i];

            $sql="INSERT INTO member_interests
                        (`Email`,`InterestID`)
                    VALUES
                        ('$email',$interest);";

            $result = mysqli_query($this->Con,$sql);
        }

I am getting: Notice: Undefined index: InterestID in C:\xampp-portable\htdocs\A340\Assign5\Assign5_Club_Membership_Class.php on line 287

How can I print the InterestDescription instead of the value of $interests?

share|improve this question
it seems that $interestDescription['InterestID'] doesnt exist... check $interestDescription with print_r(). – vlzvl May 1 at 16:58
I still get Notice: Undefined index: InterestID. I am not sure why this is. I used the same information in a previous class function that runs in the same class. I can assure you that InterestID does exist. Why is this happening? – CharlWillia6 May 1 at 17:05
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

there:

while($row = mysqli_fetch_array($result))
{
   $arrayResult[] = $row;
}

now the $arrayResult, which is actually your $interestDescription, contains something like:

$interestDescription[0]["InterestID"] 
$interestDescription[1]["InterestID"]
...

and not $interestDescription[InterestID"]

UPDATE:

perhaps you need something like this (assuming that $interests holds an array of IDs)

        $interestDescription = $this->Get_Interests_Types_From_DB();
        foreach($interests as $likes) {
           foreach($interestDescription as $desc) {
              if($likes == $desc['InterestID']) {
                 echo $desc['InterestDescription'] . "<br />";
              }
           }
        }
share|improve this answer
So does this mean I should use a for loop instead of foreach, so I can say $interestDescription[$i]['InterestID']? – CharlWillia6 May 1 at 17:08
Ok, so I changed my code to: $interestDescription = $this->Get_Interests_Types_From_DB(); for($i = 0; $i < sizeof($interestDescription); $i++) { if($interests = $interestDescription[$i]['InterestID']) echo($interestDescription[$i]['InterestDescription'] . "<br />"); } and all it does is populate the display with all the InterestDescription data from the table, and not just the ones by value in $interests. So if the user picked interests with the value of 2 and 5, I am still getting all six descriptions printed that are in the database – CharlWillia6 May 1 at 17:15
check my update – vlzvl May 1 at 17:16
Perfect. Thank you. I get it now. I appreciate the help. – CharlWillia6 May 1 at 17:17
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.