Forum Moderators: coopster

Message Too Old, No Replies

Iterating Through an Array

         

PianoGuy

5:33 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



Hi, again --

I'm attempting to iterate through an array for the purpose of listing its elements in a report. This is a problem that will be replicated several times in my application in various ways.

The array is the result of:

$StudNoteQry = mysql_query("select STUDNOTE_DATE, STUDNOTE from STUDENT_NOTES where STUDENT_NOTES.STUD_ID = '$SelectID'");

$notes = mysql_fetch_array($StudNoteQry, MYSQL_ASSOC);

<?php
if ($notes!= ""){
foreach ($notes as $value){
echo "$value<br>";
}
}
?>

This produces only the first STUDNOTE item and the first STUDNOTE_DATE item. What have I overlooked? I attempted to advance the internal array pointer with $i++ after the echo statement. As I understand it, foreach should advance the pointer automatically.

Grateful for any help,
PianoGuy

dreamcatcher

5:40 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi PianoGuy,

You don`t need a foreach loop when using mysql_fetch_array as this returns the data in an array already. Try a while loop.

while ($notes = mysql_fetch_array($StudNoteQry))
{
echo $notes['STUDNOTE']."<br>";
}

Something like that should help you.

dc

vincevincevince

5:41 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$notes = mysql_fetch_array($StudNoteQry, MYSQL_ASSOC);

This only gets one result.

You should do it like this:


while ($notes = mysql_fetch_array($StudNoteQry, MYSQL_ASSOC)) echo "$notes[STUDNOTE]<br>";

mysql_fetch_array() returns false if there are no records, so the while () loop will kepe going until you run out of rows.

<added>well done DC, beat me to that one</added>

[edited by: vincevincevince at 5:44 pm (utc) on Oct. 11, 2005]

madmac

5:43 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



mysql_fetch_array() does not push the entire result set into an array. It only grabs the current row.

you want to do something like:

while ($note = mysql_fetch_assoc($StudNoteQry)) {
echo $note['STUDNOTE_DATE'];
}

madmac

5:44 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



you guys are too fast for me

PianoGuy

6:08 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



You guys are incredibly fast!

I took Dreamcather's suggestion (I see that they all are basically the same), but curiously, now the first item in the array is the ONLY one that won't output...

Any ideas of what may be causing this?

PianoGuy

vincevincevince

7:59 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Get rid of the other mysql_fetch_array line :) only the one in DC's code is needed

PianoGuy

8:56 pm on Oct 11, 2005 (gmt 0)

10+ Year Member



I should have probably gotten more than 1.5 hours of sleep last night...

Thanks,
PianoGuy

dreamcatcher

10:36 pm on Oct 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad we could help you. :)

mcibor

9:27 am on Oct 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use the DC solution, or to get data as well:

while ($notes = mysql_fetch_assoc($StudNoteQry))
{
echo "Studnote: ". $notes['STUDNOTE'] ."&nbsp;&nbsp; Studnote date: ". $notes['STUDNOTE_DATE'] ."<br>";
}

Or if you really want the foreach loop (eg. you don't know how many fields there are in db) do:

while ($notes = mysql_fetch_assoc($StudNoteQry))
{
foreach ($notes as $key => $value)
{
echo "$key: $value&nbsp;&nbsp";
}
echo "<br>";
}

The problem is that mysql_fetch_array fetches only one mysql result row and then moves the pointer ($StudNoteQry). Hope these all explanations help you understand.

Best regards
Michal Cibor