Jump to content

Can you generate variable labels eg $s1 $s2 $s3.......


heldenbrau

Recommended Posts

I have a recursive sql query.  I'm doing this with an sql query as a function. 

 

Here is the function:

 

function select() {
  $sql="SELECT cat FROM $scat";
             if($result=$mysqli->query($sql)){
               if($result->num_rows>0){
                 $level =+1;
                 echo<<<HTML
                 <div class="menutitles">
                 onmouseover="document.getElementById('$scat').style.display='block';document.getElementById('$scat2').style.display='block';"
                 onmouseout="document.getElementById('$scat').style.display='none';document.getElementById('$scat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$scat"></a><br/>
                 <div class="contain" id="$scat">
                 <div class="menuitems" id="$scat2">
HTML;
               select();
               }else{$level=-1;
               if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$scat'></a><br/>
               ";
               }else{
               While($row=$result->fetch_array()){
               $scat=replace($row[cat]);
               echo"
               <a class='topseltxt2' href='gem.php?cat=$scat'></a><br/>
               ";}
               }
        }
      }
}

 

and this is the database query with the function added to it:

 

$level=1;
$count=0;
    
    $sql = "SELECT cat FROM dczcat1 ORDER BY position";
    if ($result = $mysqli->query($sql)){
      if ($result->num_rows > 0){
        $count=+1;
         while ($row = $result->fetch_array()){
           $scat=replace($row[cat]);
           $scat2=replace($row[cat]) . 2;
           $cat=$row[cat];
           $sql="SELECT cat FROM $scat";
             if($result=$mysqli->query($sql)){
               $count=+1;
               if($result->num_rows>0){
                 $level =+1;
                 echo<<<HTML
                 <div class="menutitles">
                 onmouseover="document.getElementById('$scat').style.display='block';document.getElementById('$scat2').style.display='block';"
                 onmouseout="document.getElementById('$scat').style.display='none';document.getElementById('$scat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$scat"></a><br/>
                 <div class="contain" id="$scat">
                 <div class="menuitems" id="$scat2">
HTML;
               select();
               }else{$level=-1;
               if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$scat'></a><br/>
               ";
               }else{
               While($row=$result . $count->fetch_array()){
               $scat=replace($row[cat]);
               echo"
               <a class='topseltxt2' href='gem.php?cat=$scat'></a><br/>
               ";}
               }
        }
      }echo"</div> </div> </div>";
    }
  }
}

 

I get the error:

 

Fatal error: Call to a member function fetch_array() on a non-object in /home/kangerc1/public_html/zleftsidebar.php on line 60

 

I think this is because I am using $result more than once and I need to call it $result1 $result2 $result3 etc. but how can I generate the name of the variable?

Link to comment
Share on other sites

This is line 60 

 

while ($row = $result->fetch_array()){

 

It is meant to be for a left side bar, the categories are in different tables.  It is meant to list the first category from the first table, then check to see if there are any categories listed in the second table.  If no categories in the second table, then it moves on and writes just 1 category in the side bar.  If there are categories in the second table, then it lists them in a pop up menu and so on until all the levels of the pop up menu are showing.

Link to comment
Share on other sites

The error is not line 60 any more, that was caused by a mistake now fixed.  The error is now line 11 which is in the function.  I have updated the code now.

 

function select($fullcat) {
  echo"fullcat: $fullcat";
  $sql="SELECT cat FROM $fullcat ORDER BY position";
             if($result=$mysqli->query($sql)){
               $count=+1;
               if($result->num_rows>0){
                 $level =+1;
                 echo<<<HTML
                 <div class="menutitles"
                 onmouseover="document.getElementById('$fullcat').style.display='block';document.getElementById('$fullcat2').style.display='block';"
                 onmouseout="document.getElementById('$fullcat').style.display='none';document.getElementById('$fullcat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$fullcat"></a><br/>
                 <div class="contain" id="$fullcat">
                 <div class="menuitems" id="$fullcat2">
HTML;
               select($fullcat);
               }else{$level=-1;
               if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$fullcat'></a><br/>
               ";
               }else{
               While($row=$result . $count->fetch_array()){
               $scat=replace($row[cat]);
               $fullcat="$fullcat" . $scat;
               echo"
               <a class='topseltxt2' href='gem.php?cat=$fullcat'></a><br/>
               ";}
               }
        }
      }
      return($fullcat);
}

 

$sql = "SELECT cat FROM dczcat1 ORDER BY position";
    if ($result = $mysqli->query($sql)){
      if ($result->num_rows > 0){
        $count=+1;
         while ($row = $result->fetch_array()){
           $scat=replace($row[cat]);
           $scat2=replace($row[cat]) . 2;
           $cat=$row[cat];
           $fullcat="dczcat1" . $scat;
           $fullcat2="dczcat1" . $scat . 2;
           


           $sql="SELECT cat FROM $fullcat ORDER BY position";
             if($result=$mysqli->query($sql)){
               $count=+1;
               if($result->num_rows>0){
                 $level =+1;
                 echo<<<HTML
                 <div class="menutitles"
                 onmouseover="document.getElementById('$fullcat').style.display='block';document.getElementById('$fullcat2').style.display='block';"
                 onmouseout="document.getElementById('$fullcat').style.display='none';document.getElementById('$fullcat2').style.display='block';"
                 >
                 <a class="topseltxt2" href="gem.php?cat=$fullcat"></a><br/>
                 <div class="contain" id="$fullcat">
                 <div class="menuitems" id="$fullcat2">
HTML;
               select($fullcat);
               }else{$level=-1;
               if($level==1){echo"
               <a class='topseltxt' href='gem.php?cat=$fullcat'></a><br/>
               ";
               }else{
               While($row=$result . $count->fetch_array()){
               $scat=replace($row[cat]);
               $fullcat="$fullcat" . $scat;
               echo"
               <a class='topseltxt2' href='gem.php?cat=$fullcat'></a><br/>
               ";}
               }
        }
      }echo"</div> </div> </div>";
    }
  }
}

Link to comment
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.