Please help with PHP/MYSQL

Discussion in 'PHP' started by archc, Oct 29, 2012.

  1. #1
    Hi Guys,

    First of all thanks for taking time to read my thread. I'm learing PHP/MYSQL and by now I'm in a position to understand syntax and other basic things. One thing that I'm unable to understand, neither found any explanation about it online.

    My query is, when we code any website using PHP/MYSQL (I use komodo edit or Eclipse), how can I put LOGIN BOX (with user name and password boxes) anywhere I want on the page.

    For Example, look at the file here New Picture (11).jpg

    On right hand side, there is drop box for selecting country, once country is selected, the pages refreshes and states and cities of that country display. When I look at the code, I'm not able to understand how it is done. How can I put entire system (country drop down menu, states, cities) on LEFT hand side (adjacent to "FOR SALE" section). How to retrieve data from database and just display anywhere I want at index page? how it will be done. If I have 20 countries in database, how to display all the countries at footer (list). Is it done by DIV? (sorry for being layman).

    I would be very thankful to whosoever will answer my question.

    Many Many thanks
    Archc
     
    archc, Oct 29, 2012 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    It could be done a number of ways, but without looking at the code it is hard to help.

    One way it could be done is to hide a div on submit.

    Another way it could be done is load different data depending on the URL. For example, if the user selects a country on the page 'countries.php' it could refresh the page and put a variable in the URL such as 'countries.php?country=India' and extract the information from the database on the variable 'india'

    As I say, without looking at the code or the webpage, it's hard to help, but more than likely it is using an if submit statement such as:

    if (isset ($_GET['submit'])) {
    
    //Get country from url
    $country = $_GET['country'];
    
    //Query database depending on the URL variable
    }
    PHP:
     
    scottlpool2003, Oct 29, 2012 IP
  3. archc

    archc Banned

    Messages:
    367
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #3
    Hi Mate,

    Thankyou very much for the reply. I don't want to get it done.. it was just an example. I just wanted to explain what my concern is.

    My question is still not answered. I'm asking in general, how can we do this part:

    "If I have 20 countries in database, how to display all the countries at footer (list). Is it done by DIV? (sorry for being layman).

    I want to retrieve data from database and display where I want on the page. How it will be done?

    Not just country, any data which is present in database, how we display at any page, anywhere ?


     
    archc, Oct 29, 2012 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Firstly you need to connect to your database, see this tutorial: http://www.w3schools.com/php/php_mysql_connect.asp

    Secondly you need to select the table which contains your data and then pull the data from the rows, see here: http://www.tizag.com/mysqlTutorial/mysqlselect.php (this one also contains the connect code).

    The way you display the results is up to you. For best practice, I'd display the data in DIVs, here is some example code:

    
    //Login to mySQL
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    
    //select the database you wish to extract data from
    mysql_select_db("database_name") or die(mysql_error());
    
    //Get a value from the URL
    $country_id = $_GET['country'];
    
    //Query the database for all results where the ID = $country_id
    $result = mysql_query("SELECT * FROM countries WHERE country_id = '$country_id'");
    
    //Make sure there's rows
    if(mysql_num_rows($result)!=0)
    {
    while($row = mysql_fetch_array($result))
      {
        //Ok there are rows, display the data (in this example, the data is a hyperlink
        echo "<div><a href=\"countries.php?id=" . $row['id'] . "\">" . $row['title'] . "</a></div><br />";
      }
    }
    
    PHP:
     
    scottlpool2003, Oct 29, 2012 IP
  5. eqien

    eqien Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    helo....can someone help me... i have problem..

    => i want to used if else statement...i have 3 condition(from database, total1, total2, total3)..then for each total i want to used if else statement..
    => example...

    if total1 > 3
    { score = 1; }

    else
    score = 0;

    => but how i want to write for condition total2 and total 3....
     
    eqien, Oct 31, 2012 IP
  6. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #6
    if ($total1 > = 3){
       $score = 1;}
      else { if ($total2 > = 3){
       $score = 1;}
      else { if $total3 > = 3){
      $score = 1; }}}
    PHP:
    Not sure exactly what your script is doing but if you're adding totals from each you'd use something like:

    $score = 0;
      if $total1 > = 3){
      $score = $score+1;}
      else { if $total2 > = 3){
      $score = $score+1;
      }
      echo $score;
    PHP:
    If the both of the above = > 3 then the output would be 2.
     
    scottlpool2003, Oct 31, 2012 IP
  7. archc

    archc Banned

    Messages:
    367
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    30
    #7
    Do I need to make any other changes except username and password for Mysql connection?
    country id?


     
    archc, Nov 1, 2012 IP
  8. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #8
    This bit is your connection:

    //Login to mySQL
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    
    //select the database you wish to extract data from
    mysql_select_db("database_name") or die(mysql_error());
    PHP:
    Change username, password and database name.
     
    scottlpool2003, Nov 1, 2012 IP