PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Nov 17, 2019 6:47 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Code behaviour
PostPosted: Thu Feb 15, 2018 12:13 am 
Offline
Forum Newbie

Joined: Thu Feb 15, 2018 12:00 am
Posts: 1
Hi guys i am learning php by following the book "Learning PHP". I building a simple form for an excercise in the book and need some clarification on this line of code $input['dish_name'] = $dishes[$_POST['dish_name']], why do i need $dishes in there?? Why cant i simply go $input['dish_name'] = $_POST['dish_name'] When i tried this line i wouldnt get any output just a blank page. Any help would be appreciated. Thanks
p.s I am using php 7.3 and here is the code of the form

Syntax: [ Download ] [ Hide ]
<?php
require 'formHelper.php';
try{
    $db = new PDO('sqlite:c:\xampp\htdocs\tmp\restaurant2.db');
}
catch(PDOException $e){
    print "Couldnt connect to the database, " . $e->getMessage();
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$dishes = array();
$query = $db->query("SELECT dish_name FROM dishes");
foreach($query as $dish){
    $dishes[] = $dish['dish_name'];
   
}
function validate_form(){
    global $dishes;
    $errors = array();
    $input = array();
    if(isset($_POST['dish_name'])){
       $input['dish_name'] = $dishes[$_POST['dish_name']];
    }
   
    else{
        $errors[] = "Please select a dish from the menu.";
    }
    return array($errors,$input);
}


Top
 Profile  
 
 Post subject: Re: Code behaviour
PostPosted: Thu Feb 15, 2018 2:26 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367

_________________
Warning: I have no idea what I'm talking about.


Last edited by thinsoldier on Thu Feb 15, 2018 9:05 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Code behaviour
PostPosted: Thu Feb 15, 2018 8:18 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Google [Bot] and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group