Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy PHP Image Changing

    I am building a website for a friend of mine and have run into a little trouble!

    The site is being made for a youth group. There is a box on the homepage which displays whether or not the group is open on the coming Wednesday, using an image - either "images/statusopen.gif" or "images/statusclosed.gif".

    Now the tough part... I need some coding which will enable the user to change this image on the homepage from within the login panel (PHP/MySQL).

    I would like this to be done with radio buttons for "open" and "closed".
    How would i get this to work?

    Can someone please provide some coding which will enable me to make this form (which must be submitted to the database) and the code to pull this data from the database and display the images for each status on the homepage.

    Would it be easier to have the form interpret the radio button data and submit the image link (images/statusopen.gif) to the database then have it retrieved or simply submit "open" or "closed" and have the fetching script interpret it into the image links?

    Thanks in advance, this has bugged me for a few days!
    Last edited by Mattyblob; 06-23-2010 at 02:48 PM.

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by Mattyblob View Post
    I am building a website for a friend of mine and have run into a little trouble!

    The site is being made for a youth group. There is a box on the homepage which displays whether or not the group is open on the coming Wednesday, using an image - either "images/statusopen.gif" or "images/statusclosed.gif".

    Now the tough part... I need some coding which will enable the user to change this image on the homepage from within the login panel (PHP/MySQL).

    I would like this to be done with radio buttons for "open" and "closed".
    How would i get this to work?

    Can someone please provide some coding which will enable me to make this form (which must be submitted to the database) and the code to pull this data from the database and display the images for each status on the homepage.

    Would it be easier to have the form interpret the radio button data and submit the image link (images/statusopen.gif) to the database then have it retrieved or simply submit "open" or "closed" and have the fetching script interpret it into the image links?

    Thanks in advance, this has bugged me for a few days!
    Welcome to the Forum! Either method (storing image location or storing status marker) will work so that part is just a matter of your personal preference.

    The form to make the change in the control panel is not anything special. Just create an HTML form with the appropriate inputs and have it submit to a page that captures the data and updates the database. You can do this a few different ways as well, and you can even do this with AJAX (a javascript call to another page that updates the status without the user having to actually navigate away from the control panel that they are in). You can post the data to the same page or to a processing page that handles the request and then redirects the user back to the control panel. The possibilities are numerous.

    Pulling up the status from the database afterward is simple stuff so you should tackle the control panel/database storage factor first. And before you can do that, you need to know exactly how you want the page to work.

    So, how exactly do you want to do this?
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • Users who have thanked Rowsdower! for this post:

    Mattyblob (06-23-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hey and thank you for the warm welcome,

    I'm wondering if there's a simpler way of doing this without using PHP at all?

    My knowledge of PHP+MySQL is fairly limited and tend to adapt my material from examples my friends have sent me over the years.

    I've just found this script which my friend developed for an online DJ panel:

    PHP Code:
    <?php
    $file 
    "djmsg_message.php";
    if (!isset(
    $_POST['submit']))
    {
      
    $fo fopen($file"r");
      
    $fr fread($fofilesize($file));
      if ( 
    get_magic_quotes_gpc () ) $fr stripslashes($fr);
      
      
    $fr str_replace("&""&amp;"$fr);
      
    $fr str_replace("<""&lt;"$fr);
      
    $fr str_replace(">""&gt;"$fr);
      
      echo 
    "<form method='post' action='{$_SERVER['PHP_SELF']}'>
            <textarea name='newfile' rows='6' cols='40'>{$fr}</textarea>
            <p>
            <input type='submit' name='submit' value='Save' />
            </form>"
    ;
      
    fclose($fo);
    }
    else
    {
      
    $fo fopen($file"w");
      
    $fw fwrite($fo, (get_magic_quotes_gpc()?stripslashes($_POST['newfile']):$_POST['newfile']));
      
    fclose($fo);
    echo 
    "<p><h1>The DJ Message has been updated!</h1>";
    }
    ?>
    I'm only guessing but it seems to write to the file djmsg_message.php without connecting to the database at all (i may be COMPLETELY wrong)... but could this be adapted to work with what i'm trying to do?

    If this is not viable i'd like to have the form submit to the same page for efficiency. I think it'll also be easier to submit the status and have it interpreted into the image links in the data fetching script. How would i go about creating this?

    Thanks
    Last edited by Mattyblob; 06-23-2010 at 01:30 PM.

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Quote Originally Posted by Mattyblob View Post
    Hey and thank you for the warm welcome,

    I'm wondering if there's a simpler way of doing this without using PHP at all?

    My knowledge of PHP+MySQL is fairly limited and tend to adapt my material from examples my friends have sent me over the years.

    I've just found this script which my friend developed for an online DJ panel:

    PHP Code:
    <?php
    $file 
    "djmsg_message.php";
    if (!isset(
    $_POST['submit']))
    {
      
    $fo fopen($file"r");
      
    $fr fread($fofilesize($file));
      if ( 
    get_magic_quotes_gpc () ) $fr stripslashes($fr);
      
      
    $fr str_replace("&""&amp;"$fr);
      
    $fr str_replace("<""&lt;"$fr);
      
    $fr str_replace(">""&gt;"$fr);
      
      echo 
    "<form method='post' action='{$_SERVER['PHP_SELF']}'>
            <textarea name='newfile' rows='6' cols='40'>{$fr}</textarea>
            <p>
            <input type='submit' name='submit' value='Save' />
            </form>"
    ;
      
    fclose($fo);
    }
    else
    {
      
    $fo fopen($file"w");
      
    $fw fwrite($fo, (get_magic_quotes_gpc()?stripslashes($_POST['newfile']):$_POST['newfile']));
      
    fclose($fo);
    echo 
    "<p><h1>The DJ Message has been updated!</h1>";
    }
    ?>
    I'm only guessing but it seems to write to the file djmsg_message.php without connecting to the database at all (i may be COMPLETELY wrong)... but could this be adapted to work with what i'm trying to do?

    If this is not viable i'd like to have the form submit to the same page for efficiency. I think it'll also be easier to submit the status and have it interpreted into the image links in the data fetching script. How would i go about creating this?

    Thanks
    The script you posted could do this task, yes. What it essentially does is overwrite a file on the server that stores the "DJ message" in this case. Then the other pages in the website presumably use an include() or fopen() on that file to display the message.

    If you wanted to go that route, you certainly could.

    This is untested, but it would look something like this:
    PHP Code:
    <?php
    $file 
    "djmsg_message.php";
    if (!isset(
    $_POST['submit'])){
        
    $fo fopen($file"r");
        
    $fr fread($fofilesize($file));

        
    $current_status=str_replace(".gif","",str_replace("images/status","",$fr));

        echo 
    "<form method='post' action='{$_SERVER['PHP_SELF']}'><div>
            Open: <input type='radio' name='newfile' value='images/statusopen.gif'"
    ;
        if(
    $current_status=="open"){
            echo 
    " checked='checked'";
        }
        echo 
    " /><br />
            Closed: <input type='radio' name='newfile' value='images/statusclosed.gif'"
    ;
        if(
    $current_status=="closed"){
            echo 
    " checked='checked'";
        }
        echo 
    " />
            <p>
            <input type='submit' name='submit' value='Save' />
            </p>
            </div></form>"
    ;
        
    fclose($fo);
    }
    else{
        
    $fo fopen($file"w");
        
    $fw fwrite($fo$_POST['newfile']);
        
    fclose($fo);
        echo 
    "<p><h1>The DJ Message has been updated!</h1>";
    }
    ?>
    You would just need to create a file on your server to hold the image location we are storing and then make sure that $file is changed to that file's name.
    Last edited by Rowsdower!; 06-23-2010 at 02:02 PM. Reason: Forgot input type parameter and changing to "checked" rather than "selected"... Sloppy coding today!
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,472
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Matty,
    How about a simple text box on the main page where
    you enter a code word for "open" and a code word for "closed".
    No login or password needed, the person(s) just needs to know which
    code word to enter in the box.

    Example:

    <form action="status.php" method="post">
    <input type="textbox" name="status"><input type="submit" name="submit" value="go">
    </form>

    The PHP script called "status.php":
    PHP Code:
    <?php
    // Get form variable.
    $status=$_POST['status'];

    // Determine if codeword is entered.
    $f1=0;
    if(
    $status=="o123"){
    $f1=1;
    }
    if(
    $status=="c123"){
    $f1=2;
    }
    if(
    $f1>0){
    // write code to file.
    $myFile="status.txt";
    $fh fopen($myFile'w+') or die("can't open file");
    fwrite($fh$f1);
    fclose($fh);
    }
    // redirect back to main page
    header ("location: index.php");
    ?>

    Then, on the page you want to display the open/close ...
    Put this above the HTML stuff ...

    PHP Code:
    <?php
    // read the code in the status.txt file
    $line file("status.txt");
    $status=$line[0];

    $img="images/statusclosed.gif";
    if(
    $status=="1"){
    $img="images/statusopen.gif";
    }
    ?>

    <html>
    <body>
    blah blah blah

    <img src="<?=$img?>" />

    blah blah blah
    </body>
    </html>

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    First off i'd like to say thank you for the time and effort you've put into solving this problem with me!

    I've just implemented the script onto a test server and everything seems to be okay apart from the form itself - It appears as

    Open: [Text box which says: "images/statusopen.png"]
    Closed: [Tex box which says: "images/statusclosed.png]

    There is no way of selecting which one is to be submitted. Any ideas?

    Thanks again


    EDIT: Thanks for updating the code, it works perfectly! You have been such a great help - M. Amos.
    Last edited by Mattyblob; 06-23-2010 at 02:46 PM.

  • #7
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry for the double post, in reply to mlseim:

    Thank you for the suggestion but the login system is already implemented and contains other features i have built for use by the website user (uploads, a simple news system, etc).

    Nevertheless that is a great way of doing something like this - i may try and use it in future builds!


  •  

    Tags for this Thread

    Posting Permissions

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