Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10

Thread: Small Code Help

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    61
    Member #
    30162
    Liked
    1 times

    Small Code Help

    Hi There,

    Is anyone able to write some code short?



    I'm looking for something where you type in the word which illustrates an image for example red, for a red flag image.

    When the word is typed in to a form text box an image of the red flag will appear on the admin side and a section on the website. But to make is appear you will need one password to activate it.


    See image attached image to get the idea.

    When the submit button has been pressed, where the green square it is it will change to the image that was selected


    Hope someone would be able to make something like this and wont take too long to do.
    Please write comments explaining what each part does. For me to understand how it works.
    Attached Images Attached Images

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    I'm sorry that I don't understand what you're trying to do.

    Is it like a captcha?

    What is the purpose?

    What is an "admin side"?


  4. #3
    Member
    Join Date
    Dec 2011
    Posts
    61
    Member #
    30162
    Liked
    1 times
    It is for a river status for water sports.

    Using 2 colored flag images that I have made which are green and red. Green meaning safe to go on the water red meaning to it is dangerous and do not go out on the water.

    I need the admin bit to be something like the image above (i can do the css). So they type in the color which will trigger the flag status, but so no randomer changes the color it will have one single password that will be given to the safety people.

    I will need the image to be show on a different page for the public to see.

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    See the code below.

    You'll have to change the HTML portion to display your flag image instead of the text.
    Instead of echoing the status, you'll be using <img src ...> to display image.

    The value of the submit button will change depending on the state of the flag.

    PHP Code:
    <?php

    // The name of the water status file:
    $fname="safe!water.txt";

    // The secret code to switch flags:
    $secret="flag!123";

    // Make sure the water status file exists.
    // The first time you run it, the file won't exist.
    // If the file does exist, get the current value stored in the file.
    if(file_exists($fname)){
    list(
    $safe)=file($fname);
    }
    else{
    // File does not yet exist, so create it ...
    // It will start out with a safe code of 1.
    $fh fopen($fname'w+') or die("can't open file");
    fwrite($fh"1");
    fclose($fh);
    $safe=1;
    }

    // Get the code from the form if the user has submitted it.
    // If someone clicks submit without entering any code, ignore the form.
    $code="";
    if(isset(
    $_POST['code'])){
    $code=$_POST['code'];
    }

    // If proper code entered, switch flags ...
    if($code == $secret){
        if(
    $safe == 1){
            
    $fh fopen($fname'w') or die("can't open file");
            
    fwrite($fh"2");
            
    fclose($fh);
            
    $safe=2;
        }
        else{
        if(
    $safe == 2){
            
    $fh fopen($fname'w') or die("can't open file");
            
    fwrite($fh"1");
            
    fclose($fh);
            
    $safe=1;
        }
    }
    }

    // Determine current status of flag ...
    // These variables will be for your HTML display.
    if($safe==1){
    $status="Flag is now GREEN";
    $switch="Change flag to RED";
    }
    if(
    $safe==2){
    $status="Flag is now RED";
    $switch="Change flag to GREEN";
    }
    ?>

    <!--  Your HTML Portion with your CSS is located here, below the PHP script -->
    <br />
    <?php echo $status?>
    <br />
    <form method="POST">
    Code to change flag: <input type="text" name="code">
    <input type="submit" name="submit" value="<?php echo $switch?>">
    </form>
    Last edited by mlseim; Nov 15th, 2014 at 02:50 PM.


  6. #5
    Member
    Join Date
    Dec 2011
    Posts
    61
    Member #
    30162
    Liked
    1 times
    Thank you so much for this.

    What code do I use to allow just the flag to appear in the header of the website?
    Last edited by TheTwister; Nov 15th, 2014 at 03:56 PM.

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Save this file as "water_status.php";
    PHP Code:
    <?php

    // The name of the water status file:
    $fname="safe!water.txt";

    if(
    file_exists($fname)){
    list(
    $safe)=file($fname);
    }

    // Determine current status of flag ...
    // Display the correct flag in header ...
    if($safe==1){
    echo 
    "<img src='images/greenflag.jpg' />";
    }
    if(
    $safe==2){
    echo 
    "<img src='images/redflag.jpg' />";
    }
    ?>
    I of course have no way of knowing where your flag images are, or their filenames ....

    Then add this to your header where you want it to appear ...

    <?php include("water_status.php')?>


    You didn't mention anything about your website...
    are you currently using PHP, is it a WordPress site?

    If it's a file like "index.html", you won't be able to execute PHP unless your server has .html filetypes configured for PHP.

    How much experience do you have at scripting?


  8. #7
    Member
    Join Date
    Dec 2011
    Posts
    61
    Member #
    30162
    Liked
    1 times
    Yeah it is a WordPress site.

    It's all done now! Thank you soo much for this

    I have one more small thing that I tried doing but it didn't work. How do you make it so if the code is wrong it brings an alert saying code is wrong. Also the same thing with after submitting, status has been updated?

    The include also doesn't appear in the header. include ('./status/water_status.php'); ?>

    .
    Last edited by TheTwister; Nov 16th, 2014 at 11:04 AM.

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    If the code is wrong, the flag won't change. If it's successful, the flag changes.

    But below would add a message ...

    PHP Code:

    <?php

    // The name of the water status file:
    $fname="safe!water.txt";

    // The secret code to switch flags:
    $secret="flag!123";

    // Make sure the water status file exists.
    // The first time you run it, the file won't exist.
    // If the file does exist, get the current value stored in the file.
    if(file_exists($fname)){
    list(
    $safe)=file($fname);
    }
    else{
    // File does not yet exist, so create it ...
    // It will start out with a safe code of 1.
    $fh fopen($fname'w+') or die("can't open file");
    fwrite($fh"1");
    fclose($fh);
    $safe=1;
    }

    // Get the code from the form if the user has submitted it.
    // If someone clicks submit without entering any code, ignore the form.
    $code="xxx";
    $message="Waiting for code ...";
    if(isset(
    $_POST['code'])){
    $message="Invalid Code - try again!";
    $code=$_POST['code'];
    }

    // If proper code entered, switch flags ...
    if($code == $secret){
    $message="Flag has been changed!";
        if(
    $safe == 1){
            
    $fh fopen($fname'w') or die("can't open file");
            
    fwrite($fh"2");
            
    fclose($fh);
            
    $safe=2;
        }
        else{
        if(
    $safe == 2){
            
    $fh fopen($fname'w') or die("can't open file");
            
    fwrite($fh"1");
            
    fclose($fh);
            
    $safe=1;
        }
    }
    }

    // Determine current status of flag ...
    // These variables will be for your HTML display.
    if($safe==1){
    $status="Flag is now GREEN";
    $switch="Change flag to RED";
    }
    if(
    $safe==2){
    $status="Flag is now RED";
    $switch="Change flag to GREEN";
    }
    ?>

    <!--  Your HTML Portion with your CSS is located here, below the PHP script -->
    <br />
    <?php echo $status?>
    <br />
    <?php echo $message?>
    <br />
    <form method="POST">
    Code to change flag: <input type="text" name="code">
    <input type="submit" name="submit" value="<?php echo $switch?>">
    </form>


  10. #9
    Member
    Join Date
    Dec 2011
    Posts
    61
    Member #
    30162
    Liked
    1 times
    Thank you, this is the last thing. The include also doesn't appear in the header. include ('./status/water_status.php'); ?> in the header of the wordpress theme

  11. #10
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,686
    Member #
    5580
    Liked
    716 times
    Using PHP within WordPress can be tricky. There are some options.

    1) You could try putting the script inside the "header.php" script, instead of using an include. header.php is located in your theme directory.

    2) The PHP script could use PHP GD to output a flag as an image. Then you would have this in your header.php file:
    <img src="http://www.mysite.com/safe.php" />
    In this case, the PHP has to output the object as a real image, which it can do with GD.

    3) You could make the PHP script output javascripting and use javascripting in your header.
    This means css has <div> sections containing your flags, and javascript merely makes them visible or not.

    4) You could make your own plugin for the flag script ... (very advanced, but a good option).

    5) There might be a way to use custom fields, or a dedicated post that is always displayed in the header.
    Or the staff changes a post remotely by email: Settings Writing Screen WordPress Codex


    Where you put the script, and where the .txt file resides would be important so you can properly indicate the path. This all comes down to a situation where you will have to know enough about WordPress, PHP, and javascripting to modify your theme files.

    Sorry that I can't really help anymore because I have no access to your site or files.
    It might be nice to actually see your site too.

    You'll have to figure it out or hire someone you trust that can access your website.
    Last edited by mlseim; Nov 16th, 2014 at 01:08 PM.



Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:04 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com