Jump to content

Recommended Posts

 $sql = "SELECT * FROM dczcat1 ORDER BY position";
    if ($result = $mysqli->query($sql)){
      if ($result->num_rows > 0){
         while ($row = $result->fetch_array()){
           $table=$row[cattable];
           echo"$table";
           $scat2=$table . 2;
           echo"$csat";
           $cat=$row[cat];
           echo"$cat";
           $sql="SELECT * FROM $table ORDER BY position";
           echo"$sql";
             if($result=$mysqli->query($sql)){
               if($result->num_rows>0){
                 echo"Pumpkin";
                 while ($row = $result->fetch_array()){
                 echo"<br />should not be displayed if it doesn't say Pumpkin above";
                 $level+=1;
                 echo"LEVEL69:$level";
                 echo<<<HTML
                 <div class = "menutitles"
                 onmouseover="document.getElementById('$table').style.display='block';document.getElementById('$scat2').style.display='block';"
                 onmouseout="document.getElementById('$table').style.display='none';document.getElementById('$scat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$fullcat">$cat pos44</a><br/>
                 <div class="contain" id="$table">
                 <div class="menuitems" id="$scat2">
                 
HTML;
                 $table=$row[cattable];
                 echo "$table";
                 $scat2= $table . 2;
                 echo "$scat";
                 $cat = $row[cat];
                 echo "$cat";
               select($cat, $table, $scat2, $mysqli, $level);
               }
               }else if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$table'>$cat pos2</a><br/>
               ";
               echo"table:$table";
               }else{
               While($row=$result->fetch_array()){
               $table=$row[cattable];
               $scat2= $table . 2;
               $cat = $row[cat];
               echo"
               <a class='topseltxt2' href='gem.php?cat=$table'>$cat pos3</a><br/>
               ";}
               $level-=1;
        }
      }else{
        die("ERROR: Could not execute query $sql." . $mysqli->error);
      }echo"</div> </div> </div>";
    }
  }
}else{
  die("ERROR: Query failed: $sql." . $mysqli->error);
}

 

At line 15 it says echo "Pumpkin" and below after the while loop it says "should not be displayed unless it says pumpkin above"  When I run the above code it displays:

 

should not be displayed unless it says pumpkin above

 

No Pumpkin, how is this possible, if num_rows is greater than 0, then it should either display the word pumpkin, or it should skip everything between the {}'s shouldn't it?  I don't want the while loop to run if num_rows is greater than 0, but it does and I'm sure it shouldn't be.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/
Share on other sites

It shows this

 

<a class="topseltxt2" href="gem.php?cat=">Round Brilliant pos44</a><br/>
                 <div class="contain" id="dczcat1RoundBrilliant">
                 <div class="menuitems" id="dczcat1RoundBrilliant2">dczcat1RoundBrilliantWhiteWhiteSELECT * FROM dczcat1RoundBrilliantWhite ORDER BY positionshould not be displayed if it doesn't say Pumpkin aboveLEVEL69:3                 <div class="menutitles"
                 onmouseover="document.getElementById('dczcat1RoundBrilliantWhite').style.display='block';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1RoundBrilliantWhite').style.display='none';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 >

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129204
Share on other sites

It's likely that the code you are actually running is not the code you have posted.

 

If your bigger problem you are trying to solve is that the code does not loop as expected, it is because you are reusing the $result variable inside of the outer loop, so when the outer loop goes back to fetch the next row from the first query, $result is no longer what you expect.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129208
Share on other sites

I have posted the exact code that I'm running.  I know that it's important to post the exact code, it has been doing my head in all night.  I just want to get on and get it finished, but this "if" thing has baffled me.

 

I'm still working out how to do the thing I'm trying to do, but need to understand why the if doesn't work.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129225
Share on other sites

That might be the code you think you are running, but it is not the code that is on your server and being executed or you are looking at an old cached version of the output due to the settings on your web server.

 

Download the code to a different file name and look in it.

 

