O'Reilly Forums: Chapter 8 Undefined Offset Error - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 8 Undefined Offset Error

#1 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 30 November 2012 - 07:00 AM

Hello,

I tried to follow along with the mymismatch script in chapter 8 but for some reason I appear to be getting a weird error and I don't understand why that might be


Attached Image: chapter8.png


The errors appear to be related to this block of code:


// Compare each response and calculate a mismatch total  	$score = 0;  	$topics = array();  	for ($i = 0; $i < count($user_responses); $i++) {    	if ($user_responses[$i]['response'] + $mismatch_responses[$i]['response'] == 3) {      	$score += 1;      	array_push($topics, $user_responses[$i]['topic_name']);    	}  	}


Here's the rest of the code for some greater context:

[code]


// Start the session
require_once('startsession.php');

// Insert the page header
$page_title = 'Where opposites attract!';
require_once('header.php');

require_once('appvars.php');
require_once('connectvars.php');

// Show the navigation menu
require_once('navmenu.php');

// Only look for a mismatch if the user has questionnaire responses stored

$query = "SELECT * FROM mismatch_response WHERE user_id = '" . $_SESSION['user_id'] . "'";

$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or die(DB_CONNECT_ERR . mysqli_connect_error() . DB_CONNECT_ERR_NO . mysqli_connect_errno());

$data = mysqli_query($dbc, $query)
or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query );

if (mysqli_num_rows($data) != 0) {

// First grab the user's response from the response table (JOIN to get the topic name)
$query = "SELECT mr.response_id, mr.topic_id, mr.response, mt.name AS topic_name " .
"FROM mismatch_response AS mr " .
"INNER JOIN mismatch_topic AS mt " .
"USING (topic_id) " .
"WHERE mr.user_id = '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query)
or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query );

$user_responses = array();

while ($row = mysqli_fetch_array($data)) {
array_push($user_responses, $row);

}



// Initialize the mismatch results
$mismatch_score = 0;

$mismatch_user_id = -1;

$mismatch_topics = array();

// Loop through the user table comparing other people's responses to the user's response
$query = "SELECT user_id FROM mismatch_user WHERE user_id != '" . $_SESSION['user_id'] . "'";

$data = mysqli_query($dbc, $query)
or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query );


while ($row = mysqli_fetch_array($data)) {
// Grab the response data for the user (a potential mismatch)
$query2 = "SELECT response_id, topic_id, response FROM mismatch_response " .
"WHERE user_id = '" . $row['user_id'] . "'";

$data2 = mysqli_query($dbc, $query2);

$mismatch_responses = array();

while ($row2 = mysqli_fetch_array($data2)) {

array_push($mismatch_responses, $row2);

}

}

// Compare each response and calculate a mismatch total
$score = 0;
$topics = array();
for ($i = 0; $i < count($user_responses); $i++) {
if ($user_responses[$i]['response'] + $mismatch_responses[$i]['response'] == 3) {
$score += 1;
array_push($topics, $user_responses[$i]['topic_name']);
}
}




// Check to see if this person is better than the best mismatch so far

if ($score > $mismatch_score) {
// We found a better mismatch, so update the mismatch search results
$mismatch_score = $score;
$mismatch_score = $score ;
$mismatch_user_id = $row['user_id'];
$mismatch_topics = array_slice($topics, 0);
}


}

// Make sure a mismatch was found

if ($mismatch_user_id != -1) {
$query = "SELECT username, first_name, last_name, city, state, picture from mismatch_user " .
"WHERE user_id = '$mismatch_user_id'";

$data = mysqli_query($dbc, $query)
or die( SQL_SELECT_ERR . mysqli_error($dbc) . SQL_ERR_NO . mysqli_errno($dbc) . QUERY_USED . $query );

if (mysqli_num_rows($data) == 1) {
// The user row was found, so display the user data

$row = mysqli_fetch_array($data);

echo '';

if (!empty($row['first_name']) && !emtpy($row['last_name'])) {

echo $row['first_name'] . ' ' . $row['state'] . '
';

}

echo '';


if (!empty($row['picture'])) {
echo '%27%20.%20MM_UPLOAD_PATH%20.%20$row[%27picture%27]%20.%20%27
';

}

echo '';

// Display the mismatched topics
echo '<h4>View
0

#2 User is offline   bluethundr 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 30
  • Joined: 05-May 09
  • Gender:Male
  • Location:Summit, NJ

Posted 30 November 2012 - 09:47 AM

Quick update: I tried doing a print_r on both arrays involved in this loop:

// Compare each response and calculate a mismatch total
      $score = 0;
      $topics = array();
      for ($i = 0; $i < count($user_responses); $i++) {
        if ($user_responses[$i]['response'] + $mismatch_responses[$i]['response'] == 3) {
          $score += 1;
          array_push($topics, $user_responses[$i]['topic_name']);
        }
      }



It turns out that the user_responses array is fine and normal. But the mismatch_responses is empty. Now all I need to do is try and figure out why.

Thanks
Tim
0

#3 User is offline   drewdin 

  • Super Veteran Member
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: Members
  • Posts: 589
  • Joined: 11-February 10
  • Gender:Male
  • Location:Boston

Posted 30 November 2012 - 10:44 AM

check this and see what you are getting for results, also chekc your database and make sure your tables names match your script. On a side note, You did fill out the form right?

while ($row = mysqli_fetch_array($data)) {
// Grab the response data for the user (a potential mismatch)
$query2 = "SELECT response_id, topic_id, response FROM mismatch_response " .
"WHERE user_id = '" . $row['user_id'] . "'";

$data2 = mysqli_query($dbc, $query2);

$mismatch_responses = array();

while ($row2 = mysqli_fetch_array($data2)) {

array_push($mismatch_responses, $row2);

}

}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users