We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7455
    • 2,204 Posts
    Would it be posible to to have a snippet that first looks in the postdata from a link and then select an image related to that post data?

    if i would make a link like this:
    http://www.domainname.tld?image=nicecar


    and add this to a snippet to disply the right image?

    $image = $_GET[’image’];
    return "<img src=’assets/images/".$image.".jpg />"

    than this snippet would return this?: <img src=’assets/images/nicecar.jpg />

    Maybe this will work lets see
    I would only need a way to see if there is an image and when ther is is nit then it schould place a defauld image or random image script.

    Dim
      follow me on twitter: @dimmy01
      • 7455
      • 2,204 Posts
      ok this works:

      $image = $_GET['image'];
      return "<img src='assets/images/$image.jpg' />";
      


      now a way to find out if there is a post done or els post an devault image
        follow me on twitter: @dimmy01
        • 7455
        • 2,204 Posts
        OK work great now here it is for thos who needs it;

        //GetPostData
        //23dec2005 by Dimitri Hilverda
        //A small snippet to get post data from the url and place it.
        //Made this version for images but it could be used for any post data.
        //make a link like this on site 1 http://www.domainname.com/page.html?image=imagename
        //image name in url must be without an extention
        //edit this for extention:
        $ext= ".jpg";
        //edit this for default image when no post data is found:
        $defimg= "pand"; //no extention
        //endconfig
        
        $image = $_GET['image'];
        if (($image == '')) {
        $image = $defimg;
        }
        return "<img src='assets/images/$image$ext' />";
        
          follow me on twitter: @dimmy01
          • 32241
          • 1,495 Posts
          Hi,

          to clarify things a little bit, you’re using get solution, which you can use either form or simple link to access and change the parameter, like the one you use right now. To check whether it was set or not, use isset function.

          if(isset($_GET['image'])) {
            $image = $_GET['image'];
            return "<img src='assets/images/$image.jpg' />";
          }
          else {
            return "<img src='assets/images/$image.jpg' />";
          }
          


          If you want to use post, you can set the action on the form to post, which the parameter name and value will not appear on the address bar, and you can access it using $_POST[’image’], the same way as you did with GET.
            Wendy Novianto
            [font=Verdana]PT DJAMOER Technology Media
            [font=Verdana]Xituz Media
          • try "is_file" to check if the image requested exists

            http://il.php.net/is_file

            if(isset($_GET['image']) && is_file('assets/images/'.$_GET['image'].'jpg')) {
                return "<img src='assets/images/".$_GET['image']."jpg' />";
            }else{             // no image requested or requested image file does not exist
                return "<img src='assets/images/default.jpg' />";
            } 
            
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 7455
              • 2,204 Posts
              mm ok

              just merged my random image and getpostdata into one snippet, works nice only when postdata image does not exist i get a red cross maybe i could fix that with your code.

                follow me on twitter: @dimmy01
                • 32241
                • 1,495 Posts
                don’t you think that sometime PHP is not that consistent in defining their function name? For example isset and is_file. Why is_file using underscore, but yet isset is not.

                Anyway, it’s 5 am in here, that’s why I come up with this weird questions.
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 7455
                  • 2,204 Posts
                  OK used Susans code to check if the file exist and it works great now
                  When no post info or file is found it will act like a normal random image snippet, when post data is found and the file asosiated whith that data exist then it wil place that image.

                  Cool Thanks for the help
                  Dimmy


                  Snippet:

                  //GetPostImage
                  //23dec2005 by Dimitri Hilverda
                  //A small snippet to get post data from the url and place it.
                  //When no post data is found it wil place a random image from the same folder
                  //make a link like this on site 1 http://www.domainname.com/page.html?image=imagename
                  //image name in url must be without an extention
                  //edit this for extention: //Postdata only
                      $ext= ".jpg";
                  //Define what folder to use
                      $dir=opendir("assets/images/acc"); 
                      $directory=("assets/images/acc/"); 
                      $pattern=".(GIF|JPG|gif|jpg|jpeg|JPEG)$"; //for random part of the snippet
                  
                  //endconfig
                  
                  $image = $_GET['image'];
                  if(isset($_GET['image']) && is_file($directory.$_GET['image'].$ext)) {
                  $output= "<img src='$directory$image$ext' />";
                  }else{
                  
                      $s=readdir($dir); 
                      $count=0; 
                      $image = array(); 
                      while($s) 
                      { 
                          if(ereg($pattern, $s)) { 
                              $image[$count]=$s; 
                              $count++; 
                          } 
                          $s=readdir($dir); 
                      } 
                      closedir($dir); 
                  
                      //Spit it out 
                      srand(time()); 
                      $limit=count($image); 
                      $limit--; 
                      $randNum=mt_rand(0,$limit); 
                      $filename = "$image[$randNum]"; 
                  
                  
                    
                  $output= "<img src='$directory$filename'>";
                  }
                  return $output;
                  
                    follow me on twitter: @dimmy01
                    • 7455
                    • 2,204 Posts
                    Quote from: wendy at Dec 23, 2005, 10:51 AM

                    Hi,

                    to clarify things a little bit, you’re using get solution, which you can use either form or simple link to access and change the parameter, like the one you use right now. To check whether it was set or not, use isset function.

                    if(isset($_GET['image'])) {
                      $image = $_GET['image'];
                      return "<img src='assets/images/$image.jpg' />";
                    }
                    else {
                      return "<img src='assets/images/$image.jpg' />";
                    }
                    


                    If you want to use post, you can set the action on the form to post, which the parameter name and value will not appear on the address bar, and you can access it using $_POST[’image’], the same way as you did with GET.

                    Thanks Wendy

                    I do not use a form to submit the data but a direct link

                    I made a dealer portal with dealers that deals in more that 1 catacory products listed in this portal, so a dealers link could as wel be in the list of cars as in the list of boats now when somone kliks a link to that user in the catacory boats I would like to send them to the homepage of that dealer and show a boat not a car and visaversa.

                    With this snippet its posible now.

                    Thanks for the suggestions and help

                    Dimmy
                      follow me on twitter: @dimmy01
                    • Quote from: wendy at Dec 23, 2005, 11:05 AM

                      don’t you think that sometime PHP is not that consistent in defining their function name? For example isset and is_file. Why is_file using underscore, but yet isset is not.

                      Anyway, it’s 5 am in here, that’s why I come up with this weird questions.

                      That’s one of the more common criticisms of PHP. It is often very inconsistent in names and even in expected functionality.

                      http://tnx.nl/php

                      I’ve done perl...suffice it to say that even with all of its inconsistencies I decided to focus on php.
                        Studying MODX in the desert - http://sottwell.com
                        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                        Join the Slack Community - http://modx.org