4 Replies - 113 Views - Last Post: 26 December 2014 - 01:23 PM Rate Topic: -----

#1 bath_robe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 14

SQL While Loop inside a function

Posted 26 December 2014 - 11:31 AM

I am attempting to make a file tree system.
It's a little complicated do I will do my best to explain with comments.

This is what I have so far

//init folders
//sql
$sql = $conn -> prepare("SELECT * FROM folders WHERE projectId = :projectId AND parent_folder = 'none'");
$sql -> execute(array(':projectId' => $projectId));

//loop through first row of folders
while($row = $sql -> fetch(PDO::FETCH_ASSOC)) {
  $folderId   = $row['id'];
  $folderName = $row['name'];
  echo '  
  <li oncontextmenu="openFolderContextMenu(event, \''.$folderId.'\', 1);return false;" ondrop="drop(event, \''.$folderId.'\')" ondragover="allowDrop(event)" draggable="true" ondragstart="dragFolder(event, \''.$folderId.'\')">
    <span class = "folder-img">
      <img src = "http://novuta.com/img/folder.png">
    </span>
    <span id = "folder-name-'.$folderId.'">
      '.$folderName.'
    </span>
  </li>';
  //echo all folders function
  //pass $folderId which is the id of the folder that the files will be in
  displayFiles($folderId);
}


That is the code to display the first row of folders.
The displayFiles function calls this

//dispaly files function
function displayFiles($parentFolderId) {
  global $conn;
  $sql = $conn -> prepare("SELECT * FROM extrafiles WHERE project_id = :projectId AND parent_folder = :parentFolder");
  //parent folder is how I am defining what folder the files are in
  $sql -> execute(array(':projectId' => $projectId,
                        ':parentFolder' => $parentFolderId));
  //nothing is echoing, no files or errors.
  while($row = $sql -> fetch(PDO::FETCH_ASSOC)) {
    $fileId   = $row['id'];
    $fileName = $row['name'];
    $fileType = $row['file_type'];
    echo '  
    <li oncontextmenu="openFileContextMenu(event, \''.$fileId.'\', \''.$fileType.'\');return false;" draggable="true" ondragstart="dragFile(event, \''.$fileId.'\')" ondrop="drop(event, \'none\')" ondragover="allowDrop(event)" onclick = "openFile(\''.$fileId.'\')">
      <span class = "file-img deep-1">
        <img src = "http://novuta.com/img/html_file.png">
      </span>
      <span id = "file-name-'.$fileId.'">
        '.$fileName.'.'.$fileType.'
      </span>
    </li>';
  }
  
}


That is what I have so far
The displayFiles function is supposed to display all the files that are inside the folder that called the displayFiles function.
The problem is, the function is being called fine and all the variables are there but the sql while look seem to not be doing anything.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: SQL While Loop inside a function

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: SQL While Loop inside a function

Posted 26 December 2014 - 12:41 PM

What is the value of $projectId within your displayFiles function? You have echo, print_r and var_dump to assist with debugging.

You can also add the following to the top of your scripts to see all errors during development:
error_reporting(E_ALL);
ini_set('display_errors', '1');

Was This Post Helpful? 1
  • +
  • -

#3 bath_robe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-April 14

Re: SQL While Loop inside a function

Posted 26 December 2014 - 01:13 PM

View Postandrewsw, on 26 December 2014 - 12:41 PM, said:

What is the value of $projectId within your displayFiles function? You have echo, print_r and var_dump to assist with debugging.

You can also add the following to the top of your scripts to see all errors during development:
error_reporting(E_ALL);
ini_set('display_errors', '1');



Once I saw the error projectId was not defined in the function. I needed to add that to the global variables.
Thanks :)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3838
  • View blog
  • Posts: 13,595
  • Joined: 12-December 12

Re: SQL While Loop inside a function

Posted 26 December 2014 - 01:17 PM

Glad you saw that ;)
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5941
  • View blog
  • Posts: 12,870
  • Joined: 16-October 07

Re: SQL While Loop inside a function

Posted 26 December 2014 - 01:23 PM

View Postbath_robe, on 26 December 2014 - 04:13 PM, said:

I needed to add that to the global variables.


No. You need less globals. Seriously. Use functions instead and pass parameters.

I'd start with something like:
// this is a nice thing to have
function showDebug($sql, $stmt, $params) {
    if (DEBUG) {
        echo "SQL: $sql\n"; 
        var_dump($params);
        var_dump($stmt->errorInfo());
        echo "\n"; 
    }
}

function getDb() {
    // global conn, bad
    // your code here
}

function getRows($sql, $params) {
    $stmt = getDb()->prepare($sql);
    if ($stmt->execute($params)) { return $stmt->fetchAll(); }
    showDebug($sql, $stmt, $params);
    return null;
}

function getFolders($projectId, $parentFolderId) {
    $sql = "SELECT * FROM folders WHERE project_id = :projectId AND parent_folder = :parentFolder";
    return getRows(array(':projectId' => $projectId, ':parentFolder' => $parentFolderId));
}


function getFiles($projectId, $parentFolderId) {
    $sql = "SELECT * FROM extrafiles WHERE project_id = :projectId AND parent_folder = :parentFolder";
    return getRows(array(':projectId' => $projectId, ':parentFolder' => $parentFolderId));
}

function showFolder($projectId, $folderId, $folderName, $parentFolderId) {
    // your code here
}



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1