You could also have a different section of code that is similar or a different/wrong include file, but the output you posted didn't get produced by the code you posted. The HTML doesn't even match.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129291
Share on other sites

I have updated it since, I will post the code again and the html to make sure that it is the exact same code.

 

$sql = "SELECT * FROM dczcat1 ORDER BY position";
    if ($result = $mysqli->query($sql)){
      if ($result->num_rows > 0){
         while ($row = $result->fetch_array()){
           $table=$row[cattable];
           echo"$table";
           $scat2=$table . 2;
           echo"$scat2";
           $cat=$row[cat];
           echo"$cat";
           $sql="SELECT * FROM $table ORDER BY position";
           echo"$sql";
             if($result=$mysqli->query($sql)){
               if($result->num_rows>0){
                 echo"Pumpkin";
                 while ($row = $result->fetch_array()){
                 echo"<br />should not be displayed if it doesn't say Pumpkin above";
                 $level+=1;
                 echo"LEVEL69:$level";
                 echo<<<HTML
                 <div class = "menutitles"
                 onmouseover="document.getElementById('$table').style.display='block';document.getElementById('$scat2').style.display='block';"
                 onmouseout="document.getElementById('$table').style.display='none';document.getElementById('$scat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$table">$cat pos44</a><br/>
                 <div class="contain" id="$table">
                 <div class="menuitems" id="$scat2">
                 
HTML;
                 $table=$row[cattable];
                 echo "$table";
                 $scat2= $table . 2;
                 echo "$scat";
                 $cat = $row[cat];
                 echo "$cat";
               select($cat, $table, $scat2, $mysqli, $level);
               }
               }else if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$table'>$cat pos2</a><br/>
               ";
               echo"table:$table";
               }else{
               While($row=$result->fetch_array()){
               $table=$row[cattable];
               $scat2= $table . 2;
               $cat = $row[cat];
               echo"
               <a class='topseltxt2' href='gem.php?cat=$table'>$cat pos3</a><br/>
               ";}
               $level-=1;
        }
      }else{
        die("ERROR: Could not execute query $sql." . $mysqli->error);
      }echo"</div> </div> </div>";
    }
  }
}else{
  die("ERROR: Query failed: $sql." . $mysqli->error);
}

 

dczcat1RoundBrilliantdczcat1RoundBrilliant2Round BrilliantSELECT * FROM dczcat1RoundBrilliant ORDER BY positionPumpkin<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:2                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1RoundBrilliant').style.display='block';document.getElementById('dczcat1RoundBrilliant2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1RoundBrilliant').style.display='none';document.getElementById('dczcat1RoundBrilliant2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=dczcat1RoundBrilliant">Round Brilliant pos44</a><br/>
                 <div class="contain" id="dczcat1RoundBrilliant">
                 <div class="menuitems" id="dczcat1RoundBrilliant2">

                 dczcat1RoundBrilliantWhiteWhiteSELECT * FROM dczcat1RoundBrilliantWhite ORDER BY position<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:3                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1RoundBrilliantWhite').style.display='block';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1RoundBrilliantWhite').style.display='none';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=dczcat1RoundBrilliantWhite">White pos44</a><br/>
                 <div class="contain" id="dczcat1RoundBrilliantWhite">
                 <div class="menuitems" id="dczcat1RoundBrilliantWhite2">
                 dczcat1RoundBrilliantCitrineCitrineSELECT * FROM dczcat1RoundBrilliantCitrine ORDER BY position</div> </div> </div>

 

I am using tsWebeditor to edit the php files, it loads the files from and saves them directly to the FTP site.  If I change anything I see the changes immediately in the browser.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129345
Share on other sites

Now that you have posted MORE of the output on the page, it seems like it is echoing your Pumpkin test string as expected -

dczcat1RoundBrilliantdczcat1RoundBrilliant2Round BrilliantSELECT * FROM dczcat1RoundBrilliant ORDER BY positionPumpkin<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:2                <div class = "menutitles"

 

