SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Switch with an if statement problem

    Guys,

    I've been trying to get this right for hours and need help:

    This page originally took a series of values and built a table (in a table view page - not my design!). The amendments I made changed the page so that the order of injection was shuffled so that the names ran alphabetically down the page...

    I think I did pretty good...

    Problem is this: You have to 'array_fill' the final column to keep the layout and order consistent...

    When the table generates it now generates the padding cell with an image and no writing...

    I'm trying to find a logical way to say that if the $field ==" extra" (which is the 'fill value'), just return an empty cell...

    No matter how I've twisted the logic, I just can't get it to happen...any one know a way?

    (NB I know that if and else statements take the form if(condition){code to be executed}, the way it currently is is just one of the many variations I've tried, as curly brackets then get described as 'unexpected')

    Really, it's 'case 3' that's the problem: how do you put an if statement in there...??

    Many thanks...

    Jim

    PHP Code:
    <?
    @session_start();

    require(
    'includes/global_confiq.php');
    require(
    'includes/commonClass.php');
    $ClassObject = new commonclass ;

    //$user=$_REQUEST['UserName'];
    //$pass=$_REQUEST['PassWord'];
    /*this is for validating login*/



    //if(isset($_REQUEST['Submit'])&&$_REQUEST['Submit']=='Submit')
    //{
      
    $ClassObject->getGroupTable() ;
      
    //$Condition="emailaddress='".$user."' and pwd='".$pass."'";
      
    $SelQuery=$ClassObject->selectSql($Condition);
      
    $SelQueryRS=$ClassObject->cmdResult($SelQuery);
      
    $NumOfRecords=$ClassObject->rowNum($SelQueryRS);
    // echo $NumOfRecords; die();

        
    if($NumOfRecords>0)
      {
           
    $rec_arr=$ClassObject->fetchData($SelQueryRS);
        
    $input=$rec_arr;

       
    //Re-alphabetise the array to take account of newly entered records...
       
       
    function subval_sort($a,$subkey) {
            foreach(
    $a as $k=>$v) {
                
    $b[$k] = strtolower($v[$subkey]);
            }
            
    asort($b);
            foreach(
    $b as $key=>$val) {
                
    $c[] = $a[$key];
            }
            return 
    $c;
        }
       
        
    $ordered subval_sort($input,'groupname'); 
        
     
    //finding the number necessary for division of array to produce 4 roughly even columns (last column may not be full at this stage)...
        
    $mn=ceil($NumOfRecords/4);
        

    //performing the split and assigning each group to a variable...

        
    $col1=array_slice($ordered0$mn);
        
    $col2=array_slice($ordered$mn$mn);
        
    $col3=array_slice($ordered, (2*$mn), $mn);
        
    $precol4=array_slice($ordered, (3*$mn), $mn);

    //stuff $precoll4 to full size if required...
        
    $count=count($precol4);
        
        if (
    $count<$mn){
        
    $shortfall=$mn-$count;
        
    $start_pos=($mn-$shortfall)+1;
        
    $fill=array_fill($start_pos$shortfall" extra");
        
    $col4=array_merge((array)$precol4, (array)$fill); 
        }

     

    //Make key arrays

        
    foreach ($col1 as $key => $value) {
            
    $ind1[]=($key*4);
                   }
            
        foreach (
    $col2 as $key => $value) {
            
    $ind2[]=($key*4)+1;
                   }
            
        foreach (
    $col3 as $key => $value) {
            
    $ind3[]=($key*4)+2;
                    }
            
        foreach (
    $col4 as $key => $value) {
            
    $ind4[]=($key*4)+3;
                    }



    //Merge the key arrays
        
    $newind array_merge((array)$ind1, (array)$ind2, (array)$ind3, (array)$ind4);
        
    //merge the treated columns...

        
    $newcol array_merge((array)$col1, (array)$col2, (array)$col3, (array)$col4);
        
    //combine the arrays...

        
    $final=array_combine((array)$newind, (array)$newcol);



    //Sort the array by key number...

        
    ksort($final);
            
    //Voila! The finished article - $final is now injected back into the flow of the page.

        
    $temp_count=0;
       
        foreach(
    $final as $key=>$field)
        {
            
            
              switch(
    $temp_count){
            
            case(
    $temp_count==0):
            
    $CategoryData.="</tr><tr><td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            break;
            
            case(
    $temp_count==1):
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;
            break;
            
            case(
    $temp_count==2):
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;
            break;
            
            case(
    $temp_count==3):
                if(
    $field==" extra")$CategoryData.="<td>Hello</td>";$temp_count++;
            if(!
    $field==" extra")
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;
            break;

            case(
    $temp_count==4):
            
    $CategoryData.="</tr><tr><td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count=1;
            break;}
           }
      }
      else
      {
          
    $CategoryData.="<td colspan=4 class='error_text'>Groups Does Not Exist...</td></tr>";
      }

    include_once(
    "html/client/header.html");
    include_once(
    "html/client/directory.html");
    include_once(
    "html/client/footer.html");


    ?>

  2. #2
    SitePoint Enthusiast Linkoroo's Avatar
    Join Date
    Jul 2009
    Location
    linkoroo.com
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you sure you're not missing some {} for your if?

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Your 'cases' should look like this:
    PHP Code:
    case 0:
      ....
      break;
    case 
    1:
      ....
      break;
    etc

  4. #4
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Linkoroo View Post
    are you sure you're not missing some {} for your if?
    Hi Linkoroo,

    As I said earlier; I did originally have them encased in {}, but that complicated matters - out of preference I would always have them in {}: that's just the way it's done...


    @Guido - good point: still works the other way though...without the if condition it still rendered perfectly...

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,510
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Without the brackets like Linkoroo pointed out, you add 2 to temp_count.

    Btw, what is the result of the script as it is?

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Location
    London
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's funny...I swear I did this last night about 4 times, but:

    PHP Code:
    <?
    @session_start();

    require(
    'includes/global_confiq.php');
    require(
    'includes/commonClass.php');
    $ClassObject = new commonclass ;

    //$user=$_REQUEST['UserName'];
    //$pass=$_REQUEST['PassWord'];
    /*this is for validating login*/



    //if(isset($_REQUEST['Submit'])&&$_REQUEST['Submit']=='Submit')
    //{
      
    $ClassObject->getGroupTable() ;
      
    //$Condition="emailaddress='".$user."' and pwd='".$pass."'";
      
    $SelQuery=$ClassObject->selectSql($Condition);
      
    $SelQueryRS=$ClassObject->cmdResult($SelQuery);
      
    $NumOfRecords=$ClassObject->rowNum($SelQueryRS);
    // echo $NumOfRecords; die();

        
    if($NumOfRecords>0)
      {
           
    $rec_arr=$ClassObject->fetchData($SelQueryRS);
        
    $input=$rec_arr;

       
    //Re-alphabetise the array to take account of newly entered records...
       
       
    function subval_sort($a,$subkey) {
            foreach(
    $a as $k=>$v) {
                
    $b[$k] = strtolower($v[$subkey]);
            }
            
    asort($b);
            foreach(
    $b as $key=>$val) {
                
    $c[] = $a[$key];
            }
            return 
    $c;
        }
       
        
    $ordered subval_sort($input,'groupname'); 
        
     
    //finding the number necessary for division of array to produce 4 roughly even columns (last column may not be full at this stage)...
        
    $mn=ceil($NumOfRecords/4);
        

    //performing the split and assigning each group to a variable...

        
    $col1=array_slice($ordered0$mn);
        
    $col2=array_slice($ordered$mn$mn);
        
    $col3=array_slice($ordered, (2*$mn), $mn);
        
    $precol4=array_slice($ordered, (3*$mn), $mn);

    //stuff $precoll4 to full size if required...
        
    $count=count($precol4);
        
        if (
    $count<$mn){
        
    $shortfall=$mn-$count;
        
    $start_pos=($mn-$shortfall)+1;
        
    $fill=array_fill($start_pos$shortfall" extra");
        
    $col4=array_merge((array)$precol4, (array)$fill); 
        }

     

    //Make key arrays

        
    foreach ($col1 as $key => $value) {
            
    $ind1[]=($key*4);
                   }
            
        foreach (
    $col2 as $key => $value) {
            
    $ind2[]=($key*4)+1;
                   }
            
        foreach (
    $col3 as $key => $value) {
            
    $ind3[]=($key*4)+2;
                    }
            
        foreach (
    $col4 as $key => $value) {
            
    $ind4[]=($key*4)+3;
                    }



    //Merge the key arrays
        
    $newind array_merge((array)$ind1, (array)$ind2, (array)$ind3, (array)$ind4);
        
    //merge the treated columns...

        
    $newcol array_merge((array)$col1, (array)$col2, (array)$col3, (array)$col4);
        
    //combine the arrays...

        
    $final=array_combine((array)$newind, (array)$newcol);



    //Sort the array by key number...

        
    ksort($final);
            
    //Voila! The finished article - $final is now injected back into the flow of the page.

        
    $temp_count=0;
       
        foreach(
    $final as $key=>$field)
        {
              switch(
    $temp_count){
            
            case(
    0):
            
    $CategoryData.="</tr><tr><td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count=1;
            break;
            
            case(
    1):
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;
            break;
            
            case(
    2):
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;
            break;
            
            case(
    3):
                if(
    $field==" extra"){$CategoryData.="<td>&nbsp;</td>";$temp_count++;}
            else{
            
    $CategoryData.="<td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count++;}
            break;

            case(
    4):
            
    $CategoryData.="</tr><tr><td class='normaltext'><img src='images/b_02.gif'/>&nbsp;<a href='category.php?id=".$field[id]."&group=".strtoupper($field[groupname])."' target='_self' class='link'>".strtoupper($field[groupname])."</a></td>";
            
    $temp_count=1;
            break;}
           }
      }
      else
      {
          
    $CategoryData.="<td colspan=4 class='error_text'>Groups Does Not Exist...</td></tr>";
      }

    include_once(
    "html/client/header.html");
    include_once(
    "html/client/directory.html");
    include_once(
    "html/client/footer.html");


    ?>
    SOLVED: Thanks guys...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •