1

Here I am writing the code fully to explain my problem. the links are generated but the list is not. what could be the problem? thanks..

require 'database.php'; $query="SELECT subject.subjectName, subject.subjectId FROM course,subject where subject.subjectId=course.subjectId and course.memberId=1"; $courses=$db->query($query); ?> <!doctype html> <html> <head> <title>foreach</title> </head> <body> <?php foreach ($courses as $course):?> <a href="#"> <?php echo $course['subjectName'];?> </a> <br> <?php endforeach; ?> <ul> <?php foreach ($courses as $course):?> <li> <?php echo $course['subjectName'];?> </li> <?php endforeach; ?> </ul> </body> </html> 
8
  • Small comment to clarify you are not using objects in this particular situation. Knowing the correct terminology for PHP can help other people here solve your problems. Commented Mar 22, 2014 at 13:29
  • @TomKriek: foreach twice would not cause an issue unless $courses is no longer in the scope on second iteration. I suspect he's either out of scope, or $courses actually implements ArrayAccess and Iterator and makes a mess of it. Need more info. Commented Mar 22, 2014 at 13:31
  • @TomKriek doesn't this create an object ? $courses=$db->query($query); Commented Mar 23, 2014 at 7:29
  • Why don't you take a look at the source code and make sure the UL is actually empty - maybe some strange CSS behaviour is hiding the elements from you Commented Mar 23, 2014 at 11:23
  • @Barakzai If you include your source code from the start then we can see what's actually going on. Commented Mar 23, 2014 at 12:26

5 Answers 5

1

Your second code has a quote inside before the closing PHP tag.

Before

<?php foreach($courses as $course):?> <a href"<?php echo $course['courseName'] "?> </li> <?php endforeach;?> 

After

<?php foreach($courses as $course):?> <a href="<?php echo $course['courseName']; ?>">???</a> <?php endforeach;?> 

And for easier to read code

<?php foreach($courses as $course) { echo '<a href="'.$course['courseName'].'">A Link</a>'; } ?> 
Sign up to request clarification or add additional context in comments.

Comments

0

No, foreach does not modify the object. You are probably overriding $courses variable between two foreach blocks

1 Comment

No the only place I am using $courses is in foreach loops and it perfectly works when i make a copy of $courses as $courses1 and use it in second foreach loop
0

Just not sure if its mistake while type code here or you have exactly same code in your script but pls check this in second for each

Here you are missing "=" after href should be like this is missing

Comments

0

Did you check page source? It might be hidden by css as you have different HTML tags in each block

1 Comment

I have not used any CSS . the same code works if I make a copy of $courses and use it as $courses1 in the second loop..
0

Hi everyone I finally got the answer for my problem and here it is:

In my code $courses is a PDOstatement Object and foreach loop calls upon fetch() method and fetch() method frees the result set, that means at end of foreach loop there are no more rows left. its all freed, so we can not use the same $courses again in loop since there is nothing left in it...

hope I am not missing anything ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.