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