Jump to content

Nesting while loops


9three

Recommended Posts

Hey, I can't seem to get 2 while loops to work properly.

 

public function RandomFilterBox($intID) {
    $intID = (int)$intID;
   
    if (empty($intID))
      return false;
   
    $strQueryCheckRandomSettings = 'SELECT u_id, random_settings
                                    from home_settings
                                    WHERE u_id = :id';
    $objStatement = $this->DB->prepare($strQueryCheckRandomSettings);
    $objStatement->bindParam(':id', $intID, PDO::PARAM_INT);
    $objStatement->execute();
    if ($row = $objStatement->fetch()) {
      if (!empty($row['random_settings']))
        $strQueryRandom = 'AND ('.$row['random_settings'].')';
     
      $strQuery = 'SELECT id, song_file
                   FROM customers
                   WHERE type = "A"
                   AND verified = "Y"
                   '.$strQueryRandom.'
                   ORDER BY RAND()
                   LIMIT 0, 18';
      $objStatement->closeCursor();
      $objStatement = $this->DB->prepare($strQuery);
      $objStatement->execute();
    }
    else {
      $strQuery = 'SELECT id, song_file
                   FROM customers
                   WHERE type = "A"
                   AND verified = "Y"
                   ORDER BY RAND()
                   LIMIT 0, 18';
      $objStatement = $this->DB->prepare($strQuery);
      $objStatement->execute();
    }
      $strQueryVotes = 'SELECT v_id, a_id, n_stars
                        FROM votes
                        WHERE v_id = :voter_id';
      //$objStatement->closeCursor();
      $objStatement2 = $this->DB->prepare($strQueryVotes);
      print_r($this->DB->errorInfo());
      $objStatement2->bindParam(':voter_id', $intID, PDO::PARAM_INT);
      $objStatement2->execute();
     
      $intI = 0;
      while ($rowArtist = $objStatement->fetch()) {
        while ($rowVotes = $objStatement2->fetch()) {

 

See where the while loop starts? If I do

while ($rowArtist = $objStatement->fetch()) {
  echo $rowArtist['id'];

 

It works fine. If I do

 

while ($rowArtist = $objStatement->fetch()) {
  while ($rowVotes = $objStatement2->fetch()) {
    echo $rowArtist['id'];

 

It doesnt display anything.

 

How can I get these two to work, I need to compare the values to continue.

Link to comment
https://forums.phpfreaks.com/topic/175567-nesting-while-loops/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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