Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP Help

Options
  • 23-02-2013 12:03pm
    #1
    Closed Accounts Posts: 3,596 ✭✭✭


    Hi

    I am wondering can someone point me in the direction of some decent material on using PHP. We have a project due in less than 3 weeks, the project is to make a hotel reservation system, along with managing stock levels etc. This subject is new on our course and our lecturer hasnt a clue how to teach the subject , (she gave us 2 sets of slides copied off w3schools.com which covered the very very basics). Im spending hours searching online trying to find how to do whats being asked and its time I would much rather spend on other areas of our course.


«1

Comments

  • Registered Users Posts: 9,061 ✭✭✭Kenny Logins




  • Closed Accounts Posts: 3,596 ✭✭✭threein99



    It was giving to us the week before Christmas, but we didnt actually start PHP until after Christmas, the whole thing has been a disaster, we have a server to upload the php files to and they only worked out how to turn the error messages on it the other day. There has been quite a few complaints to the head of the department over it believe me.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Can anyone help me populate a textboxes based on a selection from a dropdown menu ? The dropdown is populated with supplier names from my database, depending on which supplier I click, the relavent supplier ID goes into a text box underneath


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    threein99 wrote: »
    Can anyone help me populate a textboxes based on a selection from a dropdown menu ? The dropdown is populated with supplier names from my database, depending on which supplier I click, the relavent supplier ID goes into a text box underneath

    I presume your using a SQL link and know something about interfacing with it. The basic outline:
    //Start SQL
    $link = mysqli_connect($host, $uid, $pwd, $db);
    
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    //End SQL Set Up
    
    $query = "SELECT supplierName FROM suppliers LIMIT 100";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    
    echo "<form name=myform>";
    echo "<select name=suppliertable>";
    $counter = 0;
    foreach($rows as $row)
    {
    echo <option name=name value=".$row[$counter].">".$row[$counter]." </option>;
    }
    echo "</select></form>";
    

    I'm assuming your using HTML. Thats probably littered with mistakes but its the general outline. You basically want to get an array and loop it out into the form. At least thats how I'd do it.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    ironclaw wrote: »
    I presume your using a SQL link and know something about interfacing with it. The basic outline:
    //Start SQL
    $link = mysqli_connect($host, $uid, $pwd, $db);
    
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    //End SQL Set Up
    
    $query = "SELECT supplierName FROM suppliers LIMIT 100";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    
    echo "<form name=myform>";
    echo "<select name=suppliertable>";
    $counter = 0;
    foreach($rows as $row)
    {
    echo <option name=name value=".$row[$counter].">".$row[$counter]." </option>;
    }
    echo "</select></form>";
    

    I'm assuming your using HTML. Thats probably littered with mistakes but its the general outline. You basically want to get an array and loop it out into the form. At least thats how I'd do it.


    Yeah im using HTML. Sorry for asking this, but which part of that will populate the text box ?


  • Advertisement
  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    threein99 wrote: »
    Yeah im using HTML. Sorry for asking this, but which part of that will populate the text box ?

    In all honesty, you should understand nearly every single line of that. Its very basic html.

    But this bit will build a form with a drop down:
    echo "<form name=myform>";
    echo "<select name=suppliertable>";
    $counter = 0;
    foreach($rows as $row)
    {
    echo <option name=name value=".$row[$counter].">".$row[$counter]." </option>;
    }
    echo "</select></form>";
    


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    ironclaw wrote: »
    In all honesty, you should understand nearly every single line of that. Its very basic html.

    But this bit will build a form with a drop down:
    echo "<form name=myform>";
    echo "<select name=suppliertable>";
    $counter = 0;
    foreach($rows as $row)
    {
    echo <option name=name value=".$row[$counter].">".$row[$counter]." </option>;
    }
    echo "</select></form>";
    

    Since September we have been shown, javascript, php, and mysql, all thats been achieved is to confuse the entire year


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    threein99 wrote: »
    Since September we have been shown, javascript, php, and mysql, all thats been achieved is to confuse the entire year

    Thats quite alot though :confused: My best suggestion is w3schools. Get a basic HTML template going and then build PHP into it. PHP just adds dynamic functionality so unless you know HTML your not going to be able to do much with PHP.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    ironclaw wrote: »
    Thats quite alot though :confused: My best suggestion is w3schools. Get a basic HTML template going and then build PHP into it. PHP just adds dynamic functionality so unless you know HTML your not going to be able to do much with PHP.

    And you can throw java into that as well ;) We are allegedly studying software development , the web development stuff is interesting but they are trying to cram so much in that we are learning very little. Thanks for your help


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    I am wondering could anyone help me, I am trying to populate a dropdown list with data depending on the selection from another dropdown list. I have one dropdown drop populated with different hotel areas, Bar, Leisure centre etc. When one of them is selected I want a second dropdown to populate with all the stock items associated with that area.

    I have a table with the areas in the hotel,the first dropdown is populated from this table I also have a stockItem table, this contains all the stock details, stockID etc right down to area the item is belonged too.

    Thanks in advance


  • Advertisement
  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    What you're looking for is AJAX. Best way to do this is to think logically and sequentially about what is happening.
    • An option box is displayed with a list of options (PHP/HTML)
    • When the option is changed (Javascript change event handler)
    • Populate another box with options based on this (PHP/HTML/Javascript)

    There's a very good example here https://forums.digitalpoint.com/threads/onchange-populate-select-boxes-newbie-question.147516/ (2nd response). All you need to do is determine where the options come from, for the second box.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    What you're looking for is AJAX. Best way to do this is to think logically and sequentially about what is happening.
    • An option box is displayed with a list of options (PHP/HTML)
    • When the option is changed (Javascript change event handler)
    • Populate another box with options based on this (PHP/HTML/Javascript)

    There's a very good example here https://forums.digitalpoint.com/threads/onchange-populate-select-boxes-newbie-question.147516/ (2nd response). All you need to do is determine where the options come from, for the second box.


    Thanks for replying, I dont know anything about AJAX we only started php in January. I will have a look at that though and try work it out


  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    AJAX is normally used when querying a server (in your case, querying the database), waiting on the response, and doing something with that response.

    Alternatively, you can create a javascript array on your page, inline, that does the same thing, so you dont have to query. In your case your array might look like
    var boxOptions = new Array();
    
    boxOptions['Bar'][] = 'Drinks';
    boxOptions['Bar'][] = 'Glasses';
    boxOptions['Bar'][] = 'Peanuts';
    boxOptions['Bar'][] = 'Crisps';
    ...
    
    boxOptions['Leisure Centre'][] = 'Weights';
    boxOptions['Leisure Centre'][] = 'Energy Bars';
    ...
    
    

    Then use Javascript to go through the array, and get all the keys, and use these to populate your first select box.

    When the select box changes, go back to your array, and grab all the items from boxOptions, and use these to populate your second listbox.

    Remember to clear the options in the second list box every time the first list box changes.

    That should do it. You might have to do some messing about with the array if you want the option VALUE to be something other than "Crisps", for example, 10. In this case, you might need to use objects, for example:
    var options = new Array();
    
    boxOptions['Bar'][] = { value: 10, text: 'Drinks'};
    
    


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    AJAX is normally used when querying a server (in your case, querying the database), waiting on the response, and doing something with that response.

    Alternatively, you can create a javascript array on your page, inline, that does the same thing, so you dont have to query. In your case your array might look like
    var boxOptions = new Array();
    
    boxOptions['Bar'][] = 'Drinks';
    boxOptions['Bar'][] = 'Glasses';
    boxOptions['Bar'][] = 'Peanuts';
    boxOptions['Bar'][] = 'Crisps';
    ...
    
    boxOptions['Leisure Centre'][] = 'Weights';
    boxOptions['Leisure Centre'][] = 'Energy Bars';
    ...
    
    

    Then use Javascript to go through the array, and get all the keys, and use these to populate your first select box.

    When the select box changes, go back to your array, and grab all the items from boxOptions, and use these to populate your second listbox.

    Remember to clear the options in the second list box every time the first list box changes.

    That should do it. You might have to do some messing about with the array if you want the option VALUE to be something other than "Crisps", for example, 10. In this case, you might need to use objects, for example:
    var options = new Array();
    
    boxOptions['Bar'][] = { value: 10, text: 'Drinks'};
    
    

    My stock items are comin form a stock table, this table includes the area they belong too, so when I select "Bar" from my drop down I want the second drop down to populate with the names of all the stocks items belonging to the bar area . I havent hard coded in any stock items.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    3 weeks? Cutting that short.

    TBH it's normal for the class to be confused, the only way you start to make sense of this is to cut code in anger. Simply understanding the syntax is not even half the battle, you have to learn how to put these things together. Start with a single task and break it down into smaller and smaller tasks until you can't break it down any more. Then develop each of these tasks one-by-one, don't worry about the other tasks until you have your current one complete. You don't have to have an fully working system to pass the assignment.

    For future reference have a read of this, you'll find it helpful.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Evil Phil wrote: »
    3 weeks? Cutting that short.

    TBH it's normal for the class to be confused, the only way you start to make sense of this is to cut code in anger. Simply understanding the syntax is not even half the battle, you have to learn how to put these things together. Start with a single task and break it down into smaller and smaller tasks until you can't break it down any more. Then develop each of these tasks one-by-one, don't worry about the other tasks until you have your current one complete. You don't have to have an fully working system to pass the assignment.

    For future reference have a read of this, you'll find it helpful.

    We were given the brief before xmas but they have only shown us in the last few weeks the stuff we need. There has been many complaints out in against how we are being thought, it's head wrecking

    I have been having a bit of joy with the baby steps approach, I still have loads to do, and basically have to search the web for help, as our lecturer is only learning the language herself as she goes along.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    <form action="test.php" method="post" onsubmit= "confirm()" >
    



    I am trying to refresh back to a form page when the submit button click is confirmed in my confirm function. The data submits to my database but keeps directing the browser to test.php

    Here is my confirm function
    <script>
    function confirm()
    {
    if(confirm("add stock ?")
    {
    document.location = 'http://http://backToMyform.com/'; " 
    }
    
    
     
    }
    </script>
    

    Any tips as to what Im doing wrong ?


  • Closed Accounts Posts: 249 ✭✭OneIdea


    threein99 wrote: »
    <form action="test.php" method="post" onsubmit= "confirm()" >
    

    I am trying to refresh back to a form page when the submit button click is confirmed in my confirm function. The data submits to my database but keeps directing the browser to test.php

    Here is my confirm function
    <script>
    function confirm()
    {
    if(confirm("add stock ?")
    {
    document.location = 'http://http://backToMyform.com/'; " 
    }
    
    
     
    }
    </script>
    
    Any tips as to what Im doing wrong ?
    <script type="text/javascript">
    function proceed(){
    window.location = "http://backToMyform.com";}
    </script>
    </head>
    <body>
    
    <form action="test.php" method="post" onClick= "javascript:proceed()" >
    

    Ideally though you would be better of just putting
    header("Location: " . http://backToMyform.com);
    
    at the end of test.php


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Can someone help me with another problem, I am trying to remove stock from a stock table, but if the current level of stock is zero, or is the amount required is less than the quantity in stock I want a message to display this to the screen.

    Heres how I have been trying to do it, it ignores the if statement regardless whether there is enough stock or not


    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css" />
    <title>Remove Stock</title>
    </head>
    <body>
    </body>
    </html>
    
    
    <?php
    include "connect.php";
    date_default_timezone_set('Europe/London');
    
    
    if('$_POST[currentLevel]'>'$_POST[amountToRemove]')
    {
    $result = "UPDATE stockitem 
    SET currentLevel =  currentLevel-'$_POST[amountToRemove]' WHERE stockitem.stockId='$_POST[removeStockId]' ";
    
    $theDate = Date('Ymd');
    $theTime = Date('His');
    $movementID = "SM".Date('Ymdhis').rand(1000,9999);
    
    $sql="INSERT INTO stockmovement(movedBy,stockId,date,time,quantityMoved,movedTo,movementId)
    VALUES
    ('$_POST[staffName]','$_POST[removeStockId]','$theDate','$theTime','$_POST[amountToRemove]','$_POST[area]','$movementID')";  
    
    
    if(!mysql_query($sql,$con))
            {
            echo "</br><script>alert('Error connecting to the database!');</script> ";
            }
    
    else if(!mysql_query($result,$con))
            {
            die('Error: ' . mysql_error());
            }
    else
    {     
      if(mysql_affected_rows() != 0 )
            {
                 echo "</br><script>alert('Item has been removed from the stock room!');</script> ";
                 
                    }
            else 
            {
                echo "<BR>" . "No Change";
            }
    }
    
    }//outer if
    else 
    {
    echo "</br><script>alert('Not enough items in the stock room!');</script> ";#
    echo "</br><script>document.location = 'https://hotel.candept.com/stock/removeStock.html.php';</script> ";
    }
    
    ?>
    


  • Subscribers Posts: 1,911 ✭✭✭Draco


    Your if statement is wrong - you're quoting the values.

    It should look like this:
    if($_POST['currentLevel'] > $_POST['amountToRemove'])
    


  • Advertisement
  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Draco wrote: »
    Your if statement is wrong - you're quoting the values.

    It should look like this:
    if($_POST['currentLevel'] > $_POST['amountToRemove'])
    

    That seems to have fixed it, thanks a million, I would never have noticed that .


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    threein99 wrote: »
    That seems to have fixed it, thanks a million, I would never have noticed that .

    If you want some extra kudo's, sanitise the POST data for SQL Injection.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    ironclaw wrote: »
    If you want some extra kudo's, sanitise the POST data for SQL Injection.

    That's a bit advanced for me I'm afraid


  • Registered Users Posts: 8,004 ✭✭✭ironclaw


    threein99 wrote: »
    That's a bit advanced for me I'm afraid

    Depending on how your connecting, it could literally be one line:
    $currentLevel = mysqli_real_escape_string($link, $_POST['currentLevel']);
    

    $currentLevel is now a 'safe' variable. Won't stop a sophisticated attack but would get your some serious kudo's if you explained it well.

    Link: http://php.net/manual/en/mysqli.real-escape-string.php


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    ironclaw wrote: »
    Depending on how your connecting, it could literally be one line:
    $currentLevel = mysqli_real_escape_string($link, $_POST['currentLevel']);
    

    $currentLevel is now a 'safe' variable. Won't stop a sophisticated attack but would get your some serious kudo's if you explained it well.

    Link: http://php.net/manual/en/mysqli.real-escape-string.php

    This is just a college a project, in a lecture the other day we asked our lecturer to explain how she was using cookies on a login screen she had, she told us to google it so I think using real_escape would be lost on her.


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    err.... doesn't really bode well for your education then!

    That's pretty basic sanitisation, I would think she'd be quite familiar with it.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    Dave! wrote: »
    err.... doesn't really bode well for your education then!

    That's pretty basic sanitisation, I would think she'd be quite familiar with it.

    Don't get me started about how bad she is, we get programming grinds off a guy who works in the industry, he cant believe how rubbish she is. Its the first year this has been taught (not that that's any excuse). If I had a euro for every time we got "google it" as an answer to a question again I wouldn't need to be in college.


  • Closed Accounts Posts: 22,479 ✭✭✭✭philologos


    OP: You are expecting other people to do your work for you. That's not what learning is about. You're in a computer science / software engineering course you should expect to learn.

    Your questions are geared towards "can someone do this for me" rather than can someone give me guidance on what steps to consider, what tools to use so that I can build this myself.

    If while you are learning you are using the "can someone do this for me" what will that mean for when you finish your degree and you're actually developing projects?
    threein99 wrote: »
    This is just a college a project, in a lecture the other day we asked our lecturer to explain how she was using cookies on a login screen she had, she told us to google it so I think using real_escape would be lost on her.

    What a poxy attitude! How are you going to view real life projects if you're thinking that now?

    When I was at uni we were encouraged to learn Android for ourselves for 2 weeks before starting a project. The same for many in terms of PHP / MYSQL and so on. That is an unfair complaint against your lecturer really.


  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    philologos wrote: »
    OP: You are expecting other people to do your work for you. That's not what learning is about. You're in a computer science / software engineering course you should expect to learn.

    Your questions are geared towards "can someone do this for me" rather than can someone give me guidance on what steps to consider, what tools to use so that I can build this myself.

    If while you are learning you are using the "can someone do this for me" what will that mean for when you finish your degree and you're actually developing projects?

    Im not expecting anyone to do my work for me, we started learning php in January, a month after we were giving the brief for our project, our lecturer is the most incompetent educator I have ever had the misfortune of being taught by, there are close to 100 people doing this assignment and everyone has said the same,there have been numerous complaints to the head of the department. I am perfectly aware that learning by doing is the best way to learn a language, but we have literally no notes on the topic, no grounding, we were not taught the basics. This year alone we have been shown, javascript, my sql, and now php, and you can stick java onto that lot as well.


  • Advertisement
  • Closed Accounts Posts: 3,596 ✭✭✭threein99


    philologos wrote: »
    OP: You are expecting other people to do your work for you. That's not what learning is about. You're in a computer science / software engineering course you should expect to learn.

    Your questions are geared towards "can someone do this for me" rather than can someone give me guidance on what steps to consider, what tools to use so that I can build this myself.

    If while you are learning you are using the "can someone do this for me" what will that mean for when you finish your degree and you're actually developing projects?



    What a poxy attitude! How are you going to view real life projects if you're thinking that now?

    When I was at uni we were encouraged to learn Android for ourselves for 2 weeks before starting a project. The same for many in terms of PHP / MYSQL and so on. That is an unfair complaint against your lecturer really.


    One of the lads in the class had that real_escape in his code but it wasn't working so he asked that lecturer why it wasn't and she had no idea what it was. What are our fees being used on ? if the lecturers dont even understand the language


Advertisement