And someone already told you why your overall output is incorrect -

If your bigger problem you are trying to solve is that the code does not loop as expected, it is because you are reusing the $result variable inside of the outer loop, so when the outer loop goes back to fetch the next row from the first query, $result is no longer what you expect.

 

You also have a very bad database design, where you have individual tables for each product/variation and you must execute one query to get the table name and then put that table name into another query to actually get the information.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129415
Share on other sites

It only echos the pumpkin once, the second time round it doesn't at line 9 on the html.

 

I am still learning and maybe I will work on the database structure later, but now I want to understand how it is possible that the word pumpkin is not displayed and the next line is.

 

For the database, is it significantly slower to search a new table than to just search 1 huge table for everything.  I thought it would be quicker to search lots of little tables than search one  big one each time.  I've  tried to do this program with one big table and it is really difficult so I decided it would be easier to split them up and if it was not too slow use this structure.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129436
Share on other sites

It only echos the pumpkin once, the second time round it doesn't at line 9 on the html.

 

I am still learning and maybe I will work on the database structure later, but now I want to understand how it is possible that the word pumpkin is not displayed and the next line is.

 

For the database, is it significantly slower to search a new table than to just search 1 huge table for everything.  I thought it would be quicker to search lots of little tables than search one  big one each time.  I've  tried to do this program with one big table and it is really difficult so I decided it would be easier to split them up and if it was not too slow use this structure.

 

It's actually *more* complicated to do this using lots of small tables. Trust me, do that and learn a join (or to simplify, just loop more queries for learning purposes) and I'm sure your other problems will go away. I'd stop this database structure right now, scrap it all.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129437
Share on other sites

If I do get rid of the database structure though, I still want to understand how it can skip passed the word pumpkin but still display the next line of text.  The problem is all caused because of these 2 lines, I have fixed the $result thing, but now I have a new problem.

 

