Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: help with php if statement

  1. #1
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default help with php if statement

    I have a page that I need to php include a file only IF a category has been selected.

    I'm not sure what syntax I need to use, I've tried both the following:

    PHP Code:
    <?php
       
    if ($cat1=='')
       echo 
    "Test";
       if (
    $cat1!='')
       include (
    "gallery.php") ;
       
    ?>
    and

    PHP Code:
    <?php 
    $cat1 
    $_POST['cat1'];
    if (!isSet(
    $_POST['cat1'])){
      echo 
    "<p>Please choose a category!</p>";
      }
    if (isSet(
    $_POST['cat1'])){
    include (
    "gallery.php") ;
    }
    ?>
    Both echo the statements when the category is not selected and both display the file regardless...

    Any advice would be appreciated!
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  2. #2
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    If category selector is set via HTTP request, than forget about method 1. If it is set over POST request that the second one will do. If via GET request - both methods will not work.
    Calling an URL like domain.com/test_gallery.php?cat=1 is a GET HTTP request and the code will be like this:
    if (isset($_GET['cat])) {
    include whatever you should
    } else {
    echo 'an error';
    }
    Anyway, regarding your second example - it is impossible for a variable to be set and unset in the same time. In the case of $_POST request - the second example will work.

    Also, turn on all errors in PHP - you'll got pleanty of notices about your code and line numbers with descriptions of possible problems.

  3. #3
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    Dmitriy -
    Thank you so much!!! This worked perfectly:

    PHP Code:
    <?php
       
    if (isset($_POST['cat1'])) {
       include 
    "gallery.php";
       } else {
       echo 
    "Please select a category";
       } 
       
    ?>
    How do I turn on errors for the page?

    Thanks again!
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  4. #4
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    If I wanted to set a category based off one not being selected, like $cat1=="recent";
    how would I POST that information to the next page?
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  5. #5
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    >How do I turn on errors for the page?

    error_reporting (E_PARSE | E_ERROR );

    As for your cat question, I dont understand what you are asking.

  6. #6
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    Welcom, JM

    I'd suggest using E-NOTICE too at dev stage.

    As for the posting vars. I can't understand your question either But a few guesses: If you're using a form on the next page, you can add something like

    <input type="hidden" name="cat" value="category">
    And use $_POST['cat] again.

    You can also use sessions or cookies. Depends on the situation.

  7. #7
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    Here's the navigation:
    1st page: Select Product (goes to the next page with the GET I think)

    2nd Page: Select Category
    this page is created by a gallery script which loops through directories based off the prodID and the catID which is selected.
    I used your code suggestion above to tell the gallery script to display thumbnails from the directory prodID/recent/ if no cat had been selected.

    3rd page: Select Layout
    this displays the layout with the background selected from the previous page.
    If there was a category selected it works perfectly and posts the information to the next page for the designer...however, if the category images are displayed from the directory "recent", the images don't show up as the background, and it doesn't post to the next page.

    Here's the gallery script for reference:
    PHP Code:
    <?php
    function createThumbs$pathToImages$pathToThumbs$thumbWidth 
    {
      
    // open the directory
      
    $dir opendir$pathToImages );
      
    // loop through it, looking for any/all JPG files:
      
    while (false !== ($fname readdir$dir ))) {
        
    // parse path for the extension
        
    $info pathinfo($pathToImages $fname);
        
    // continue only if this is a JPEG image
        
    if ( strtolower($info['extension']) == 'jpg' 
        {
         
    // echo "Creating thumbnail for {$fname} <br />";
          // load image and get image size
          
    $img imagecreatefromjpeg"{$pathToImages}{$fname});
          
    $width imagesx$img );
          
    $height imagesy$img );
          
    // calculate thumbnail size
          
    $new_width $thumbWidth;
          
    $new_height floor$height * ( $thumbWidth $width ) );
          
    // create a new temporary image
          
    $tmp_img imagecreatetruecolor$new_width$new_height );
          
    // copy and resize old image into new image 
          
    imagecopyresized$tmp_img$img0000$new_width$new_height$width$height );
          
    // save thumbnail into a file
          
    imagejpeg$tmp_img"{$pathToThumbs}{$fname});
        }
      }
      
    // close the directory
      
    closedir$dir );
    }
    // call createThumb function and pass to it as parameters the path 
    // to the directory that contains images, the path to the directory
    // in which thumbnails will be placed and the thumbnail's width. 
    // We are assuming that the path will be a relative path working 
    // both in the filesystem, and through the web for links
    $cat1 $_POST['cat1'];
    if (isSet(
    $_POST['cat1'])){
      
    createThumbs("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/",200);
     }
     else {
      
    createThumbs("prodimages/{$rs['pId']}/recent/","gallery/{$rs['pId']}/recent/",200);
       } 
     
    ?>
    <?php
    function createGallery$pathToImages$pathToThumbs 
    {
      
    $output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
      
    $output .= "<tr>";
      
    // open the directory
      
    $dir opendir$pathToThumbs );
      
    $counter 0;
      
    // loop through the directory
      
    while (false !== ($fname readdir($dir)))
      {
        
    // strip the . and .. entries out
        
    if ($fname != '.' && $fname != '..'
        {
          
    $output .= "<td valign=\"middle\" align=\"center\"><input name=\"optn0\" type=\"radio\" class=\"radio\" id=\"{$fname}\" onclick=\"updateoptimage(0,2,0,1);updateprice0();\" value=\"{$fname}\" />";
          
    $output .= "<input type=\"image\" src=\"{$pathToThumbs}{$fname}\" alt=\"{$fname}\" id=\"img{$fname}\" border=\"0\" style=\"cursor:pointer\" onclick=\"document.getElementById('{$fname}').checked=true;\" />";
          
    $output .= "</td>";
     
    value=\"/select_category.php\" />";
          
    $counter += 1;
          if ( 
    $counter == ) { $output .= "</tr><tr>"; }
        }
      }
      
    // close the directory
      
    closedir$dir );
      
    $output .= "</tr>";
      
    $output .= "</table>";
     
       
    // open the file
      
    $fhandle fopen"gallery.html""w" );
      
    // write the contents of the $output variable to the file
      
    fwrite$fhandle$output ); 
      
    // close the file
      
    fclose$fhandle );
    }
    // call createGallery function and pass to it as parameters the path 
    // to the directory that contains images and the path to the directory
    // in which thumbnails will be placed. We are assuming that 
    // the path will be a relative path working 
    // both in the filesystem, and through the web for links
    $cat1 $_POST['cat1'];
    if (isSet(
    $_POST['cat1'])){
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");
     }else {
     
    createGallery("prodimages/{$rs['pId']}/recent/","gallery/{$rs['pId']}/recent/",200);
       } 
     
    ?>
    Last edited by rlhanson; 05-30-2009 at 09:39 AM.
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  8. #8
    Dmitriy is offline Nearly a Master Glow Jedi
    Join Date
    Feb 2007
    Location
    Ukraine
    Posts
    124

    Default

    Than you have the category on the second page in $_GET['cat]. I see no problem.

  9. #9
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    I basically want this:
    PHP Code:
    <?php 
    $cat1 
    $_POST['cat1'];
    if (isSet(
    $_POST['cat1'])){
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/");
     }else {
    //tell script that $cat1 is "recent"
     
    createGallery("prodimages/{$rs['pId']}/{$cat1}/","gallery/{$rs['pId']}/{$cat1}/",200);
       } 
    ?>
    Thank you,
    Lynne Hanson
    RL Hanson-Online

  10. #10
    rlhanson's Avatar
    rlhanson is offline Master Glow Jedi
    Join Date
    Aug 2007
    Location
    Chapman, Kansas
    Posts
    531

    Default

    I've not used GET before, and as it is now, I am POSTing all over the place to go from one page to another! lol
    I'm going to think about this for a bit and read some info on the GET statments...

    thanks so much to both of you!
    Thank you,
    Lynne Hanson
    RL Hanson-Online

Page 1 of 2 12 LastLast

Similar Threads

  1. Referencing a PHP Class inside a PHP Include
    By rickpugh in forum Programming Talk
    Replies: 1
    Last Post: 01-22-2006, 08:30 AM

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14