genzedu777 Posted May 24, 2010 Share Posted May 24, 2010 // Grab the response data from the database to generate the form $query = "SELECT response_id, topic_id, response FROM mismatch_response WHERE user_id = '" . $_SESSION['user_id'] . "'"; $data = mysqli_query($dbc, $query); $responses = array(); while ($row = mysqli_fetch_array($data)) { // Look up the topic name for the response from the topic table $query2 = "SELECT name, category FROM mismatch_topic WHERE topic_id = '" . $row['topic_id'] . "'"; $data2 = mysqli_query($dbc, $query2); if (mysqli_num_rows($data2) == 1) { $row2 = mysqli_fetch_array($data2); $row['topic_name'] = $row2['name']; $row['category_name'] = $row2['category']; array_push($responses, $row); } } / Generate the questionnaire form by looping through the response array echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">'; echo '<p>How do you feel about each topic?</p>'; $category = $responses[0]['category_name']; echo '<fieldset><legend>' . $responses[0]['category_name'] . '</legend>'; foreach ($responses as $response) { // Only start a new fieldset if the category has changed if ($category != $response['category_name']) { $category = $response['category_name']; echo '</fieldset><fieldset><legend>' . $response['category_name'] . '</legend>'; } Hi I need help in understanding $category = $responses[0]['category_name']; $responses[0] is an array right? How about $responses[0]['category_name'], is it a double array? I have 3 different category names... [0] --> Entertainment [1] --> Sports [3] --> Reading So how does the coding works? It will first call ... $responses[0][0]? Which is the first entry and Entertainment? Thanks, Wilson Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 24, 2010 Share Posted May 24, 2010 $category = $responses[0]['category_name']; That will assign to the variable $category the value for the 'category_name' index in the first index of $responses. You are dealing with a multi-dimensional array Put this after $responses is defined: echo "<pre>"; print_r($responses); echo "<pre>"; It will show you the entire contents of the array showing the multiple dimensions. Maybe it will make more sense to you then. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.