0

I am trying to show it all with while loop on a dynamic page, but it wont show anything at all..

if (isset($_GET['id'])) {
      $genre = $_GET['id'];
      $sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
      $result = $db->query($sql);
      $row = $result->fetch_assoc();
}else{
     $sql = "SELECT ID, genre FROM movstream_genre";
     $result = $db->query($sql);
}

<html>
<body>
<ul>
    <?php while ( $row = $result->fetch_assoc() ): ?>
    <li><a href="genre?id=<?=$row['ID'];?>"><?=$row['genre'];?></a></li>
    <?php endwhile; ?>
</ul>
</body>
</html>

Anyone know why it wont show anything on the dynamic page, but if the page is not dynamic it works fine :)

Thanks.

3
  • 2
    Do you get any errors? Commented May 16, 2015 at 11:12
  • No errors, it just wont show anything from the db when its on a dynamic page. Commented May 16, 2015 at 11:21
  • 1
    var_dump($row) ,to see whether values are coming or not Commented May 16, 2015 at 11:53

4 Answers 4

1

If dynamic page means that your sending an id in Url an you are not getting result. I think that the problem is that you are using $row = $result->fetch_assoc(); in if condition and also in while. Please use fetching in one place. Better be in while loop. Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

It was allmost that :D now it only shows the id that the dynamic page is on if you know what i mean :) I want it to show all the id's. Its an sub menu that show all the 'genre' Action, Crime etc.
@krunos, so, do you want to show an entire list of genres regardless the id parameter set or not? In this case you, probably, don't need to write the if/else block in that manner. Just fetch all needed data from DB. And if the id parameter is set, take it from that list. Alternatively, you can make an additional request to DB instead of searching the needed item in the previously fetched list.
Do you want to show all the genre and highlight the given one?
1

I figured it out

if (isset($_GET['id'])) {
$genre = $_GET['id'];

$sql = "SELECT ID, genre FROM movstream_genre";
$result = $db->query($sql);
}

This it how it needs to look when its a dynamic page :) well i think it is, it works now.

Comments

0

please echo the result ...

<li><a href="genre?id=<?php echo $row['ID'];?>"><?php echo $row['genre'];?></a></li>

And in href, it is only genre OR genre.php

In the first query..

$sql = "SELECT * FROM movstream_genre WHERE ID = '$genre'";
$result = $db->query($sql);
$row = $result->fetch_assoc();// this line not required

comment this line $row = $result->fetch_assoc();

Comments

0

It looks like you haven't enclosed the first part of your PHP code in <?php ?> tags

1 Comment

Yeah, sorry, i forgot to copy it in here :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.