Jump to content

echo out something when $selected_dir matches the same name (first part) of a html file


IamTomCat

Recommended Posts

Hi 

 

I want to echo out something when I post the value of a radio button and this value matches the same name of a html file. How can I formulate my code to achieve that?

 

 

What I have so far:

 <?php  
 
   if (isset($_POST['submitradio']))
{ 
    $selected_dir = $_POST['radio1'];

  echo substr($selected_dir, 0, -4);
  echo '<img src="'.$selected_dir.'" />';
  
} 
?>

So far I get for example the name of the image(order.gif) and the image order. gif.

 

What I now want is to formulate my code so that I can say if I have an image called "order.gif" and a file called "order.html" that I can then echo out some thing. I don't want to actually name the image or the html file. I just want to say if I have two different file types that start with the same name then I can echo out what ever. How can I do that?

Link to comment
Share on other sites

Not sure what you are asking but maybe... use file_exists to see if a html file shares the same name as the image.

if (isset($_POST['submitradio']))
{ 
    $selected_dir = $_POST['radio1'];
    echo '<img src="'.$selected_dir.'" />';

    // get the filename of the file
    $filename = pathinfo($selected_dir, PATHINFO_FILENAME);

    // check to see if a html file named the same also exists
    if(file_exists("$filename.html"))
    {
        echo "$filename.html shares the same name as $selected_dir";
    }
    else
    {
        echo "Could not find a html file that shares the same $filename";
    }
} 
Edited by Ch0cu3r
Link to comment
Share on other sites

HI many thanks for your reply. I try to explain it again. I want that when I submit the radio button($selected_dir = $_POST['radio1']), ( which would be for example order.gif) and when there is a file in the same folder called "order.html" then I could for example echo out "boa boa".  If I submit let's say a radio button value (let's say "smoke.gif") and there is no html file called "smoke.html" then I could echo out "No boa boa available". Don't know how else to explain it. It's just really important that I don't give actually the name of each file. I want that the programme looks what is there.

 

Example of what I got with the code you gave me: Could not find a html file that shares the same order.

 

But there is a file called order.gif and a file called order.html in the same folder.

 

 

Help would be very much appreciated.

Link to comment
Share on other sites

Thats is what my code does. All you need to do is change the echo statements to what you want to output when a filename match is/not found.

 

For my code to work correctly the .gif and .html files need to be in the same directory as where this code is being ran. If the files are in a different directory then you need to tell us.

Link to comment
Share on other sites

Hi. Thanks for your reply and your patience. Well the files are in the same folder. And I don't get anything for that part:

echo "$filename.html shares the same name as $selected_dir";

Have you tried it yourself? And when yes, did it work for you?

 

 

Well I just wonder, if I may have made a mistake or if there could be maybe an other solution for the problem. I tried now for hours without making any progress.

Link to comment
Share on other sites

Yes. I have tested my code and it functions correctly. This is the form I am using to test with and I only have an order.html file in the same directory as the php code

<form method="post">
    <input type="radio" name="radio1" value="order.gif" /> Order<br />
    <input type="radio" name="radio1" value="dummy.gif" /> Dummy<br />
    <input type="submit" name="submitradio" />
</form>

Selecting the Order radio button I get "order.html shares the same name as order.gif"

Selecting Dummy radio button I get  "Could not find a html file that shares the same dummy".

 

Maybe the data in your form is different. What is the output of the following

printf('<pre>%s</pre>', print_r($_POST, 1));
Link to comment
Share on other sites

 

Animation is the folder name

That will be why my code is not working as expected. You did not mention that earlier 

 

As I said earlier all files (including the php file) need to all in the same directory in order for it work correctly. Code updated

if (isset($_POST['submitradio']))
{ 
    $selected_file = $_POST['radio1'];

    // get the filename of the file
    $fileinfo = pathinfo($selected_file);
    $filename = $fileinfo['dirname'] . DIRECTORY_SEPARATOR . $fileinfo['filename'];

    // check to see if a html file named the same also exists
    if(file_exists("$filename.html"))
    {
        echo "$filename.html shares the same name as $selected_file";
    }
    else
    {
        echo "Could not find a html file that shares the same name as $filename.html";
    }
}

Should work as expected this time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.