heldenbrau Posted October 31, 2010 Share Posted October 31, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/ Share on other sites More sharing options...
darkfreaks Posted October 31, 2010 Share Posted October 31, 2010 can you comment line 60 so we know exactly what line to debug Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128722 Share on other sites More sharing options...
jcbones Posted October 31, 2010 Share Posted October 31, 2010 So you only want to fetch the array if the query doesn't return rows? At least, that is how the code is written. Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128725 Share on other sites More sharing options...
heldenbrau Posted October 31, 2010 Author Share Posted October 31, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128752 Share on other sites More sharing options...
heldenbrau Posted October 31, 2010 Author Share Posted October 31, 2010 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>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128759 Share on other sites More sharing options...
BlueSkyIS Posted October 31, 2010 Share Posted October 31, 2010 $result is not an object. where is the class for $mysqli defined? and when it's function query() is called, does it return an object? Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128760 Share on other sites More sharing options...
BlueSkyIS Posted October 31, 2010 Share Posted October 31, 2010 in the function select(), $mysqli is undefined. you either need to pass it to the function or declare it global. Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128761 Share on other sites More sharing options...
heldenbrau Posted October 31, 2010 Author Share Posted October 31, 2010 Thanks pineapple, that $mysqli was the problem, it's sort of working now, cheers. Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128767 Share on other sites More sharing options...
heldenbrau Posted October 31, 2010 Author Share Posted October 31, 2010 Btw though, is there any way to generate variable names like what I originally wanted to do? Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128768 Share on other sites More sharing options...
BlueSkyIS Posted October 31, 2010 Share Posted October 31, 2010 If I understand correctly: // Create variable $s1 through $s10, making each one a random number for ($i=1;$i<=10;$i++) { ${'s'.$i} = rand(); } Quote Link to comment https://forums.phpfreaks.com/topic/217380-can-you-generate-variable-labels-eg-s1-s2-s3/#findComment-1128773 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.