0

I have a form on my website for users to submit blog

Within that form, I have a drop-down select menu for different blog categories.

E.g.U.S. News, World News, Economic News, etc......'

Each form option allows for a single value input.

In my php, I would like to create two separate variables for that 'category' form field and use each value for two different tasks.

TASK #1:

I would like to use the selected value as part of the URL path to post/store the particular blog files.

For this task, I have been able to use the values as they are embedded in the form.

And... in my php, I created the variable $catPath with a category array to handle this task.

For example:

*If the user selects "U.S. News" as the category their blog should appear in, the value for U.S. News is set to 'US'.

So, in the example above... the 'US' value would be used as such;

file_put_contents("../Blogs/$catPath/$unique_id.html", ob_get_contents());

Ex: ../Blogs/US/0001.html

So for, so good.....

However...

TASK #2:

I would like to create a separate variable named $catTag to be used within the blog header itself.

For Example:

Sticking with the example above... if the user selects 'U.S. News' in the form... I would like the full category text "U.S. News" to appear in the header of the blog, not as 'US' as it is in the url path.

DEMO:

I included a very stripped down demo version of the form and the php below.

In the php, please notice I have created the variable $catTag, and I am successfully able to echo the adjusted value in php echo example 'A'.

However, when I attempt to use the variable $catTag as a simple php echo, as in php example 'B'... it still echos the original value, not the adjusted value.

I don't want to have to create two redundant fields in my form for 'category'.

I need to be able to use a separate variable so it echos the adjusted value.

I apologize in advance for my limited knowledge with this. I hope I have explained this well enough to be understood.

I have tried several things, but so far, I have had no luck.

Any advice would be greatly appreciated.

THE FORM:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="MyPHP.php" method="post" enctype="multipart/form-data">

<label>Select A Category: </label>
<select name="category">
<option value="US">U.S. News</option>
<option value="World">World News</option>
<option value="Government-Politics">Government & Politics</option>
<option value="Economic">Economic News</option>
<option value="National-Weather">National Weather News</option>
<option value="Entertainment">Entertainment News</option>
<option value="Science-Technology">Science & Technology</option>
<option value="Sports">Sports News</option>
<option value="Health">Health News</option>
</select>
<br>
<br>
<input name="Submit" type="submit" value="Submit">

</form>
</body>
</html>

THE PHP: (MyPHP.php)

<?php ob_start(); ?>

<html>
<head>
</head>
<body>


<!---| Start php Example A |---------------------------->
<?php 

$catTag = $_REQUEST['category']; 

if  ($catTag == "US") { echo "U.S. News"; }
if  ($catTag == "World") { echo "World News"; }
if  ($catTag == "Government-Politics") { echo "Government & Politics"; }
if  ($catTag == "Economic") { echo "Economic News"; }
if  ($catTag == "National-Weather") { echo "National Weather News"; }
if  ($catTag == "Entertainment") { echo "Entertainment News"; }
if  ($catTag == "Science-Technology") { echo "Science & Technology"; }
if  ($catTag == "Sports") { echo "Sports News"; }
if  ($catTag == "Health") { echo "Health News"; }

?><br>
<!---| End php Example A |---------------------------->


<!---| Start php Example B |------------------------------>
<?php echo $catTag; ?>
<!---| End php Example B |-------------------------------->

</body>
</html>

<?php echo ''; 
$catPath = $_REQUEST['category']; 
$valid_categories = array('US','World','Government-Politics','Economic','National-Weather','Entertainment','Science-Technology','Sports','Health');
if (!in_array($catPath, $valid_categories)) { die("Invalid Category"); } file_put_contents("$catPath.html", ob_get_contents()); ?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • I don't like the idea of a battery of if conditions. Rather, I recommend a smart array with specific keys and values for both of your purposes. – mickmackusa Aug 03 '17 at 07:20
  • I would prefer that way too. But that seemed like an even more challenging way for me. :/ –  Aug 03 '17 at 07:24
  • Thank you mickmackusa! Meanwhile... Arun's method did work. But I will wait to see if your method is cleaner instead of all of the 'if' conditions. –  Aug 03 '17 at 07:27

2 Answers2

0

Im not much for writing code for others on SO, but here is a little more clean implementation of what you are trying to achieve. I've commented the code so that it hopefully makes sense to you.

This has a Key/value array containing the category slugs and their titles. We then perform a check if the category exists before executing the code. Thus you can use the value of the key in the array as the title for the chosen category.

$chosen_category = filter_input(INPUT_POST, 'category'); // Filter the input

if( !$chosen_category ) die('No category chosen');

// Array holding the category keys and titles
$category_array = [
    'US' => 'U.S. News',
    'World' => 'World News',
    'Government-Politics' => 'Government & Politics'
];

if( !array_key_exists($chosen_category, $category_array) ) die('Chosen category does not exist'); // Confirm that the category exists in as key value in $category_array

$category_title = $category_array[$chosen_category]; // Title of the chosen category, as mapped in the KV Array

file_put_contents("{$chosen_category}.html", ob_get_contents());
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
  • Thank you Ole, I appreciate that. But I already had that part worked out. Needed a second variable for the same value. –  Aug 03 '17 at 09:07
0

I think the cleanest way forward is to structure your array to serve both of your purposes without being too bloated. I suggest specific keys and values to leveraged in the following ways:

$array=[
    'US'=>'U.S. News',
    'World'=>'World News',
    'Government-Politics'=>'Government & Politics',
    'Economic'=>'Economic News',
    'National-Weather'=>'National Weather News',
    'Entertainment'=>'Entertainment News',
    'Science-Technology'=>'Science & Technology',
    'Sports'=>'Sports News',
    'Health'=>'Health News'
];

Form generation:

echo "<select name=\"category\">";
foreach($array as $k=>$v){
    echo "<option value=\"$k\">$v</option>";
}
echo "</select>";

Submission validation:

if(isset($_POST['category'])){  // something was sent
    if(isset($array[$_POST['category']])){
        $catPath=$_POST['category'];
        $catTag=$array[$_POST['category']];
    }else{
        // set some kind of default values or deny submission
    }
}

Of course, you will have to determine the placement of these methods in your project.

The overarching message is that reusing array data delivers DRY code and makes the "programming Gods" smile.


I'll also add:

  • if you are going to use the POST method at your form, use $_POST to receive it.
  • you should check if the $_POST['category'] isset() before trying to work with it to avoid warnings.
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • Thank you so much mickmackusa! I will have to give this a try in the morning. I am exhausted. I'll pop back in once I've tried your method. –  Aug 03 '17 at 09:16