if(${'result' . $n}->num_rows>0){
                 echo"Pumpkin";
                 while ($row = ${'result' . $n}->fetch_array()){
                 echo"<br />should not be displayed if it doesn't say Pumpkin above";

 

It shouldn't try to fetch the array because num_rows is not greater than 0, but it does try and fetch the array and then it says call to function fetch_array() on a non object.  But it shouldn't call it, it should skip it because num_rows is not greater than 0 and if it was greater than 0 then surely it should display the word pumpkin, but it doesn't display the word pumpkin and still goes on to execute the next 2 lines.  To me this is impossible.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129478
Share on other sites

Here is the current code

 

$level=1;
$n=1;
    
    $sql = "SELECT * FROM dczcat1 ORDER BY position";
    if ($result = $mysqli->query($sql)){
      if ($result->num_rows > 0){
         while ($row = $result->fetch_array()){
           $table=$row[cattable];
           echo"$table";
           $scat2=$table . 2;
           echo"$scat2";
           $cat=$row[cat];
           echo"$cat";
           $n+=1;
           ${sql . $n}="SELECT * FROM $table ORDER BY position";
           echo"$sql";
             if(${'result' . $n}=$mysqli->query(${sql . $n})){
               if(${'result' . $n}->num_rows>0){
                 echo"Pumpkin";
                 while (${row . $n} = ${'result' . $n}->fetch_array()){
                 echo"<br />should not be displayed if it doesn't say Pumpkin above";
                 $level+=1;
                 echo"LEVEL69:$level";
                 echo<<<HTML
                 <div class = "menutitles"
                 onmouseover="document.getElementById('$table').style.display='block';document.getElementById('$scat2').style.display='block';"
                 onmouseout="document.getElementById('$table').style.display='none';document.getElementById('$scat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$table">$cat pos44</a><br/>
                 <div class="contain" id="$table">
                 <div class="menuitems" id="$scat2">
                 
HTML;
                 $prevtab=$table;
                 $table=${row . $n}[cattable];
                 echo "$table";
                 $scat2= $table . 2;
                 echo "$scat";
                 $cat = ${row . $n}[cat];
                 echo "$cat";
               select($n, $cat, $table, $scat2, $mysqli, $level);
               }
               }else if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$table'>$cat pos2</a><br/>
               ";
               echo"table:$table";
               }else{
               ${sql . $n}="SELECT * FROM $prevtab ORDER BY position";
               if(${'result' . $n}=$mysqli->query(${sql . $n})){
               if(${'result' . $n}->num_rows>0){
               While(${row . $n}=${'result' . $n}->fetch_array()){
               $table=${row . $n}[cattable];
               $scat2= $table . 2;
               $cat = ${row . $n}[cat];
               echo"
               <a class='topseltxt2' href='gem.php?cat=$table'>$cat pos3</a><br/>
               ";}
             }
           }
               $level-=1;
        }
      }else{
        die("ERRORpos1: Could not execute query $sql." . $mysqli->error);
      }echo"</div> </div> </div>";
    }
  }
}else{
  die("ERROR: Query failed: $sql." . $mysqli->error);
}

 

Here is the html

dczcat1RoundBrilliantdczcat1RoundBrilliant2Round BrilliantSELECT * FROM dczcat1 ORDER BY positionPumpkin<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:2                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1RoundBrilliant').style.display='block';document.getElementById('dczcat1RoundBrilliant2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1RoundBrilliant').style.display='none';document.getElementById('dczcat1RoundBrilliant2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=dczcat1RoundBrilliant">Round Brilliant pos44</a><br/>
                 <div class="contain" id="dczcat1RoundBrilliant">
                 <div class="menuitems" id="dczcat1RoundBrilliant2">

                 dczcat1RoundBrilliantWhiteWhite<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:3                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1RoundBrilliantWhite').style.display='block';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1RoundBrilliantWhite').style.display='none';document.getElementById('dczcat1RoundBrilliantWhite2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=dczcat1RoundBrilliantWhite">White pos44</a><br/>
                 <div class="contain" id="dczcat1RoundBrilliantWhite">
                 <div class="menuitems" id="dczcat1RoundBrilliantWhite2">
                 dczcat1RoundBrilliantCitrineCitrine</div> </div> </div>dczcat1Antiquedczcat1Antique2AntiqueSELECT * FROM dczcat1 ORDER BY positionPumpkin<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:4                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1Antique').style.display='block';document.getElementById('dczcat1Antique2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1Antique').style.display='none';document.getElementById('dczcat1Antique2').style.display='block';"
                 >

                 <a class="topseltxt2" href="gem.php?cat=dczcat1Antique">Antique pos44</a><br/>
                 <div class="contain" id="dczcat1Antique">
                 <div class="menuitems" id="dczcat1Antique2">
                 dczcat1AntiqueOldEuropeanCutOld European CutPumpkin<br />should not be displayed if it doesn't say Pumpkin aboveLEVEL69:5                 <div class = "menutitles"
                 onmouseover="document.getElementById('dczcat1AntiqueOldEuropeanCut').style.display='block';document.getElementById('dczcat1AntiqueOldEuropeanCut2').style.display='block';"
                 onmouseout="document.getElementById('dczcat1AntiqueOldEuropeanCut').style.display='none';document.getElementById('dczcat1AntiqueOldEuropeanCut2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=dczcat1AntiqueOldEuropeanCut">Old European Cut pos33</a><br/>
                 <div class="contain" id="dczcat1AntiqueOldEuropeanCut">
                 <div class="menuitems" id="dczcat1AntiqueOldEuropeanCut2">

dczcat1AntiqueOldEuropeanCutWhiteWhite</div> </div> </div>

 

It has run with no errors, but it isn't displaying the information correctly.  That is because it is not obaying the if(){} it is ignoring that num_rows>0 and executing what is in {} anyway.

Link to comment
https://forums.phpfreaks.com/topic/217495-mystery/#findComment-1129520
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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