Jump to content

Some pages show results and some don't for the same code


Gotharious

Recommended Posts

Hello,

 

Here's the weird thing, some of the pages shows the results, and some pages just won't show results at all, and I'm not getting any errors

 

here is the one that doesn't

 

<?
function display_mptt($user) {
global $db;
// retrieve the left and right value of the $root node

$sql2 = "SELECT * from mptt where id='25'";

$result2 = mysql_query($sql2 ,$db);
if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
echo '<h1>Users List</h1>';

// start with an empty $right stack
$right = array();

// now, retrieve all descendants of the $root node
  $sql = "SELECT * from mptt WHERE 'left' BETWEEN '".$row2['left']."' AND '".$row2['right']."' ORDER BY 'left' ASC";
$result = mysql_query($sql ,$db);

// display each row
  while ($row = mysql_fetch_array($result)or die(mysql_error())) {
  // only check stack if there is one
  $count = mysql_num_rows($result);
  if (count($right)>0) {
  // check if we should remove a node from the stack
  while ($right[count($right)-1]<$row['right']) {
  array_pop($right);
  }
  }
// display indented node title
  
// add this node to the stack

  
  $var3 = '10';
    echo "<table width='589' border='1'>
<tr>
<th>ID</th>
<th>Name</th>
</tr>";

echo  "<tr><td><a href=\"user.php?id=".$row['id']."\">".$row['id']."</a></td>";
echo "<td>" . $row['title'] ." </td>";

echo   "</tr>";

echo "</table>";
  $right[] = $row['right'];
  }
} 
  
  

   display_mptt(1);     ?>  

 

and here is the page that does

 

<? 
function display_mptt($user) {
global $db;
$id = $_GET['id'];
// retrieve the left and right value of the $root node
$sql2 = "SELECT * from mptt where id= ".$id."";

$result2 = mysql_query($sql2 ,$db);
if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
echo '<h1>Your Tree</h1>';

// start with an empty $right stack
$right = array();

// now, retrieve all descendants of the $root node
  $sql = "SELECT * from mptt WHERE `left` BETWEEN ".$row2['left']." AND ".$row2['right']." ORDER BY 'left' ASC";
$result = mysql_query($sql ,$db);

// display each row
  while ($row = mysql_fetch_array($result)) {
  // only check stack if there is one
  if (count($right)>0) {
  // check if we should remove a node from the stack
  while ($right[count($right)-1]<$row['right']) {
  array_pop($right);
  }
  }
// display indented node title
  echo str_repeat('      ',count($right)).$row['title']."<br>";
// add this node to the stack
  $right[] = $row['right'];
  }
} 
  
  display_mptt(1);
  ?>

Link to comment
Share on other sites

You have single-quotes ' ' around your `left` column name in your queries (both in the query that doesn't work and you even have them around the order by column name in the query that does work). That makes them string data. You need to use back-ticks `` not single-quotes ' ' around column names that happen to be mysql keywords.

 

You also have an or die(mysql_error()) on the end of a mysql_fetch_array() statement. Mysql_fetch_array doesn't set mysql_error, so all that will do is stop your code with no output when there are no rows to fetch. The or die(mysql_error()) should be on the end of your mysql_query() statement.

Link to comment
Share on other sites

Thanks a lot, mate.

that worked perfectly, tried the same with another page but that didn't work, I still have blank results

 

see the code below

<?php
if ($_GET['id']) {
  if(isset($_REQUEST['Submit'])) {
  $sql = "SELECT * FROM mptt where id=".$_GET['id']." ";
  $result = mysql_query($sql ,$db);
  $row = mysql_fetch_array($result);
  $right = $row['right'];
  // UPDATE RIGHT VALUES
  $sql = "UPDATE mptt SET `right`=`right`+2 WHERE `right` > ". ($right - 1);
  if(!$result = mysql_query($sql ,$db)) echo "$sql <br>".mysql_error();
  // UPDATE LEFT VALUES
  $sql = "UPDATE mptt SET `left`=`left`+2 WHERE `left` > ". ($right - 1);
  if(!$result = mysql_query($sql ,$db)) echo mysql_error();
  // INSERT NEW CATEGORY
  $sql = "INSERT INTO mptt (`left`,`right`,`title`) values ('".$right."', '".($right +1)."', '".$_REQUEST['title']."')";
  if(!$result = mysql_query($sql ,$db)) echo mysql_error();
  } else {
  ?>
  <table>
<form name="form1" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>">
<tr><td> Full Name: <input type="text" name="title"></td></tr>

<tr><td> Mobile: <input type="text" name="title"></td></tr>

<tr><td> National ID: <input type="text" name="title"></td></tr>

<tr><td> Password: <input type="text" name="title"></td></tr>

<tr><td> Type: <input type="text" name="title"></td></tr>
<tr><td><input type="submit" name="Submit" value="Submit"></td></tr>
</form>
</table>
<?
  }
  } 
function display_mptt($root) {
  global $db;
  echo '<table border="0" width="200">';
  // retrieve the left and right value of the $root node
  $sql2 = "SELECT * from mptt where id=".$root."";
  //$sql = "SELECT left,right FROM mptt WHERE `id`=1";
  $result2 = mysql_query($sql2 ,$db);
  if(!$row2 = mysql_fetch_array($result2)) echo mysql_error();
  echo '<h1>Adding Users</h1>';
  // start with an empty $right stack
  $right = array();
// now, retrieve all descendants of the $root node
  $sql = "SELECT * from mptt WHERE `left` BETWEEN '".$row2['left']."' AND '".$row2['right']."' ORDER BY `left` ASC";
  $result = mysql_query($sql ,$db)or die(mysql_error());
// display each row
  while ($row = mysql_fetch_array($result)) {
  // only check stack if there is one
  if (count($right)>0) {
  // check if we should remove a node from the stack
  while ($right[count($right)-1]<$row['right']) {
  array_pop($right);
  }
  }
// display indented node title
  echo '<tr><td>'.str_repeat('  ',count($right)).$row['title'].'</td><td><a href="'.$_SERVER['PHP_SELF'].'?id='.$row['id'].'">[ add ]</a></td></tr>';
// add this node to the stack
  $right[] = $row['right'];
  }
  echo '</table>';
} 
  
  display_mptt(1);
  echo "<hr>";
?>

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.