Diba ganto yung sinsabi nila na spaghetti code?

ito yung sa html form ko


HTML:
    <form action="index.php" method="post">
        <p>What foods do yoou like?</p>
        <input type="checkbox" name="kalamaris" value="Kalamaris">Kalamaris<br>
        <input type="checkbox" name="chapsoy" value="Chapsoy">Chapsoy<br>
        <input type="checkbox" name="adobo" value="Adobo">Adobo<br>
        <input type="checkbox" name="kare_kare" value="Kare Kare">Kare Kare<br>
        <input type="submit" name="foodssubmit" value="submit">
    </form>

ito naman sa php code ko hahahahha


PHP:
if (isset($_POST["foodssubmit"])) {
    $foods = array();
    $arraycounter = null;
    if (isset($_POST["kalamaris"])) {
        $food = $_POST["kalamaris"];
        array_push($foods, $food);
        $arraycounter += 1;
    }
    if (isset($_POST["chapsoy"])) {
        $food = $_POST["chapsoy"];
        array_push($foods, $food);
        $arraycounter += 1;
    }
    if (isset($_POST["adobo"])) {
        $food = $_POST["adobo"];
        array_push($foods, $food);
        $arraycounter += 1;
    }
    if (isset($_POST["kare_kare"])) {
        $food = $_POST["kare_kare"];
        array_push($foods, $food);
        $arraycounter += 1;
    }
    switch ($arraycounter) {
        case 1:
            echo "You like $foods[0].";
            break;
        case 2:
            echo "You like $foods[0], $foods[1].";
            break;
        case 3:
            echo "You like $foods[0], $foods[1], $foods[2].";
            break;
        case 4:
            echo "You like $foods[0], $foods[1], $foods[2], $foods[3].";
            break;
        default:
        echo "Please Choose in the Menu";
    }
}

Libre Lait sa Code hahaha nag si self study kasi me hehehhe
 
Haha, walang problema! Lahat tayo dumaan sa learning process. Tingnan natin yung code mo at subukan natin itong i-improve.

### HTML Form
Sa HTML form mo, okay naman siya, malinis at straightforward. Isang maliit na correction lang sa typo sa label ng unang checkbox:

HTML:
<p>What foods do you like?</p>

### PHP Code
Sa PHP code mo, maganda na nakikita mong gumagana ito, pero may ilang improvements na pwede nating gawin para mas maging efficient at mas madaling maintindihan.

1. Initialization ng $arraycounter:
Hindi mo actually kailangan ng $arraycounter. Pwede mong gamitin ang count($foods) para malaman ilan ang laman ng array.

2. Dynamic na pag-handle ng output:
Imbes na gumamit ng switch statement para i-handle ang iba't ibang cases, pwede kang gumamit ng loop o built-in functions sa PHP para gawin ito dynamically. Halimbawa, pwede mong gamitin ang implode() function para i-join ang mga elements ng array na may separator.

3. Pag-simplify ng pag-add sa array:
Dahil pare-pareho lang ang ginagawa mo sa bawat if statement (i.e., checking kung set ang isang variable at pag-push nito sa array), pwedeng i-simplify ito gamit ang loop.

Narito ang revised version ng iyong PHP code:

PHP:
if (isset($_POST["foodssubmit"])) {
    $foods = array();
    // List of possible food options based on the form input names
    $food_options = ["kalamaris", "chapsoy", "adobo", "kare_kare"];

    foreach ($food_options as $option) {
        if (isset($_POST[$option])) {
            array_push($foods, $_POST[$option]);
        }
    }

    if (count($foods) > 0) {
        echo "You like " . implode(', ', $foods) . ".";
    } else {
        echo "Please choose from the menu.";
    }
}

### Ano ang nagbago dito?
  • Dynamic Handling: Gumagamit na ngayon ng implode() para maging flexible sa bilang ng mga pagkaing pinili.
  • Code Reduction: Binawasan ang redundancy sa code sa pamamagitan ng paggamit ng loop.
  • Readability: Mas madaling maintindihan ang flow ng code dahil binawasan ang complexity.

Sana makatulong ang mga suggestions na ito! Patuloy lang sa pag-code at pag-explore ng iba't ibang paraan para mas mapaganda ang iyong code. Good luck!
 
e
Haha, walang problema! Lahat tayo dumaan sa learning process. Tingnan natin yung code mo at subukan natin itong i-improve.

### HTML Form
Sa HTML form mo, okay naman siya, malinis at straightforward. Isang maliit na correction lang sa typo sa label ng unang checkbox:

HTML:
<p>What foods do you like?</p>

### PHP Code
Sa PHP code mo, maganda na nakikita mong gumagana ito, pero may ilang improvements na pwede nating gawin para mas maging efficient at mas madaling maintindihan.

1. Initialization ng $arraycounter:
Hindi mo actually kailangan ng $arraycounter. Pwede mong gamitin ang count($foods) para malaman ilan ang laman ng array.

2. Dynamic na pag-handle ng output:
Imbes na gumamit ng switch statement para i-handle ang iba't ibang cases, pwede kang gumamit ng loop o built-in functions sa PHP para gawin ito dynamically. Halimbawa, pwede mong gamitin ang implode() function para i-join ang mga elements ng array na may separator.

3. Pag-simplify ng pag-add sa array:
Dahil pare-pareho lang ang ginagawa mo sa bawat if statement (i.e., checking kung set ang isang variable at pag-push nito sa array), pwedeng i-simplify ito gamit ang loop.

Narito ang revised version ng iyong PHP code:

PHP:
if (isset($_POST["foodssubmit"])) {
    $foods = array();
    // List of possible food options based on the form input names
    $food_options = ["kalamaris", "chapsoy", "adobo", "kare_kare"];

    foreach ($food_options as $option) {
        if (isset($_POST[$option])) {
            array_push($foods, $_POST[$option]);
        }
    }

    if (count($foods) > 0) {
        echo "You like " . implode(', ', $foods) . ".";
    } else {
        echo "Please choose from the menu.";
    }
}

### Ano ang nagbago dito?
  • Dynamic Handling: Gumagamit na ngayon ng implode() para maging flexible sa bilang ng mga pagkaing pinili.
  • Code Reduction: Binawasan ang redundancy sa code sa pamamagitan ng paggamit ng loop.
  • Readability: Mas madaling maintindihan ang flow ng code dahil binawasan ang complexity.

Sana makatulong ang mga suggestions na ito! Patuloy lang sa pag-code at pag-explore ng iba't ibang paraan para mas mapaganda ang iyong code. Good luck!
eplain to me this part
$_POST[$option]

bun in html walang superglobal variable dun na option na part
 
eplain to me this part
$_POST[$option]

bun in html walang superglobal variable dun na option na part
Sure, I'd be happy to explain that part!

The $_POST[$option] in the PHP code is a way to dynamically access the values sent from the form through the POST method. Let's break it down:

### Understanding $_POST

$_POST is a superglobal array in PHP. This means it is automatically available in all scopes throughout a script, and you do not need to do anything special to access it. This array collects data sent through an HTML form using the method post.

### How $_POST[$option] Works

When you use $_POST[$option], what you're doing is accessing the elements of the $_POST array by their keys. The keys in this array are the names of the form inputs.

### Your HTML Form Example

In your HTML form, you have several checkboxes each with a unique name:

HTML:
<input type="checkbox" name="kalamaris" value="Kalamaris">Kalamaris<br>
<input type="checkbox" name="chapsoy" value="Chapsoy">Chapsoy<br>
<input type="checkbox" name="adobo" value="Adobo">Adobo<br>
<input type="checkbox" name="kare_kare" value="Kare Kare">Kare Kare<br>

Each name attribute of the input fields (kalamaris, chapsoy, adobo, kare_kare) acts as the key in the $_POST array if the form is submitted with those checkboxes selected.

### The PHP Code

In your PHP script, you created an array $food_options that contains the names of these inputs:

PHP:
$food_options = ["kalamaris", "chapsoy", "adobo", "kare_kare"];

When the form is submitted, the script loops through each item in $food_options:

PHP:
foreach ($food_options as $option) {
    if (isset($_POST[$option])) {
        array_push($foods, $_POST[$option]);
    }
}

Here’s what happens in this loop:

1. Iteration: The loop iterates over each element of the $food_options array. Each element ($option) is a string that corresponds to one of the input names in the HTML form.

2. Check and Retrieve: isset($_POST[$option]) checks if there is a posted value for the input field whose name matches the current $option. If true, it means the checkbox was checked and a value was sent through the form.

3. Accessing Value: $_POST[$option] accesses the value of the checkbox that was checked. This is the value specified in the value attribute of the input element in the HTML.

### Summary

So, $_POST[$option] is dynamically accessing the data sent from the checkboxes. The $option variable is substituted with each name of the input fields ("kalamaris", "chapsoy", "adobo", "kare_kare"), allowing the script to check and retrieve the value of each checked checkbox in a flexible and efficient manner. This avoids hard-coding each form input into your PHP, making your code cleaner and easier to manage, especially as forms grow or change.
 
for me wala naman mali sa spaghetti style hahaha, as long as beginner or nasa basic learning ka po. as long as may improvement ka. At least nadaanan mo yan and soon if you have friend or workmate of course you have this idea na kung pano rin sila matutulungan. inshort yong mali mo ngayon, future mo ang mag bbenefit 😄
 
for me wala naman mali sa spaghetti style hahaha, as long as beginner or nasa basic learning ka po. as long as may improvement ka. At least nadaanan mo yan and soon if you have friend or workmate of course you have this idea na kung pano rin sila matutulungan. inshort yong mali mo ngayon, future mo ang mag bbenefit 😄
true! tsaka pwede mo naman balikan at irefactor pag medyo malupet kana. minsan nga maasiwa ka nalang sa code mo mapapaisip ka "hala ano ba tung pinaggagawa ko hayss 🤦
"
 
for me wala naman mali sa spaghetti style hahaha, as long as beginner or nasa basic learning ka po. as long as may improvement ka. At least nadaanan mo yan and soon if you have friend or workmate of course you have this idea na kung pano rin sila matutulungan. inshort yong mali mo ngayon, future mo ang mag bbenefit 😄
Siyang tunay... (y)
Kahit pancit style pa iyan.... :pepeHA:
Nakakagutom talaga ang malinis na coding... :pepeHA:
Struggle sa umpisa.... Kasama sa learning curve iyan....

true! tsaka pwede mo naman balikan at irefactor pag medyo malupet kana. minsan nga maasiwa ka nalang sa code mo mapapaisip ka "hala ano ba tung pinaggagawa ko hayss 🤦
"
Siyang tunay...(y)
Ang next ay OOP kapag nakalagpas na sa noodle style... :pepeHA:

Padayon lang... (y)
Libre ang magtanong kay google...
 

About this Thread

  • 6
    Replies
  • 430
    Views
  • 4
    Participants
Last reply from:
Well1AmNichtDiePerson

Trending Topics

Online now

Members online
1,153
Guests online
877
Total visitors
2,030

Forum statistics

Threads
2,273,748
Posts
28,951,295
Members
1,234,942
Latest member
nuzlock
Back
Top