PHP - Foreach And Switch Statements.

Status
Not open for further replies.

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
Hey,

So I'm doing a search thingy and, user choose where to search inside site and it's done by ajax. I'm assuming if I show you code you'd understand better.
Ajax:
Code:
$(document).ready(function() {
                $('#searchBtn').click(function() {
                    var text = $('input#seachText').val();
                   
                    var where = $('#where'); 
                    var where_text = where.text();
                    console.log(where_text);
                    $.ajax({
                        url:  document.location+ '/habbo_archive/system/js/search.php',
                        method:'POST',
                        data: {
                            text:text,
                            where: where_text
                        },
                        beforeSend: function() {
                            $('#searchResults').text('Searching...'); 
                        },
                        success:function(results) {
                            $('#searchResults').text('');
                            $('#searchResults').html(results);
                        }
                    }); 
                });
            });
Classic AJAX request. Nothing too fancy.
PHP Code:
PHP:
<?php 

sleep(2);
if(!$_POST) {
    die('hell no');
}

include('../config.php');

$where_post = $_POST['where']; 
$text = $_POST['text']; 
$folders = $db->prepare("SELECT * FROM folders"); 
    $folders->execute();
    $folders = $folders->fetchAll();
switch ($where_post) {
   
    foreach($folders as $folder){
        case $row: 
            $where = $row
            break; 
            default : 
            $where = $row; 
            break;
    } 
}

$files = glob("../".$where."/*". $text ."*.*");
//var_dump(glob('*'));
if(count($files) > 0){
     foreach($files as $file){                  
     var_dump($files);
            }
}
else  {
    die('no files found');
}


 ?>
So the error is, I want to use foreach inside switch but it does not let me to do.

Any alternatives how may I do that?

Thanks.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Hey,

So I'm doing a search thingy and, user choose where to search inside site and it's done by ajax. I'm assuming if I show you code you'd understand better.
Ajax:
Code:
$(document).ready(function() {
                $('#searchBtn').click(function() {
                    var text = $('input#seachText').val();
               
                    var where = $('#where');
                    var where_text = where.text();
                    console.log(where_text);
                    $.ajax({
                        url:  document.location+ '/habbo_archive/system/js/search.php',
                        method:'POST',
                        data: {
                            text:text,
                            where: where_text
                        },
                        beforeSend: function() {
                            $('#searchResults').text('Searching...');
                        },
                        success:function(results) {
                            $('#searchResults').text('');
                            $('#searchResults').html(results);
                        }
                    });
                });
            });
Classic AJAX request. Nothing too fancy.
PHP Code:
PHP:
<?php

sleep(2);
if(!$_POST) {
    die('hell no');
}

include('../config.php');

$where_post = $_POST['where'];
$text = $_POST['text'];
$folders = $db->prepare("SELECT * FROM folders");
    $folders->execute();
    $folders = $folders->fetchAll();
switch ($where_post) {
 
    foreach($folders as $folder){
        case $row:
            $where = $row
            break;
            default :
            $where = $row;
            break;
    }
}

$files = glob("../".$where."/*". $text ."*.*");
//var_dump(glob('*'));
if(count($files) > 0){
     foreach($files as $file){              
     var_dump($files);
            }
}
else  {
    die('no files found');
}


 ?>
So the error is, I want to use foreach inside switch but it does not let me to do.

Any alternatives how may I do that?

Thanks.
It would help if you actually put a case under the switch statement instead of just doing a foreach?

Otherwise your foreach should go ABOVE the switch statement...
Code:
    foreach($folders as $folder){
switch ($where_post) {
        case $row:
            $where = $row
            break;
            default :
            $where = $row;
            break;
    }
}

Why are you using a switch statement with only 1 case though?
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
It would help if you actually put a case under the switch statement instead of just doing a foreach?

Otherwise your foreach should go ABOVE the switch statement...
Code:
    foreach($folders as $folder){
switch ($where_post) {
        case $row:
            $where = $row
            break;
            default :
            $where = $row;
            break;
    }
}

Why are you using a switch statement with only 1 case though?
Yeah just figured that out, thanks anyway :D
 
Status
Not open for further replies.

Users who are viewing this thread

Top