Jump to content

Need help in array


genzedu777

Recommended Posts

// 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

Link to comment
https://forums.phpfreaks.com/topic/202748-need-help-in-array/
Share on other sites

$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.

Link to comment
https://forums.phpfreaks.com/topic/202748-need-help-in-array/#findComment-1062639
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.