0

Possible Duplicate:
What does the PHP error message “Notice: Use of undefined constant” mean?

I am trying to display one (record) question at a time in the same page. I put a condition at the beginning of the code to check if this is the first time the page loads, if it is, then display the first record. Otherwise, go to the "else" statement where the second record will be displayed. Every time a record is displayed, the counter increases by one ($i++). Also, I saved all the retrieved records in an array and reading the records one at a time from this array. I don't know why I am getting errors here like the the following:

(1) Use of undefined constant i - assumed 'i'

(2) Undefined index: i

Does anyone know how to fix this problem?

Here is my code:

<?php

 $f_name = $_SESSION['first_name'];
 $l_name = $_SESSION['last_name'];

 $arr_rows;
 $i;

 if (!isset($_POST['next'])) 

 { //if form is not submitted,

  $command2 = "SELECT user_id FROM user_info WHERE user_info.first_name = '$f_name' 
  and user_info.last_name = '$l_name'";

  $command1 = "SELECT * FROM topics, documents WHERE topics.topic_id =documents.topic_id";  

  $i=0; // Counter for the number of documents per topic


  $userid = mysql_query($command2);
  $results = mysql_query($command1);
  $num=mysql_numrows($results);

  //////////////

  $arr_rows = array();
  while( $row = mysql_fetch_array( $results ) )
  $arr_rows[] = $row;


  $arr = mysql_fetch_row($userid);
  $id_user = $arr[0]; 

  echo $f_name;
  $relevancy = "This is the first time to load this page";


  $f1=$arr_rows[i]['topic_name'];
  $f1_topic_description=$arr_rows[i]['topic_descrip'];
  $f1_doc_content=$arr_rows[i]['doc_content'];

  ++$i;

  } 

  else 

  { //otherwise,

    $relevancy = $_POST['RadioGroup1'];

    $f1=$arr_rows[i]['topic_name'];
    $f1_topic_description=$arr_rows[i]['topic_descrip'];
    $f1_doc_content=$arr_rows[i]['doc_content'];

    ++$i;

    }
    ?>
Community
  • 1
  • 1
  • You haven't initialized your $i for the else-case. – looper Nov 09 '12 at 11:29
  • Its initialized before the if. – Adam Nov 09 '12 at 11:31
  • When you get stuck like this, try and read through your code and think about what you are doing. Then you'll notice that you haven't actually called your variable, just confused php with an undefined constant. If you're still stuck, then here's the place to ask, but working things out yourself actually helps you learn more! – Nathan Edwards Nov 09 '12 at 11:32

5 Answers5

1
$f1=$arr_rows[i]['topic_name'];
$f1_topic_description=$arr_rows[i]['topic_descrip'];
$f1_doc_content=$arr_rows[i]['doc_content'];

should be

$f1=$arr_rows[$i]['topic_name'];
$f1_topic_description=$arr_rows[$i]['topic_descrip'];
$f1_doc_content=$arr_rows[$i]['doc_content'];

You were missing the dollar sign on the variable "i".

Pedro Cordeiro
  • 2,085
  • 1
  • 20
  • 41
1

PHP doesn't store your variables over to the next request.

You can add the counter information ($i) either to a session variable, cookie, or request parameter. Otherwise your $i will always start again from zero.

Dani P.
  • 113
  • 1
  • 1
  • 9
1

When using "i" to specify an index in your arrays, you are missing the $. For example:

$f1=$arr_rows[i]['topic_name']; $f1_topic_description=$arr_rows[i]['topic_descrip']; $f1_doc_content=$arr_rows[i]['doc_content'];

should read

$f1=$arr_rows[$i]['topic_name'];
$f1_topic_description=$arr_rows[$i]['topic_descrip'];
$f1_doc_content=$arr_rows[$i]['doc_content'];
cprlkleg
  • 363
  • 1
  • 5
  • 21
0

You missing the $ before the variable name. Check through your code and replace i with $i.

Adam
  • 1,214
  • 13
  • 23
0

The error is here I think... replace i with $i so you're actually calling your variable

$f1=$arr_rows[$i]['topic_name'];
$f1_topic_description=$arr_rows[$i]['topic_descrip'];
$f1_doc_content=$arr_rows[$i]['doc_content'];
Nathan Edwards
  • 311
  • 1
  • 3
  • 17